I have a formMapper Sonata. I have add the prePersist and preUpdate function who are performing some checks before approving changes.
If it fail, I would like to display a nice error message. I have seen that there is some flags who can use but nothing really interesting. Even more, a return false does not stop the action.
Des someone know how could I manage this feature ?
Thanks a lot.
I don't really understand what you don't get in the doc. Here it says that in your controller you need to add
$session->getFlashBag()->add('key', 'message');
And to get it in the view :
{% for message in app.session.flashbag.get('key') %}
<p>{{ message }}</p>
{% endfor %}
You can show a flash message in your hooks accessing the getRequest
method.
public function prePersist($object) {
$this->getRequest()->getSession()->getFlashBag()->add('error', 'Error message');
}
To stop an action from inside an Admin hook, I found that the best way is to throw a ModelManagerException
. This particular exception is, in fact, handled by the CRUDController . Not to clean I have to admit, but effective.
Note: The way to access the flashbag may be a little different with respect too your SonataAdmin version.
What you are probably searching for is a custom validator constraint .
The validator will automatically check for all validator constraints when you submit a sonata form. If there's an error, the entity is not persisted and a nice message is displayed (depending on what constraint validation message you set).
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.