PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 20, 2022

[FIXED] Cancel the saving or the update in prePersist

 February 20, 2022     sonata-admin, symfony     No comments   

Issue

I would like to cancel the operation if persist after-has test.and i dont know how i tried the redirection goal in vain.

/**
 * @param mixed $object
 */
public function prePersist($object)
{
    if (is_null($object->getFile())) {
    }
}

any help ?


Solution

It's probably a bit late for you. Since I was looking for the same thing though, here's my solution.

It seems not to be possible to do any validation in Sonatas prePersist method.

For a simple NotNull validation like you are doing it I'd add a simple validation constraint to the entity or if the field is not mapped to the form element itself.

If you want to do some extra validation during Sonatas save process, override the validate method. This one is called before the prePersis/preUpdate methods and it allows you to add validation messages.

use Sonata\CoreBundle\Validator\ErrorElement;

public function validate(ErrorElement $errorElement, $object)
{
    $errorElement->with('file')->addViolation('Hey, this is a validation message');
}


Answered By - Samuel Wicky
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing