How to stop saving resource model and throw error messages inside _beforeSave function?
-
24-12-2020 - |
Domanda
I have the following codes inside a custom resourceModel class:
protected function _beforeSave(
AbstractModel $object
)
{
//My operations
return parent::_beforeSave($object);
}
I added a checking inside _beforeSave()
but I don't know how to prevent the resuorce model to continue the save actions.
I found this answer but it's for M1. I need a M2 solution.
Cancel save() in custom model if condition is met in _beforeSave()
Soluzione
Just throw exception.
protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
{
if(!$object->getData('identifier')):
throw new \Magento\Framework\Exception\LocalizedException(
__('The page URL key not set.')
);
endif;
return parent::_beforeSave($object);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange