Closed egorpromo closed 10 years ago
I can take refactoring on my own.
In which case do you need to validate values without models?
In many cases. For example I don't use forms with form models. It is simpler for me in some cases.
Can you show some concrete code example?
If I want to use html-form then I make:
<form method="post" enctype="multipart/form-data" action="">
<input type="file" name="myfile" />
<br/>
<input type="button" name="submit" value="Submit">
</form>
In action it is simpler for me don't use form models.
$file = UploadedFile::getInstanceByName('myfile');
$myFileValidator = new FileValidator();
$error = $myFileValidator->validateItem( $file );
if (empty($error)) {
/* I save my file in proper place */
}
I realy need a validation without models.
You are using controller to hold code that doesn't belong to controller. This is a bad practice, even though it seems simpler for simple cases. I don't think this is convincing argument for this issue.
@qiangxue The validators which validate value not models will be very helpful. First, in some case I prefer use model class without extends Model or AR. Second, validators can be used in filters, behaviors, etc (IP validator, Email validator) to reduce duplicate code.
Only suggestion the method must be static
if (EmailValidator::isValid($mail)) {
// process mail
}
I'm not saying it's useless. As you can see, most validators do support validation without models (e.g. EmailValidator
, DateValidator
). However, FileValidator
is tightly coupled with file upload validation. So I was asking for a convincing argument (this is also because the refactoring is not trivial which could affect the whole architecture of validator design.)
Just FYI, we already have Validator::validateValue()
for this purpose. Some validators (like FileValidator
) do not support this because they are not suitable for this.
All validators can support validation without model if new validateItem()
as I propose will be invented. As I can see from the code of yii\validators\Validator the validation without model is by architecture (see validateValue() method). But after many years of heavy development the code begins rot and yii developers forget about big opportunities. I think if it is possible to realize for convenient using it must be realized.
@egorpromo The returned value proposed by you is very unusual because it requires certain postprocessing to make it meaningful and useful, which is not a good design.
@qiangxue If developers want easy way they can use isValid()
. validateItem()
can be used for wide range of unidirectional tasks.
Also validateItem()
method is good approach to problem of validation without model.
And validateItem()
is good approach to problem of code duplication.
@qiangxue Look at the yii\validators\StringValidator::validateValue() method and yii\validators\StringValidator::validateAttribute(). It is the duplication. Using new validateItem()
method the problem will be eliminated.
@qiangxue Now look at validateValue() and validateAttribute() of yii\validators\DateValidator class :)
@egorpromo I'm aware of these code duplication from the very beginning. It all depends on the requirement for the validators. The current design is that when you use validators out of models (which is not recommended), you usually only care whether the data validates or not. Anyway, I will reconsider the design.
@qiangxue I can do some commits to help you :)
No, thanks. This requires very big change. It's more efficient I directly work on it.
@qiangxue Will be very good if you refactor design to decoupled validators from models. Because now for every model from collection created instance of validator, but need only one.
@slavcodev The current validator design already allows you to use it without models. This redesign is more about internal refactoring.
We can make validators more robust and universial. I have found that we have addError() method that needs 'error message' and 'error params'. These parameters can be obtain if we take new allocated method, for example
validateItem()
, which goal is to return 'error message' and 'error params'.What will we have if we will use these two methods for any validator?
validateItem()
method and returned result will inform developers about problems with validation.isValid()
method and returned result will only inform developers valid value or invalid, because it returnes true or false only.validateItem()
anyway.So we need some refactoring to make all validators robust and universal.
My issue related to https://github.com/yiisoft/yii2/pull/1431