SaaS CMS has officially launched! Learn more now.

Single select checkBox in Block


Hi, I am new to epi server. I have a requirment. I have checkboxes(3 or 4) in a block but will have to allow to select only single checkbox at a time. Can anyone help me??

Jul 26, 2022 11:41

Hi, firstly just as a point of education there's no more episerver, it's Optimizely CMS or Optimizely Content Cloud :-)

Second are these checkboxes on the front end of the site or are they properties on your block content model in the backend. Traditionally it's better to use radio buttons if you only want one option to be allowed to be selected as that's what they are for.

Edited, Jul 26, 2022 12:17
Ari - Jul 26, 2022 12:25
Hi, Thanks for your valuable response. The properties are on the block content model at backend. The first preference is to use checkboxes only one option to be selected. If not checkboxes then as you said radio buttons
Scott Reed - Jul 26, 2022 12:28
Actually normally what I do is just provide a dropdown list and therefore only one option can be selected.
However if you want to use checkboxes you can implement the IValidate interface for the block type and validate that only one has been selected which will stop the editor from being able to publish and show a red error in the validation area
Scott Reed - Jul 26, 2022 12:29
Or create a validation attribute. Here is a good blog article on it
Ari - Jul 26, 2022 12:33
If I want to implement radio buttons will IValidate interface work?
Scott Reed - Jul 26, 2022 13:32
IValidate just validates the IContent as it saves so anything you can expose as a property will work.

As mentioned if I have multiple values I usually just use a dropdown (as below) as they are super easy to implement. Then they are forced to one value.

Jul 26, 2022 13:35
Ari - Jul 26, 2022 13:59
Yes I got your point, but in my case there will be 3 or 4 checkboxes in different blocks and I want to make a common validator for them
Scott Reed - Jul 26, 2022 14:01
Yes sure, just radio buttons or dropdowns don't require a validator. It's just intrinsic that they only allow one so you don't need to create anything. Just suggestions
Ari - Jul 26, 2022 14:11
I understood but right now it's not allowing one, I can select multiple at a time which I want to restrict before editor publish it
Ari - Jul 26, 2022 14:15
public class A: BlockData

Name = "Information.",
Description = "Information",
Order = 10)]
public virtual bool Information { get; set; }

Name = "Warning",
Description = "Warning",
Order = 20)]
public virtual bool Warning { get; set; }

Name = "Error",
Description = "Error",
Order = 30)]
public virtual bool Error { get; set; }

this is how block looks like
Scott Reed - Jul 26, 2022 14:56
I was talking about a dropdown or checkbox list and they would be implemented as a single property not separate properties as you've done with your separate bools. You would just implement one property with a selection factory as shown here Usually we load them just from an Enumeration that can be updated on any their usages as per Conceptually if you're creating a selection value you usually just set it up with optimizely as a single property rather than individual properties as it's confusing to the editor.
Ari - Jul 27, 2022 9:31
Hi, I got your point and implemented a single property, Is there any checkbox click event available for that single property??
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.