简体   繁体   中英

Symfony2 + Sonata custom validation constraint CSS

I am using Symfony2 2.5.2 as well as the Sonata Admin Bundle for a small project.

For one of the entities I've generated CRUD UIs with Sonata I needed a custom validation rule which I've created according to the Symfony2 cookbook.

My problem is this: it seems that for the fields validated with this custom constraint, the error CSS isn't applied. When I do addViolation("message"), the "message" is added to the list of errors displayed but while the fields validated with classic validators have an error CSS class applied to them, those validated with my custom constraint have no visual style.

Can anybody suggest what might be wrong? I haven't found a similar subject here. To me all seems well as far as the validation is concerned.

Thanks!

You could create a callback for your custom validation, define an error message and saved it into "sonata_flash_error", this should work.

$formMapper
    ->with('form.group_general')
    ->add('parent', 'doctrine_phpcr_odm_tree', 
        array(
            'constraints' => array(new Assert\Callback(array(array($this, 'validateParent')))
        )
));

public function validateParent($event, ExecutionContextInterface $context){
    $user = $this->getCurrentUser();
    if (!$user->hasRole('ROLE_SONATA_ADMIN')){
        $errorMessage = 'You are not allowed to change this value';
        $context->buildViolation($errorMessage)
                ->addViolation();
        $this->addFlash('sonata_flash_error', $errorMessage);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM