I’ve got a problem with Assert/Callback validation. I used this as a sample for my code, but Symfony just ignores the validation function. This is the relevant part of my entity code
namespace Vendor\Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo; // gedmo annotations
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ExecutionContext;
/**
* @Assert\Callback(methods={"isValidFirma"})
* @ORM\Entity(repositoryClass="Vendor\Bundle\Entity\UserProfileRepository")
* @ORM\Table(name="user_profile")
*/
class UserProfile
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
//...
public function isValidFirma(ExecutionContext $context){
$context->addViolationAtSubPath('Firma', 'Company name must be present', array(), null);
// as of sf 2.3 use addViolationAt() instead [reference: https://github.com/propelorm/PropelBundle/issues/234 ]
}
//...
}
isValidFirma is never invoked. I tried validation.yml file instead of annotation as well, no success. I cleared the cache about fifty times, after every change, didn’t help either. What could be the problem?
The solution. The problem was in used validator groups. The assert validator has to be a part of that group, or else it wont trigger.
This piece of code in form class file was the culprit:
changing the line with assert to
did the trick.