yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Client validation works incorrect when we have relation models #13408

Open maxlapko opened 7 years ago

maxlapko commented 7 years ago

Hello, I have code

<?php $form = ActiveForm::begin([
    'enableClientValidation' => true,
]); ?>
    <?php foreach ($model->translates as $translate) : ?>
        <?php $prefix = $model->formName() . '[translates][' . $translate->id . ']'; ?>
        <?= Html::hiddenInput($prefix . '[id]', $translate->id)?>
        <?= $form->field($translate, 'subject')->textInput(['id' => "subject-{$translate->id}", 'name' => $prefix . '[subject]']); ?> 
         // other fields         
    <?php endforeach ?>
    <?= $form->field($model, 'token') ?>
    <?= $form->field($model, 'from') ?>
    // ...
// ... buttons
<?php ActiveForm::end(); ?>

Translate attributes for yiiActiveForm have same id, but input attr is different

jQuery('#w0').yiiActiveForm([{"id":"templatei18n-subject","name":"subject","container":".field-subject-1","input":"#subject-1","validate": "..."}, "...", {"id":"templatei18n-subject","name":"subject","container":".field-subject-2","input":"#subject-2","validate":""}, "..."]

When at least one translate model has errors -> yiiActiveForm marks all models as incorrect!!!

This commit https://github.com/yiisoft/yii2/commit/6745bdb65e1f0a8a91c94348f34b7899e25e2ba9 crashes the correct work

Q A
Yii version 2.0.10
PHP version 5.6
Operating system mac os x
samdark commented 7 years ago

Would you please test with code from master branch?

maxlapko commented 7 years ago

@samdark Installing yiisoft/yii2 (dev-master 9a2b2ea) Same behavior

samdark commented 7 years ago

@arogachev do you have time to look at it?

arogachev commented 7 years ago

@samdark I have not started to write tests for yii.activeForm.js yet. Fixing it without writing tests is unsafe. But yes, I can take a look at this later.

Earlier I used client validation for multiple attributes and It worked without problems. Maybe it's data / markup problem or something was broken in yii.activeForm.js in later commits.