Answer the question
In order to leave comments, you need to log in
What is the correct approach for the validator?
I meet two different types of data validation: the first one is like in Yii2: it returns a boolean type and an array with error text. The second option is to return void, and if there is an error, then we throw an exception.
What is the best way to do it though? I tried this and that, and in general, so far there hasn’t been any opinion about what is more convenient and architecturally correct :( Maybe there are some other options? It’s purely for self-education :)
If I didn’t describe it clearly, I threw in an example: https ://php-fiddle.com/5ea29ecd05d31/2
Answer the question
In order to leave comments, you need to log in
I don't like either approach.
Validation exception is an oxymoron.
An exception is thrown when the function code can not do its job . To inform about the invalidity of the data is just the function of the validation function. Where is the exception here?
For a function, say, saving data, invalid data is an absolutely legitimate reason to throw an exception. But for the validation function - by no means.
The function for checking the uniqueness of the entered login should not throw an exception if the login is not unique.
It should throw an exception if the database is not available.
But passing a value to a parameter by reference is, as it were, hell of a kstyl, the same architectural gag as global.
I would architecturally use the first approach, but add a method to get the error text.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question