symfony2实体注释断言/回调方法未调用



我对Assert/Callback验证有问题。我将其用作代码的示例,但是Symfony只是忽略了验证功能。这是我实体代码的相关部分

namespace VendorBundleEntity;
use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo; // gedmo annotations
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentValidatorExecutionContext;

/**
 * @AssertCallback(methods={"isValidFirma"})
 * @ORMEntity(repositoryClass="VendorBundleEntityUserProfileRepository")
 * @ORMTable(name="user_profile")
 */
class UserProfile
{
    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(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永远不会被调用。我尝试了验证文件,而不是注释,没有成功。在每次更改之后,我清除了五十次缓存也无济于事。有什么问题?

解决方案。问题在于用过的验证器组。断言验证器必须是该组的一部分,否则它不会触发。表单类文件中的这段代码是罪魁祸首:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $vg = array('my-profile');

    $resolver->setDefaults(array(
        'validation_groups' => $vg
    ));
}

用断言将线更改为

* @AssertCallback(methods={"isValidFirma"}, groups={"my-profile"})

做到了。

最新更新