Validating Checkboxes and Radiobuttons in Symfony

Published 2nd April 2008

Months ago I wrote about the validation of checkboxes in Symfony and how it could fill your database with wrong values.

I recommended downloading, or writing, a small validator which checked if the value of the checkbox is zero or one. But there's a much simpler solution to this problem.

If you use a numerical value for your radiobuttons/checkboxes, simply call the sfNumberValidator with zero as an minmum and one as an maximum.

tos:
    sfNumberValidator:
        min: 0
        max: 1

If you use strings, such as 'male' or 'female' for a gender radiobutton, the sfStringValidator is the right choice:

sfStringValidator:
    values: ['male', 'female']
    value_error: Invalid value