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

查看:83
本文介绍了根据Symfony中的另一个字段值验证一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Symfony形式的相关字段:object_statuscryopreservation_method.

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

第一个不能为null,并且存储三个可能的选项之一:liquidsolidcryopreserved.

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

仅当记录的object_status设置为'cryopreserved'时,才应设置第二个.否则为NULL.

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

在保存表单之前,如何在服务器端(不使用Javascript)进行检查?我试图检查模型中的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).

这篇关于根据Symfony中的另一个字段值验证一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆