Novalidate 不适用于 FOS 用户注册表
Novalidate isn't working on FOS user registration form
我重写了 FOS UserBundle 注册表并添加了默认选项:'attr'=> array('novalidate'=>'novalidate') 作为回答here(看起来喜欢正确的方式)但由于某些奇怪的原因,novalidated 被添加到 div 紧跟在表单之后而不是表单。
表单类型:
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use AppBundle\Services\RolesHelper;
class UserType extends BaseType
{
/**
* @var RolesHelper
*/
private $roles;
/**
* @param string $class The User class name
* @param RolesHelper $roles Array or roles.
*/
public function __construct($class, RolesHelper $roles)
{
parent::__construct($class);
$this->roles = $roles;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('firstName')
->add('lastName')
->add('roles', 'choice', array(
'choices' => $this->roles->getRoles(),
'required' => false,
'multiple'=>true
));
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'user_registration';
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
parent::setDefaultOptions($resolver);
$resolver->setDefaults(array(
'attr'=> array('novalidate'=>'novalidate'),
));
}
}
我的表单是这样的:
<form action="/app_dev.php/profile/edit" method="POST" class="fos_user_profile_edit">
<div id="fos_user_profile_form" novalidate="novalidate">
// ....
</div>
</form>
为什么要将它添加到表单而不是表单元素之后的 div。我做错了什么吗?
div
上的novalidate="novalide"
错了。你需要把这个放在表格上。
例如这样使用控制器
$form = $this->createForm(new TaskType(), $task, array(
'attr' => array(
'novalidate' => 'novalidate'
)
));
或直接在视图中查看
{{ form_start(form, {attr: {novalidate: 'novalidate'}}) }}
最终结果
<form action="/app_dev.php/profile/edit" method="POST" class="fos_user_profile_edit" novalidate="novalidate">
<div id="fos_user_profile_form">
// ....
</div>
</form>
编辑:
通过表单的最佳解决方案(对于 Symfony <= 2.6)WORKS
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'attr'=> array('novalidate'=>'novalidate'),
));
}
通过表单的最佳解决方案(对于 Symfony >= 2.7)
The configureOptions()
method was introduced in Symfony 2.7.
Previously, the method was called setDefaultOptions().
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'attr'=> array('novalidate'=>'novalidate'),
));
}
重要提示:
如果您使用 FOSUserBundle
,configureOptions
不能直接应用于 form
标签,因为该标签是在捆绑视图中手动调用的。
registration_content.html.twig
中的示例:
<form action="{{ path('fos_user_resetting_reset', {'token': token}) }}" {{ form_enctype(form) }} method="POST" class="fos_user_resetting_reset">
我重写了 FOS UserBundle 注册表并添加了默认选项:'attr'=> array('novalidate'=>'novalidate') 作为回答here(看起来喜欢正确的方式)但由于某些奇怪的原因,novalidated 被添加到 div 紧跟在表单之后而不是表单。
表单类型:
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use AppBundle\Services\RolesHelper;
class UserType extends BaseType
{
/**
* @var RolesHelper
*/
private $roles;
/**
* @param string $class The User class name
* @param RolesHelper $roles Array or roles.
*/
public function __construct($class, RolesHelper $roles)
{
parent::__construct($class);
$this->roles = $roles;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('firstName')
->add('lastName')
->add('roles', 'choice', array(
'choices' => $this->roles->getRoles(),
'required' => false,
'multiple'=>true
));
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'user_registration';
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
parent::setDefaultOptions($resolver);
$resolver->setDefaults(array(
'attr'=> array('novalidate'=>'novalidate'),
));
}
}
我的表单是这样的:
<form action="/app_dev.php/profile/edit" method="POST" class="fos_user_profile_edit">
<div id="fos_user_profile_form" novalidate="novalidate">
// ....
</div>
</form>
为什么要将它添加到表单而不是表单元素之后的 div。我做错了什么吗?
div
上的novalidate="novalide"
错了。你需要把这个放在表格上。
例如这样使用控制器
$form = $this->createForm(new TaskType(), $task, array(
'attr' => array(
'novalidate' => 'novalidate'
)
));
或直接在视图中查看
{{ form_start(form, {attr: {novalidate: 'novalidate'}}) }}
最终结果
<form action="/app_dev.php/profile/edit" method="POST" class="fos_user_profile_edit" novalidate="novalidate">
<div id="fos_user_profile_form">
// ....
</div>
</form>
编辑:
通过表单的最佳解决方案(对于 Symfony <= 2.6)WORKS
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'attr'=> array('novalidate'=>'novalidate'),
));
}
通过表单的最佳解决方案(对于 Symfony >= 2.7)
The
configureOptions()
method was introduced in Symfony 2.7. Previously, the method was called setDefaultOptions().
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'attr'=> array('novalidate'=>'novalidate'),
));
}
重要提示:
如果您使用 FOSUserBundle
,configureOptions
不能直接应用于 form
标签,因为该标签是在捆绑视图中手动调用的。
registration_content.html.twig
中的示例:
<form action="{{ path('fos_user_resetting_reset', {'token': token}) }}" {{ form_enctype(form) }} method="POST" class="fos_user_resetting_reset">