0

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

4

1 回答 1

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',
    )
);
于 2013-03-22T07:59:47.920 回答