【问题标题】:required don't work for repeated field type in EventSubscriberrequired 不适用于 EventSubscriber 中的重复字段类型
【发布时间】:2013-03-11 17:40:00
【问题描述】:

我用代码创建了表单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 重复字段不起作用,字段不是必需的。有什么建议?如果我做错了,请写信设置表单中的字段是否必填,具体取决于要编辑或添加的表单。需要添加表单,不需要编辑表单。

【问题讨论】:

    标签: symfony symfony-forms fosuserbundle


    【解决方案1】:

    我认为你可以这样做:

    $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',
        )
    );
    

    【讨论】:

    • 谢谢回答,我在EditProfileType 中发现了一个错误,我在EditProfileTypeUnsetReadOnlyForEmailField 两个地方声明了这个字段。
    猜你喜欢
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 2016-03-07
    相关资源
    最近更新 更多