Validate a field depending on another field value in Symfony

喜夏-厌秋 提交于 2019-12-01 23:37:19

问题


I have two related fields in a Symfony form: object_status and cryopreservation_method.

The first one cannot be null and stores one of three possible choices: liquid, solid or cryopreserved.

The second one should be only set if a record has its object_status set to 'cryopreserved'. Otherwise it is NULL.

How can I check this in the server side (not with Javascript) before saving the form? I have tried to check for null or empty values in model, but with no luck.


回答1:


You have to create a conditional validator. This can be done using a sfValidatorCallback (easier than creating a new validator). Check this example of the Symfony Cookbook (is for 1.2 but works in 1.4).



来源:https://stackoverflow.com/questions/6441982/validate-a-field-depending-on-another-field-value-in-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!