根据Symfony中的另一个字段值验证字段(Validate a field depending on another field value in Symfony)

编程入门 行业动态 更新时间:2024-10-17 02:54:26
根据Symfony中的另一个字段值验证字段(Validate a field depending on another field value in Symfony)

我有一个Symfony形式的两个相关字段: object_status和cryopreservation_method 。

第一个不能为空并存储三种可能的选择之一: liquid , solid或cryopreserved 。

如果记录的object_status设置为'cryopreserved'则只应设置第二个。 否则为NULL 。

在保存表单之前,如何在服务器端(不使用Javascript)检查这个? 我试图在模型中检查null或空值,但没有运气。

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.

最满意答案

您必须创建条件验证器。 这可以使用sfValidatorCallback完成(比创建新的验证器更容易)。 查看Symfony Cookbook的这个例子 (适用于1.2但适用于1.4)。

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).

更多推荐

本文发布于:2023-08-04 13:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416026.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   Symfony   Validate   depending   field

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!