Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/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
Php 将约束传递给symfony集合类型_Php_Symfony_Symfony Forms_Symfony 2.8_Symfony Validator - Fatal编程技术网

Php 将约束传递给symfony集合类型

Php 将约束传递给symfony集合类型,php,symfony,symfony-forms,symfony-2.8,symfony-validator,Php,Symfony,Symfony Forms,Symfony 2.8,Symfony Validator,Symfony 2.8。我将使用带有自定义条目的集合作为表单,并向其传递一些约束。现在,我的代码如下所示: FirstFormType.php: class FirstFormType extends AbstractFormType { /** @inheritdoc */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder

Symfony 2.8。我将使用带有自定义条目的集合作为表单,并向其传递一些约束。现在,我的代码如下所示:

FirstFormType.php

class FirstFormType extends AbstractFormType
{
    /** @inheritdoc */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('rates', CollectionType::class, [
                'entry_type'   => new SecondFormType([new LessThanOrEqual(300)]),
                'allow_add'    => true,
                'allow_delete' => true,
                'delete_empty' => true,
                'constraints'  => [
                    new NotBlank(),
                    new Count(['min' => 1]),
                ],
            ])
        ;
    }
}
class SecondFormType extends AbstractFormType
{
    /** @var array */
    private $constraints;

    /** @param array $constraints */
    public function __construct(array $constraints = [])
    {
        $this->constraints = $constraints;
    }

    /** @inheritdoc */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('amount', NumberType::class, [
                'scale'       => 2,
                'constraints' => array_merge($this->constraints, [new CustomConstraint()]),
            ])
        ;
    }
}
SecondFormType.php

class FirstFormType extends AbstractFormType
{
    /** @inheritdoc */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('rates', CollectionType::class, [
                'entry_type'   => new SecondFormType([new LessThanOrEqual(300)]),
                'allow_add'    => true,
                'allow_delete' => true,
                'delete_empty' => true,
                'constraints'  => [
                    new NotBlank(),
                    new Count(['min' => 1]),
                ],
            ])
        ;
    }
}
class SecondFormType extends AbstractFormType
{
    /** @var array */
    private $constraints;

    /** @param array $constraints */
    public function __construct(array $constraints = [])
    {
        $this->constraints = $constraints;
    }

    /** @inheritdoc */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('amount', NumberType::class, [
                'scale'       => 2,
                'constraints' => array_merge($this->constraints, [new CustomConstraint()]),
            ])
        ;
    }
}

我还有
ThirdFormType
,它与
FirstFormType
相同,但有其他约束传递给
SecondFormType
。但是我想将
新的SecondFormType([…])
替换为FQCN,我想我可以更正确地继承约束。你知道我该如何做吗?

从表单类型迁移此类构造函数参数的推荐方法是为它们创建表单选项,如下所示:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefault('amount_constraints', array());
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('amount', NumberType::class, [
            'scale' => 2,
            'constraints' => array_merge($options['amount_constraints'], [new CustomConstraint()]),
        ])
    ;
}
然后,新选项的用法如下所示:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('rates', CollectionType::class, [
            'entry_type'   => SecondFormType::class,
            'entry_options' => [
                'amount_constraints' => [new LessThanOrEqual(300)], 
            ],
            // ...
        ])
    ;
}