A
A
Alexander Bulatov2018-01-29 17:58:04
Yii
Alexander Bulatov, 2018-01-29 17:58:04

Why is the model not saved when disabled TRUE in ActiveForm?

I have a form in view:

<?php $form = ActiveForm::begin(); ?>
    
    <div class="reg-form_wrap-row first">
        
        <div class="center-title">Учетные данные</div>
        
        <?php
        if ($profile->email_confirmed == UsersProfileModel::NOT_CONFIRMED) {
            $emailConfirm = '<a id="sendConfirm" class="btn-red-small btn-padding-12 btn_confirmation-email">Подтвердить</a>';
        } else {
            $emailConfirm = '<div class="input-text_right-info">Email подтвержден</div>';
        }
        if ($profile->phone_confirmed == UsersProfileModel::NOT_CONFIRMED) {
            $phoneConfirmed = '';
        } else {
            $phoneConfirmed = '<div class="input-text_right-info">Телефон подтвержден</div>';
        }
        ?>
        
        <!--~~~~~~~~~~~~~~~~~~~~~~~~~ Center Col/Reg form/Row ~~~~~~~~~~~~~~~~~~~~~~~~~-->
        <?= $form->field($user, 'email', ['options' => [
            'class' => 'input-text-wrap reg-form_width-190'
        ]])->hint($emailConfirm)->textInput(['disabled' => true]) ?>
        
    </div>
    
    <div class="reg-form_wrap-row ">
        
        <div class="center-title">Контактные данные</div>
        
        <?= $form->field($profile, 'firstname')->textInput(['maxlength' => true]) ?>

        <?= $form->field($profile, 'lastname')->textInput(['maxlength' => true]) ?>

        <?= $form->field($profile, 'patronymic')->textInput(['maxlength' => true]) ?>
        
        <?= $form->field($profile, 'birthday')->widget( DatePicker::className(), [
            'language' => 'ru',
            'dateFormat' => 'php:d-m-Y',
            'clientOptions' => ['defaultDate' => '10-10-1950'],
            'options' => [
                'class' => 'form-control',
            ],
        ]) ?>
        
        <!--~~~~~~~~~~~~~~~~~~~~~~~~~ Center Col/Reg form/Row ~~~~~~~~~~~~~~~~~~~~~~~~~-->
        <?= $form->field($profile, 'phone')->hint($phoneConfirmed)->widget(MaskedInput::className(), [
            'mask' => '+7 (999) 999-99-99',
            'options' => ['class' => 'form-control', 'disabled' => true],
        ]) ?>
        
        <?= $form->field($profile, 'skype')->textInput(['maxlength' => true]) ?>

        <?= $form->field($profile, 'social_link')->textInput(['maxlength' => true]) ?>
        
        <hr />
        
        <div class="reg-form_info">Поля, отмеченные <sup>*</sup> , обязательны для заполнения</div>
        
    </div>
    
    <div class="reg-form_wrap-row">

        <div class="center-title">Настройки приватности</div>

        <!--~~~~~~~~~~~~~~~~~~~~~~~~~ Center Col/Reg form/Row ~~~~~~~~~~~~~~~~~~~~~~~~~-->
        <?= $form->field($profile, 'show_profile')->radioList([
            1 => 'Да',
            0 => 'Нет'
        ]) ?>

        <!--~~~~~~~~~~~~~~~~~~~~~~~~~ Center Col/Reg form/Row ~~~~~~~~~~~~~~~~~~~~~~~~~-->
        <?= $form->field($profile, 'allow_write')->radioList([
            1 => 'Да',
            0 => 'Нет'
        ]) ?>


    </div>
    
    <div class="bottom-btn-right">
        <?= Html::a(Yii::t('kupdam', 'Cancel'), ['/users-profile/view', 'id' => Yii::$app->user->id], ['class' => 'btn-blue-middle btn-padding-12']) ?>
        <?= Html::submitButton(Yii::t('kupdam', 'Save'), ['class' => 'btn-red-small btn-padding-12', 'title' => Yii::t('kupdam', 'Save')]) ?>
    </div>

    <?php ActiveForm::end(); ?>

The code in the method that works with the above form:
public function actionUpdate()
    {
        $id = Yii::$app->user->id;
        
        $user = $this->findModel($id);
        $profile = $user->profile;
        $profile->birthday = Yii::$app->formatter->asDate($profile->birthday, 'dd-MM-yyyy');
        
//        echo '<pre>';
//        \yii\helpers\VarDumper::dump($profile);
//        echo '</pre>';
//        die();

        if ( $user->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post()) ) {
            
            
            
            $profile->birthday = Yii::$app->formatter->asDate($profile->birthday, 'yyyy-MM-dd');
            
            if ( $user->save() && $profile->save() ) {
                Yii::$app->session->setFlash('success', Yii::t('test', 'Profile is updated successfuly'));
                return $this->redirect(['view', 'id' => $user->id]);
            } else {
                throw new ServerErrorHttpException(Yii::t('test', 'Updating your data has go wrong'));
            }
            
        } else {
            return $this->render('update', [
                'user' => $user,
                'profile' => $profile,
            ]);
        }
    }

So, when you click on the Save button, it simply redirects to the same page and no errors are reported. BUT it's worth removing the disabled attribute from the fields as everything is saved.
Why?!
I will be glad for any help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
OKyJIucT, 2018-01-29
@alexanderbulatov

Set not disabled = true, but readonly = true. Outwardly, it looks the same, but in the second case, the contents of the field will be saved.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question