Search code examples
phpsymfonyvalidationsonata

Make an admin form listen for validation from child admins


I am adding features to an application based on Symfony 2.8 and Sonata.

The application already has a Page entity and a PageAdmin class. I want to add a nested set of Synonym entities on every page, so I make my PageAdmin's configureFormFields() method look like this:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('title')
        ->add('synonym', 'sonata_type_collection', array(
            'label' => "Synonyme",
            'cascade_validation' => true,
            'required' => false,
            'error_bubbling' => true,
        ), array(
            'edit' => 'inline',
            'inline' => 'table'
        ))
        ->add('contentBlock', 'sonata_type_collection', array(
            'label' => "Inhalt",
            'cascade_validation' => true,
            'required' => false
        ), array(
            'edit' => 'inline',
            'inline' => 'table'
        ))
    ;
}

... which generally works pretty well. The only problem is that when I leave one of the required fields in my Synonym entity blank, the application does not give me a pretty red "flash" message scolding me for my omission. Instead, it throws an exception and returns 500 status, which is not what I want to see:

Failed to update object: Application\Sonata\PageBundle\Entity\Page 500 Internal Server Error - ModelManagerException 3 linked Exceptions: NotNullConstraintViolationException » PDOException » PDOException »

...

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null

Is there a way to make omissions from the Synonym fields get flagged nicely for the user, rather than throwing and exception and returning a 500 status?

=====

Update 1: Here is the content of the configureFormFields() method in my SynonymAdmin class:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('name', null, ['label' => 'Name *', 'required' => true, 'error_bubbling' => true,])
        ->add('title', null, ['label' => 'Titel', 'required' => false, 'error_bubbling' => true,])
        ->add('visible', null, ['label'=>'Sichtbarkeit', 'required' => false, 'error_bubbling' => true,])
    ;
}

Update 2: Here is the Synonyms definition in my entity class.

/**
 * @var ArrayCollection
 *
 * @Assert\NotBlank
 *
 */
private $synonyms;

... and from Synonym.php:

/**
 * @var string
 *
 * @Assert\NotBlank
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

Solution

  • For starters I think you can add 'required' => true to the fields in your SynonymAdmin to trigger html5 validation.

    besides that you can add validation rules to your entity and Sonata should pick up on that.

    class Page
    {
        /**
         * @Assert\Valid
         */
         protected $synonyms;
    }
    
    class Synonym
    {
        /**
         * @Assert\NotBlank
         */
         private $name;
    }