Idea : model support to validation warning

(Matteo Cerioni) #1

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->getWarningSummary() ecc.

At the moment this is the solution found ( Warning instead of error on model validation )

(Fabrizio Caldarelli) #2

Is not it too much work for a model?

(Matteo Cerioni) #3

depends how it will be used , basically is the same work of the validation process.

(Fabrizio Caldarelli) #4

Exactly, there is already a method to get the errors. Adding another, it can be redundant.

(Matteo Cerioni) #5

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 ?

(Fabrizio Caldarelli) #6

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.

(Matteo Cerioni) #7

Exactly , warnings would facilitate this intercept, for example:

        //set warning summary flash

(Fabrizio Caldarelli) #8

Warnings are not so usual in Model class (I have never needed). This is a too specific case, to be handled by base Model class.

(Matteo Cerioni) #9

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 .

I would be happy to hear other opinions or ideas :smiley:

(Tecnologiaterabyte) #10

In Yii 3.0 you can implement Events, it is the same as you propose basically, you could create Custom Events for each Model.

(Softark) #11

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.

(Andrey Rusovich) #12

as i see it - better to extend Validator and add global option like “level” that can be custom like “error”,“warning” or “notice”…

public function rules()
	return [
		[['title'], 'string', 'max' => 128, 'level' => self::LEVEL_NOTICE],