有条件的字段验证依赖于另一个字段 [英] Conditional field validation that depends on another field

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

问题描述

我需要更改表单中某个字段的验证。验证器通过非常大的yml文件进行配置。我想知道是否有办法一次对两个字段进行验证。
在我的情况下,我有两个不能同时为空的字段。至少需要填写一个。



不幸的是,到目前为止,我只能看到验证是基于每个字段定义的,而不是多个字段。 p>

现在的问题是:是否可以在标准的yml配置中执行上述验证?



谢谢!

解决方案

我建议你看看 Custom Validator ,尤其是=nofollow> Class Constraint Validator 。



我不会复制粘贴整个代码,只是您必须更改的部分。 p>




扩展约束类。



src / Acme / DemoBundle / Validator / Constraints / CheckTwoFields.php

 <?php 

命名空间Acme \DemoBundle\Validator\Constraints;

使用Symfony \ Component \Vididator\Constraint;

/ **
* @Annotation
* /
class CheckTwoFields extends约束
{
public $ message ='您必须填写foo或bar field。';

public function validatedBy()
{
return'CheckTwoFieldsValidator';
}

public function getTargets()
{
return self :: CLASS_CONSTRAINT;




$ b

通过扩展 ConstraintValidator 类, foo bar 是您要检查的两个字段:



src / Acme / DemoBundle / Validator / Constraints / CheckTwoFieldsValidator.php

  namespace Acme \DemoBundle\Validator\Constraints; 

使用Symfony \ Component \Vididator\Constraint;
使用Symfony \ Component \Validator\ConstraintValidator;

class CheckTwoFieldsValidator extends ConstraintValidator
{
public function validate($ protocol,Constraint $ constraint)
{
if((empty($ protocol-> ; getFoo()))&&(empty($ protocol-> getBar()))){
$ this-> context-> addViolationAt('foo',$ constraint-> message ,array(),null);





使用验证器: p>

src / Acme / DemoBundle / Resources / config / validation.yml

  Acme \DemoBundle\Entity\AcmeEntity:
约束:
- Acme\DemoBundle\Validator\Constraints\CheckTwoFields: 〜


I need to change the validation of some field in a form. The validator is configured via a quite large yml file. I wonder if there is any way to do validation on two fields at once. In my case I have two fields that cannot be both empty. At least one has to be filled.

Unfortunately till now I just could see that the validation are defined on a per-field basis, not on multiple fields together.

The question is: is it possible in the standard yml configurations to perform the aforementioned validation?

thanks!

解决方案

I suggest you to look at Custom validator, especially Class Constraint Validator.

I won't copy paste the whole code, just the parts which you will have to change.


Extends the Constraint class.

src/Acme/DemoBundle/Validator/Constraints/CheckTwoFields.php

<?php

namespace Acme\DemoBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class CheckTwoFields extends Constraint
{
    public $message = 'You must fill the foo or bar field.';

    public function validatedBy()
    {
            return 'CheckTwoFieldsValidator';
    }

    public function getTargets()
    {
            return self::CLASS_CONSTRAINT;
    }
}

Define the validator by extending the ConstraintValidator class, foo and bar are the 2 fields you want to check:

src/Acme/DemoBundle/Validator/Constraints/CheckTwoFieldsValidator.php

namespace Acme\DemoBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class CheckTwoFieldsValidator extends ConstraintValidator
{
    public function validate($protocol, Constraint $constraint)
    {
        if ((empty($protocol->getFoo())) && (empty($protocol->getBar()))) {
            $this->context->addViolationAt('foo', $constraint->message, array(), null);
        }
    }
}

Use the validator:

src/Acme/DemoBundle/Resources/config/validation.yml

Acme\DemoBundle\Entity\AcmeEntity:
    constraints:
        - Acme\DemoBundle\Validator\Constraints\CheckTwoFields: ~

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

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