How to stop saving resource model and throw error messages inside _beforeSave function?
-
24-12-2020 - |
Frage
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()
Lösung
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);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange