Symfony2: How to validate an input field is not-blank, only when checkbox is true?

后端 未结 3 1744
日久生厌
日久生厌 2020-12-30 14:51

Within Symfony2 how to validate an inputfield is not-blank, only when the value of a checkbox is 1 (True) - otherwise blank is allowed?

To be more precise, I have a

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 14:59

    An efficient approach can be to use the validation group which can be determined on the fly. Then, given the checkbox is not checked, you use the Default validation group whereas if the checkbox is checked, you use the Default group and a custom group which will check if your field is not empty.

    See this part of the documentation for more details: http://symfony.com/doc/current/book/forms.html#groups-based-on-the-submitted-data

提交回复
热议问题