Hi guys , i think would be useful to have the possibility to add “soft” warnings to the model during validation that do not compromise the validation return value, but were accessible through functions like the errors, for example:
$model->hasWarning()
$model->getWarnings()
$model->getWarningSummary() ecc.
But errors are differents from warnings, the solution linked above using the session is an incorrect trick because the session flash message should be added by the controller not by the model.
If during validation i want to notify the user or logging an inconsistency of the data, but not want to stop the entire validation process, for example to continue the save of an ActiveRecord in a db.
How can i do it correctly ?
If there are validation errors, you can’t save record in db.
If there also other kind of errors (but I’d not call them validations errors to avoid confusing with model errors), I’d intercept in action controller and setting a session var if I need to get them after a redirect.
Understand, but in certain type of application they would be very useful. For example importing data from console or in complex models.
At the moment one correct way i found is to create model methods named for example “checkWarningA” , “checkWarningB” ecc…
And in every point of the controllers that i run the model validation , i must remember to call every “checkWarning” function and get the warning message from the model through constants.
But in this way i can not use the standard core validators .
If we standardize the warnings , must be useful and simple to add a property to the base validator that would make it “soft”, adding a warning instead of an error.
In this way we could simply manage the warnings using all the logic already present in the model, like the rules() method and the scenarios.
I don’t think that the warnings would increment the “work” of the model. Because if you do not use them it will not change anything in the model validation process.
If with “work” did you mean to add new methods to the model , we can reuse the error functions with a specific parameter, but personally i prefer the first way .
This is not clear enough. Could you provide us with concrete examples?
I agree with @fabriziocaldarelli. I couldn’t imagine a use case that requires the model to have such a “warning” feature. Maybe you could consider creating a "Behavior"s for Model and ActiveForm in order to support “warnings”, but I don’t want them to be implemented as the standard features of Model and ActiveForm.