Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony2条件验证_Symfony - Fatal编程技术网

Symfony2条件验证

Symfony2条件验证,symfony,Symfony,我有一个FormType方法 <?php class PostType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) {

我有一个FormType方法

<?php
class PostType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('text')
            ->add('paymentMethod', 'choice', array(
                'required' => true,
                'choices' => array(
                    'cach' => 'Cach',
                    'check'=> 'Check'
                )
            ))
            ->add('checkId', 'integer', array(
                'required' => true
            ))
            ->add('submit', 'submit');
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'AppBundle\Entity\Post',
        ]);
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_post';
    }
}
?>
我希望仅当选中选项检查时,字段“checkId”才必须为TRUE。
我怎样才能做到这一点呢?

好的,谢谢,以下是适合每个人的解决方案:

在实体类中添加回调方法函数

    /**
     * @param ExecutionContextInterface $context
     * @Assert\Callback()
     */
    public function isPaymentIsCheck(ExecutionContextInterface $context)
    {
        if ($this->getPaymentMethod() == 'check' and $this->getCheckId() == '') {
            $context->buildViolation('A check ID as to be defined')
                ->atPath('checkID')
                ->addViolation();
        }
    }
不要忘记在实体类中添加断言ExecutionContextInterface组件:

use Symfony\Component\Validator\Constraints as Assert;    
use Symfony\Component\Validator\Context\ExecutionContextInterface;
最后,不要忘记在细枝模板中显示错误:

    {% if form.vars.errors|length %}
        <div class="alert alert-danger">{{ form_errors(form) }}</div>
    {% endif %}
{%if form.vars.errors | length%}
{{form_errors(form)}}
{%endif%}
希望它能帮助你


更多信息请点击此处:

KevinR所写的肯定会解决您的问题,但我认为还有更干净的解决方案:

想象一个用户实体,它可以是一个普通的用户实体 用户或高级用户。当它是高级用户时,一些额外的 应向用户实体(如信用卡)添加约束条件 详情)。要动态确定应激活哪些组, 您可以创建组序列提供程序。首先,创建实体和 一个名为Premium的新约束组:

这正是你解决这类问题的方法

public function getGroupSequence()
{
    $groups = array('Post'); //Or array('Default') whichever you prefer

    if ($this->getPaymentMethod() === 'Check') {
        $groups[] = 'Check';
    }

    return $groups;
}
当然,您必须在实体映射中添加验证组paymentMethod属性。这里是注释示例,有关更多示例,请查看上面的链接

/**
 * @Assert\IsTrue(groups={"Check"})
 */
private $checkId;
使用回调()或表达式()验证器
/**
 * @Assert\IsTrue(groups={"Check"})
 */
private $checkId;