要求不适用于EventSubscriber中的重复字段类型

时间:2013-03-22 00:26:38

标签: symfony symfony-forms fosuserbundle

我使用代码

创建了表单EditFormType
// ...
public function buildForm(FormBuilderInterface $builder, array $options)
{
    // ...
    $builder->addEventSubscriber(new UnsetReadOnlyForEmailField());
}
// ...

UnsetReadOnlyForEmailField

// ...
public static function getSubscribedEvents()
{
    return array(
        FormEvents::PRE_SET_DATA => 'preSetData'
    );
}

public function preSetData(FormEvent $event)
{
    $form = $event->getForm();
    $data = $event->getData();

    if ($data === null) {
        return;
    }

    if ($data->getId() === null) {
        $form->add(
            'plainPassword',
            'repeated',
            array(
                'type' => 'password',
                'options' => array('translation_domain' => 'FOSUserBundle', 'required' => true),
                'first_options' => array('label' => 'form.password'),
                'second_options' => array('label' => 'form.password_confirmation'),
                'invalid_message' => 'fos_user.password.mismatch',
            )
        );
    }
}
// ...

不幸的是required重复字段不起作用,不需要字段。有什么建议?如果我做错了,那么请写下来设置表单中的字段是否需要,具体取决于要编辑或添加的表单。在需要添加表单时,不需要在编辑表单上。

1 个答案:

答案 0 :(得分:0)

我认为你可以这样做:

$form->add(
    'plainPassword',
    'repeated',
    array(
        'type' => 'password',
        'options' => array('translation_domain' => 'FOSUserBundle'),
        'first_options' => array(
             'label' => 'form.password',
             'required' => true
        ),
        'second_options' => array(
            'label' => 'form.password_confirmation',
            'required' => true
         ),
        'invalid_message' => 'fos_user.password.mismatch',
    )
);