Looking at the code of save
method in Magento\Framework\Model\ResourceModel\Db
, I can see that there is validateBeforeSave
method which is being called on model object, so you can be usedoverride _getValidationRulesBeforeSave
method in your model where you can provide set of validation rules to validate the currentyour model data. If
Additionally, if validation fails then you can use _dataSaveAllowed
flag to stop the saving operation.
Refer Magento\User\Model\User.php
for reference.
I hope this helps someone looking for model data validation.