5

I created an apex:inputCheckbox and I made that checkbox required in the Visualforce page, by using required=true.

I filled in all the fields except this checkbox and hit save, the fields are saving without throwing the apex:inputCheckbox validation

1

3 Answers 3

5

It is not throwing an validation error because an empty checkbox explicitly HAS a value of false, so it is filled in (AKA, it's not null, which Required=true is checking for). You cannot use Required=true for checkboxes because of this. You have a few options.

  • You can write a validation rule on your object that ensures the box is checked
  • You can write the validation into your Save method in your
    controller, and use addError to throw a an error at the user if the
    box is not checked.
1

Unchecked Checkbox represents a false value, therefore it is not null. Hence its not giving error.

0
0

I had the same issue and I able to do with Validation rule on the object to ensure that checkbox is checked, you can do that by doing like this:

IF(fieldname__c , FALSE, TRUE)

Hope this help others.

2
  • You can always replace IF(condition, true, false) with simply condition. Always. The corollary is you can always replace IF(condition, false, true) with NOT(condition). It's much more clear imo.
    – Adrian Larson
    Aug 18, 2016 at 15:42
  • @AdrianLarson: I remembered you have said so first I tried exactly how you have described but it did not work then I have to explicitly defined.
    – Nick
    Aug 18, 2016 at 15:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .