Question

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()

Was it helpful?

Solution

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);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top