向SonataUserBundle添加自定义验证规则 [英] Adding customised validation rules to SonataUserBundle
问题描述
我已经根据文档安装了SonataUserBundle,并且一切正常.除了我无法添加自定义验证规则.
I have installed SonataUserBundle according to the docs and it all works fine. Except that I cannot add custom validation rules.
我的理解是,应将新规则添加到新的验证组中,然后更新config.yml以告知SonataUserBundle(或FosUserBundle)将新规则添加到验证序列中.
My understanding is that the new rules should be added to a new Validation Group and then config.yml is updated to tell SonataUserBundle (or FosUserBundle) to add the new rules to the validation sequence.
我已经以各种方式尝试过,但是新规则似乎根本没有被采纳...
I have tried this, in various ways, but the new rules just don't seem to be picked up at all...
这是我正在使用的配置...
Here's the configuration I'm using...
(出于这个示例的目的,我只是尝试向新的foo字段添加NotNull约束.实际上,我希望看到这么多的工作,然后添加更多的验证规则.)
(For the sake of this example, I'm just trying to adding a NotNull constraint to a new foo field. In reality I would like to see this much work and then add more validation rules.)
我已将foo字段添加到Application\Sonata\UserBundle\Resources\config\doctrine\User.orm.xml
,并且一切正常,将foo字段添加到User.php
类.
I have added the foo field to Application\Sonata\UserBundle\Resources\config\doctrine\User.orm.xml
and that all works fine, adding the foo field to the User.php
class.
# in Application\Sonata\UserBundle\Resources\config\doctrine\User.orm.xml
...
<field name="foo" type="string" length="100" nullable="true" />
...
在User.php中,我们具有带有其getter和setter的属性:
In User.php we have the property with its getters and setters:
// In Application\Sonata\UserBundle\Entity\User.php
// ...
/**
* @var string
*/
private $foo;
/**
* Set foo
*
* @param string $foo
* @return User
*/
public function setFoo($foo)
{
$this->foo = $foo;
return $this;
}
/**
* Get foo
*
* @return string
*/
public function getFoo()
{
return $this->foo;
}
// ...
然后,我已将新的验证规则添加到项目的现有validation.yml文件中:
I have then added the new validation rule to my project's existing validation.yml file:
Application\Sonata\UserBundle\Entity\User:
properties:
foo:
- NotNull: { groups: [CustomGroup] }
(请注意,我也尝试过在Application \ Sonata \ UserBundle \ Resources \ config中创建一个validation.yml和validation.xml文件,但这似乎没有任何区别.)
(Note that I have also tried creating a validation.yml and validation.xml file in Application\Sonata\UserBundle\Resources\config but that didn't seem to make any difference.)
在config.yml中,我告诉SonataUserBundle使用新的CustomGroup进行验证:
In config.yml I tell SonataUserBundle to use my new CustomGroup for validation:
sonata_user:
# ...
profile:
form:
validation_groups: [CustomGroup, Profile, Default]
(请注意,我也尝试过在fos_user级别(fos_user.profile.form.validation_groups: [CustomGroup, Profile, Default]
)上添加验证组,并添加sonata_user.profile.register.form.validation_groups: [CustomGroup, Registration, Default]
,但无济于事.)
(Note that I have also tried adding the validation group at the fos_user level (fos_user.profile.form.validation_groups: [CustomGroup, Profile, Default]
) and adding in sonata_user.profile.register.form.validation_groups: [CustomGroup, Registration, Default]
, but to no avail.)
为完整起见,这是添加到UserAdmin.php中的字段:
And, for completeness, here's the field added to UserAdmin.php:
// In Application\Sonata\UserBundle\Admin\UserAdmin.php
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
// ...
->add('foo', null, array('required' => false))
->end()
;
}
那么...我错过了什么? UserAdmin表单与个人资料"表单不同吗? (尽管我也尝试过更新注册表设置)还是在其他地方设置验证规则?
So... what have I missed? Is the UserAdmin form not the same as the 'profile' form? (Although I have also tried updating the registration form settings) Or do I set the validation rules somewhere else?
希望我只是错过了一些小东西!
Hopefully I've just missed something small!
预先感谢
C
推荐答案
UserAdmin表单与配置文件表单不相同.
The UserAdmin form is NOT the same as the profile form.
要使用打开的symfony的事件探查器来找出表单所使用的验证组
To find out what validation group a form is using open up symfony’s profiler
- 将表单发布到开发环境(app_dev.php)
- 打开 http://127.0.0.1:8000/app_dev.php/_profiler/
- 查看最近10个提交的内容.
- 单击提交表单的POST方法(在本例中为sonata用户admin创建表单)
- 单击FORMS
- 在通过的选项"或已解决的选项"中,您应该看到设置了哪些validation_groups.
- post your form in the development environment (app_dev.php)
- open http://127.0.0.1:8000/app_dev.php/_profiler/
- view the last 10 submissions.
- click the POST method your form was submitted on (in this case the sonata user admin create form)
- click FORMS
- in Passed Options or Resolved Options you should see what validation_groups is set.
要将自定义验证规则添加到SonataUserBundle的UserAdmin创建或编辑用户,您需要执行以下操作:
To add customized validation rules to the SonataUserBundle’s UserAdmin create or edit user, you’ll need to do the following:
如果您查看 https://github. com/sonata-project/SonataUserBundle/blob/master/Admin/Model/UserAdmin.php 您会看到用户管理员的validation_groups硬编码为注册(新)或个人资料(编辑).
if you look at https://github.com/sonata-project/SonataUserBundle/blob/master/Admin/Model/UserAdmin.php You’ll see the validation_groups for the user admin are hard coded to Registration (new) or Profile (edit).
您有2个选择.
A)只需将验证规则添加到项目的validate.yml中,并使用现有的注册验证组即可.
Application\Sonata\UserBundle\Entity\User:
properties:
foo:
- NotNull: { groups: [Registration, Profile] }
B)覆盖UserAdmin :: getFormBulder()以添加您的自定义验证组
// In Application\Sonata\UserBundle\Admin\UserAdmin.php
public function getFormBuilder()
{
$this->formOptions['data_class'] = $this->getClass();
$options = $this->formOptions;
$options['validation_groups'] = (!$this->getSubject() || is_null($this->getSubject()->getId())) ? ['CustomGroup','Registration'] : 'Profile';
$formBuilder = $this->getFormContractor()->getFormBuilder( $this->getUniqid(), $options);
$this->defineFormBuilder($formBuilder);
return $formBuilder;
}
这篇关于向SonataUserBundle添加自定义验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!