Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation of nested attributes does not work #26

Open
TomasMolnar opened this issue Nov 15, 2017 · 2 comments
Open

Validation of nested attributes does not work #26

TomasMolnar opened this issue Nov 15, 2017 · 2 comments

Comments

@TomasMolnar
Copy link

TomasMolnar commented Nov 15, 2017

If you try to validate nested attributes, it does not work - so even your example is wrong:

public function rules()
    {
        return [['dimensions.length', 'double', 'min' => 0.0]];
    }

Validation of nested attributes can't work due to fact, that yii\validators\Validator uses $model->$attribute instead of $model->getAttribute($attribute) in validateAttribute($model, $attribute) method:

public function validateAttribute($model, $attribute)
    {
    	$result = $this->validateValue($model->$attribute);
        
        if (!empty($result)) {
            $this->addError($model, $attribute, $result[0], $result[1]);
        }
    }

How to overcome this issue? We could override the validateAttribute() method of yii\validators\Validator class in our own inhered class and create a Standalone Validator for every validator type we want to use for nested dynamic attributes.
Is there a better way?

@djagya
Copy link
Collaborator

djagya commented Nov 15, 2017

@TomasMolnar $model->$attribute uses DynamicActiveRecord::__get($name) internally, that also uses $model-> getAttribute($name), so validation is functional (just tested it locally too).

I found that we don't have unit tests for this case, we'll add them. But if validation doesn't work in your case, it must be something else.

@TomasMolnar
Copy link
Author

TomasMolnar commented Nov 15, 2017

Thanks @djagya for your quick response. You are right, the issue was caused by forgotten JsonBehavior, which was tied to the same attribute. I was playing around with the best method of saving schemaless data :)
BTW thanks for this great extension!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants