Symfony2 表单在 buidler 中定义相同属性时会忽略自定义字段中的属性

问题描述 投票:0回答:4

当我在表单生成器中定义属性并在自定义字段中定义与默认属性相同的属性时,其他属性将被忽略。在表单生成器中我有:

    $builder
            ->add('validityOfADecisionOnDisability', new JQDateType(), array(
                'attr' => array(
                    'rel' => 'permanent',
                )
            ))

和自定义字段

class JQDateType extends AbstractType {

public function getDefaultOptions(array $options)
{
    return array(
        'widget' => 'single_text',
        'format' => 'yyyy-MM-dd',
        'attr' => array(
            'class' => 'datepicker'
        )
    );
}

它渲染 html

<input type="text" rel="permanent" required="required" 
  name="profile[validityOfADecisionOnDisability]" 
  id="profile_validityOfADecisionOnDisability">

不上课。但是当我将类添加到构建器中的属性时

    $builder
            ->add('validityOfADecisionOnDisability', new JQDateType(), array(
                'attr' => array(
                    'rel' => 'permanent',
                    'class' => 'datepicker',
                )
            ))

一切都按预期进行。我应该如何定义 JQDateType() 中的属性?我尝试在 JQDateType::getDefaultOptions() 中使用 array_merge() 和 array_merge_recursive() 但没有帮助。

php forms symfony custom-controls symfony-forms
4个回答
1
投票

这是 Symfony 2.1 中修复的一个错误。


0
投票

@Koral 我想你可以在实体类中使用注释比“getDefaultOptions”更好,你可以在这里看到:

http://symfony.com/doc/current/book/validation.html


0
投票

是的,您可以直接在实体类中验证表单的所有字段,例如:

/**
 * @Assert\Min(limit = "18", message = "You must be 18 or older to enter")
 */
 protected $age;

0
投票

听起来像一个错误。您应该在 https://github.com/symfony/symfony/issues

上开票
© www.soinside.com 2019 - 2024. All rights reserved.