I have installed SonataUserBundle according to the docs and it all works fine. Except that I cannot add custom validation rules.
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...
(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.)
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" />
...
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;
}
// ...
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] }
(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.)
In config.yml I tell SonataUserBundle to use my new CustomGroup for validation:
sonata_user:
# ...
profile:
form:
validation_groups: [CustomGroup, Profile, 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.)
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()
;
}
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!
Thanks in advance,
C
The UserAdmin form is NOT the same as the profile form.
To find out what validation group a form is using open up symfony’s profiler
- 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.
To add customized validation rules to the SonataUserBundle’s UserAdmin create or edit user, you’ll need to do the following:
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).
You have 2 choices.
A) Just add your validation rules to your projects validation.yml and use the existing Registration validation group.
Application\Sonata\UserBundle\Entity\User:
properties:
foo:
- NotNull: { groups: [Registration, Profile] }
B) Override the UserAdmin::getFormBulder() to add your custom validation group
// 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;
}
来源:https://stackoverflow.com/questions/25609526/adding-customised-validation-rules-to-sonatauserbundle