yiisoft / yii2

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

'Uncheck', 'unselect' should not be submitted if attribute is checked #7502

Closed SDKiller closed 9 years ago

SDKiller commented 9 years ago

qip shot - screen 573 28 02 15

It can result ambiguous situation when processing request - finally 'uncheck' values may overwrite 'checked' (especially when using something like jQuery.serialize(), .serializeArray() in ajax request - I've already met this sutuation several times - you cannot rely on elements ordering in html form).

I think yii.activeForm could care of it and make 'uncheck' disabled if it's 'master' checkbox (don't know how to call it correct) is checked and vice versa.

cebe commented 9 years ago

can you show the form code that produces this problem?

SDKiller commented 9 years ago
<?php $form = ActiveForm::begin([
    'id'          => 'add-phone-form',
    'action'      => ['customer/add-phone'],
    'fieldConfig' => ['template' => "{input}\n{error}"],
]); ?>
    <?= $form->field($model, 'phone')->widget(MaskedInput::className(), [
        'mask' => '+7 (999) 999-9999',
    ]) ?>
    <?= $form->field($model, 'comment')->textInput() ?>
    <?= $form->field($model, 'set_primary')->checkbox() ?>
    <?= $form->field($model, 'id')->hiddenInput(); ?>   
<?php ActiveForm::end(); ?>
creocoder commented 9 years ago

I've already met this sutuation several times - you cannot rely on elements ordering in html form

You can. It always come in right order to backend with classic submit.

when using something like jQuery.serialize(), .serializeArray() in ajax request

Its jQuery issue. Just use checkbox() helpers with 'unselect' => false if you use jQuery.

SDKiller commented 9 years ago

Its jQuery issue. Just use checkbox() helpers with 'unselect' => false if you use jQuery.

I don't consider it jQuery issue.

Simultaneos submitting of 2 opposite input values in hope that they will come to server in correct order is bad by design as it initially creates prerequisites for ambiguous situations.

Anyway, its a proposal, I'd better use inputs without such addons and fix my model load() to specially handle certain attributes.

creocoder commented 9 years ago

Simultaneos submitting of 2 opposite input values in hope that they will come to server in correct order is bad by design as it initially creates prerequisites for ambiguous situations.

Such approach is widely used 10 or more years already (not only in Yii) and nobody have any troubles with that.