我使用代码
创建了表单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
重复字段不起作用,不需要字段。有什么建议?如果我做错了,那么请写下来设置表单中的字段是否需要,具体取决于要编辑或添加的表单。在需要添加表单时,不需要在编辑表单上。
答案 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',
)
);