简体   繁体   中英

Sonata Admin validation

I have an trouble with validation in Sonata Admin.

On frontend for clients I have two forms: Registration and ProfileEdit. Based on FOSUser. Rules for validation are in:

src/UserBundle/Resources/config/validation.yml

There are two groups of validation for each form. For me is preferable in backend to use this rules and add one more group like AdminProfileEdit because there are diffences with frontend forms. But, I cannot understand how to config? In docs I read about inlineValidation - add in UserAdmin class a method

/**
 * @param ErrorElement $errorElement
 * @param mixed $object
 */
public function validate(ErrorElement $errorElement, $object)
{
    $errorElement
        ->with('profile.firstname')
            ->assertNotBlank()
        ->end()
        ....
    ;
}

but for me it is bad solution to have 2 validation configs. In https://sonata-project.org/bundles/admin/2-0/doc/reference/conditional_validation.html I can not find a good explanation how to validate a backend form.

In Sonata\\AdminBundle\\Admin\\AdminInterface method validate is marked as deprecated:

  • @deprecated this feature cannot be stable, use a custom validator,
  • the feature will be removed with Symfony 2.2

what it mean? And how to validate? Can someone write step-by-step what to do? Even what methods or configs I need to have?

Please help someone, I need to make task fastly, and donn't have plan or solution.

You can define a validation group for the admin and specify it as a form option in the admin class :

protected $formOptions = array(
    'validation_groups' => array('SomeGroup'),
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM