Render Required Boolean Field Radio Buttons without Any Options Selected

lasa jay asked on April 4, 2017 09:43

Hi Kentico,

I have a boolean field in a Kentico form. This is a required field and renders using radio buttons form control. The form control is configured to have the following two options.

1;YES

0;NO

This radio button list renders with Yes option selected by default. Is there a way to render it with no options selected and enforce the user to select an option?

Cheers,

Lasa

Recent Answers


Chetan Sharma answered on April 4, 2017 11:13

  1. Are you using page types to do so?
  2. Is this a Kentico form?
  3. Are you using portal engine development model?

Kentico's boolean data type will show you the options, yes, no and not set. This not set of the option which you are looking for I guess.

Answer to this depends upon above the questions.

0 votesVote for this answer Mark as a Correct answer

lasa jay answered on April 4, 2017 11:28

Thanks for your reply. Answers to your questions are; 1-Yes 2-Yes 3-Yes

"not set" option is only available if the field is not a required field.

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on April 4, 2017 11:41

You may use arrow icon after default value which will open pop up and set the required default value there.

0 votesVote for this answer Mark as a Correct answer

lasa jay answered on April 4, 2017 11:51

I tried to set a null default value via a macro({% return null; %}) but it didn't work. Also tried {% return ""; %} {% return 5; %} macros. But the field always renders with the option "No" selected.

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on April 4, 2017 12:00

If you want an option of not selected then why you are configuring it as a required field. You got all three options when you un check that. You are making it required and then setting null value.

Making it non required will automatically set that value in case you don't choose any.

What do you think?

0 votesVote for this answer Mark as a Correct answer

lasa jay answered on April 4, 2017 12:06

I need the form to throw a validation error if non of the options are selected. Basically, it needs to render without any options selected and the user must select an option to be able to save the form.

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on April 4, 2017 12:09

If it's a radio button and has three options where you set one of them as default option then you will always have an option selected.

The validation thing can be handled and are much better taken care on the front end side using JavaScript. That's what I prefer.

Thanks Chetan

0 votesVote for this answer Mark as a Correct answer

lasa jay answered on April 4, 2017 12:14

Thanks for the suggestion.

Yes, understand I can perform client side validation via JS. I was wondering if there is a workaround within Kentico to achieve the desired functionality.

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on April 4, 2017 12:25

Actually, I thought deeply about and I think these options are correctly set within Kentico. Why do you need a field required if you want to have a not set value as an option?

Selecting a right control like a radio button well affirmatively force a user to choose an option which in your case will be a not set value.

Then why will I need validation on this?

Kentico will check for validation for a required field or you may use patterns/regular expressions with in a control which can be configured at a field level. But I would not take that route.

My two cents.

1 votesVote for this answer Mark as a Correct answer

lasa jay answered on April 4, 2017 14:22

I made the field optional and selected "not set" as the default, but it didn't work, still the field was rendered with option "No" selected. However, I was able to remove the default value by specifying a Macro ({% return null; %}). Now both radio buttons appear un-checked.

However, now I am having issues triggering the validation rules. The field seems to skip all the validation rules. I tried regular expression, email, general condition validation rules. Anyway I will keep trying from my end. Thanks for your help.

0 votesVote for this answer Mark as a Correct answer

lasa jay answered on April 4, 2017 16:06

Looks like the validation rules does not fire when the default value is set to null via the macro condition. Probably the only option is to implement some client side JS validation.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.