Skip to content

Forms

Remove validation constraint

If you need to remove a validation constraint that is defined as an annotation in wfcms/standard:

php
<?php

namespace Wf\Bundle\CmsBaseBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;

class User
{
     /**
     * @ORM\Column(name="last_name", type="string", length=255, nullable=true)
     *
     * @Assert\NotBlank(message="form.error.user.last_name.notblank")
     *
     * @Serializer\Groups({"edit", "list", "version", "api"})
     *
     * @var string
     */
    protected $lastName;

}

you can use Symfony's validation_groups option (Symfony docs).

Setting the validation_groups in a Sonata admin:

php
<?php

namespace App\Bundle\CmsAdminBundle\Admin;

class UserAdmin
{
    protected $formOptions = [
        'validation_groups' => ['custom_group'],
    ];
}

Note that this option can be applied only for the entire form, it cannot be set only for one field.

That means that you'll have to define in the app entity all the fields that still need validation:

php
<?php

namespace App\Bundle\CmsBundle\Entity;

class User
{
    /**
     * @ORM\Column(name="first_name", type="string", length=255, nullable=true)
     *
     * @Assert\NotBlank(message="form.error.user.first_name.notblank", groups={"custom_group"})
     *
     * @Serializer\Groups({"edit", "list", "version", "api"})
     *
     * @var string
     */
    protected $firstName;

    // add the other properties that still require validation

}

If the entire validation should be disabled, you can set validation_groups to false.