Sonataadminbundle: How to validate entities?

Created on 31 May 2013  路  5Comments  路  Source: sonata-project/SonataAdminBundle

Hey,

after reading the chapter about the validation [1] I'm a bit confused on what's the best approach for validation. The only options seem to be:

1) AdminClass:validate() as shown in 11.3. This is marked as deprecated.
2) A separate service as stated in 11.1 which takes the object and a method to validate.

At the moment I have set up the validation through my preferred annotations in the entity class and I'm no fan of redoing this in a separate validation service. Is there an easy way to hook up my asserts in Sonata?

[1] http://sonata-project.org/bundles/admin/master/doc/reference/conditional_validation.html

Cheers
Matthias

Most helpful comment

I always use validation groups for this simply override the getFromBuilder function in your admin class and you can even change the validation group based on edit or create.

public function getFormBuilder() {
        if (is_null($this->getSubject()->getId())) {
            $this->formOptions = array('validation_groups' => array('adminCreate'));
        } else {
            $this->formOptions = array('validation_groups' => array('adminEdit'));
        }
        $formBuilder = parent::getFormBuilder();
        return $formBuilder;
    }

All 5 comments

I always use validation groups for this simply override the getFromBuilder function in your admin class and you can even change the validation group based on edit or create.

public function getFormBuilder() {
        if (is_null($this->getSubject()->getId())) {
            $this->formOptions = array('validation_groups' => array('adminCreate'));
        } else {
            $this->formOptions = array('validation_groups' => array('adminEdit'));
        }
        $formBuilder = parent::getFormBuilder();
        return $formBuilder;
    }

@mahmouds Thanks. So it's using the entity's asserts and I just missed to set the validation group? Oh my... I'll keep this issue open until I've tested this. Will try to update the doc.

In this page of Doctrine2 ORM Admin, the method show in the example.

I use this method quite. How could I replace it?

closing this. @munirsalim can you ask your question on the mailing list.

Did you test the code and update the docs @mahmouds ?

Was this page helpful?
0 / 5 - 0 ratings