K
K
kirzzzz2021-01-24 04:15:36
Yii
kirzzzz, 2021-01-24 04:15:36

Why is the form not submitted in Yii2 when editing?

There is a form in the admin panel to create a user. User services and working hours are filled in the same form. Services use tabular input, business hours do the same.
When creating, everything is filled in correctly, but when trying to edit the user (All data is also displayed correctly), the submit button is "inactive" - ​​a click occurs, the function in js submit fires, but the form does not submit (there is no redirect to the current form action).

Page code:

<?php $form = ActiveForm::begin([
        'options' => ['data-validate'=>'timesinterval'],
    ])?>
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Основная информация</h6>
        </div>
        <div class="card-body">
            <div class="user">
                <div class="form-group row">
                    <div class="col-sm-6 mb-3 mb-sm-0">
                        <?= Html::activeTextInput($model, 'name', ['class'=>'form-control form-control-user','placeholder' => 'Введите Имя']); ?>
                    </div>
                </div>
                <div class="form-group">
                    <?= Html::activeTextInput($model, 'tel', ['class'=>'form-control form-control-user','placeholder' => 'Введите телефон','data-mask'=>'tel','readonly'=>isset($model->id)]); ?>
                </div>
            </div>
        </div>
    </div>
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Услуги</h6>
        </div>
        <div class="card-body">
            <div class="user">
                <?php if(isset($modelSer) and isset($service)):for($i = 0; $i < count($modelSer); $i++):?><?=
                Html::activeInput('hidden', $modelSer[$i], "[$i]service_id", ['value'=>$service[$i]['id']]).'
                <div class="form-group row">
                    <div class="col-sm-6 mb-3 mb-sm-0">
                        <div class="custom-control custom-checkbox mr-3">
                            '.Html::activeInput('checkbox', $modelSer[$i], "[$i]status",
                            ['id'=>'service'.$i,'class'=>'custom-control-input', 'checked'=>isset( $modelSer[$i]->status)?( $modelSer[$i]->status=='active'):false]).'
                            <label class="custom-control-label" for="service'.$i.'">'.$service[$i]['service_name'].'</label>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        '.Html::activeInput('text', $modelSer[$i], "[$i]time", ['class'=>'form-control form-control-user','placeholder' => 'Время','data-mask'=>'time']).'
                    </div>
                </div>'?>
                <?php endfor; endif;?>
            </div>
        </div>
    </div>
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Часы</h6>
        </div>
        <div class="card-body">
            <div class="user">
                <div class="form-group">
                    <div class="input-group input-group-joined">
                        <input type="text" id="timesinterval-time-main" class="form-control form-control-user" placeholder="Время" data-mask="time" inputmode="text">
                        <div class="input-group-append">
                            <span class="d-flex align-items-center px-3">
                                <button class="btn btn-teal" type="button" id="add_time">
                                    <i class="fad fa-calendar-plus"></i>
                                </button>
                            </span>
                        </div>
                    </div>
                </div>
                <div class="form-group row flex-wrap" id="add_time_container">
                    <?php if(isset($timeInterval) and isset($timeInterval[0]['id'])):?>
                    <?php foreach ($timeInterval as $inT=>$time):?>
                            <div class="col-sm-4 mb-2" data-delete-time-c="">
                                <div class="input-group input-group-joined">
                                    <?= Html::activeTextInput($time, "[$inT]time", ['class'=>'form-control form-control-user','placeholder' => 'Время','data-mask'=>'time','readonly'=>true]);?>
                                    <div class="input-group-append">
                                        <span class="d-flex align-items-center px-3">
                                            <button class="btn btn-red" type="button" data-delete-time="">
                                                <i class="fad fa-calendar-times"></i>
                                            </button>
                                        </span>
                                    </div>
                                </div>
                            </div>
                    <?php endforeach; endif; ?>
                </div>
            </div>
        </div>
    </div>
    <?= Html::submitButton('Сохранить',['class'=>'btn btn-primary btn-user btn-block']); ?>
    <?php ActiveForm::end() ?>


Controller code:
public function actionCreate()
    {
        $model = new SignupForm();
        $timeInterval = new TimesInterval();
        $service = Service::find()->all();
        $modelSer = [new ServiceTime()];
        for($i = 1; $i < count($service); $i++) {
            $modelSer[] = new ServiceTime();
        }
        $post = Yii::$app->request->post();
        $get = Yii::$app->request->get();

        if(isset($get['id'])){
            $model = $model->findModel($get['id']);
            $modelSer = ServiceTime::findByMaster($get['id']);
            $timeInterval = $timeInterval->findByMaster($get['id']);
        }

        if ($model->load($post) and !isset($model->id)) {
            if ($user = $model->signup('personal')) {
                $auth = Yii::$app->authManager;
                $role = $auth->getRole('personal');
                $auth->assign($role, $user->id);
                if(isset($post['ServiceTime'])){
                    $modelSer = [new ServiceTime()];
                    for($i = 1; $i < count($post['ServiceTime']); $i++) {
                        $modelSer[] = new ServiceTime();
                    }
                    if (ServiceTime::loadMultiple($modelSer, Yii::$app->request->post())) {
                        foreach ($modelSer as $ser) {
                            $ser->status = isset($ser->status)?"active":"passive";
                            $ser->master_id = $user->id;
                            $ser->save(true);
                        }
                    }
                }
                if(isset($post['TimesInterval'])){
                    $timeInterval = [new TimesInterval()];
                    for($i = 1; $i < count($post['TimesInterval']); $i++) {
                        $timeInterval[] = new TimesInterval();
                    }
                    if (TimesInterval::loadMultiple($timeInterval, Yii::$app->request->post())) {
                        foreach ($timeInterval as $item) {
                            $item->master_id = $user->id;
                            $item->save(true);
                        }
                    }
                }
            } 
        }elseif (isset($model->id)){
            if($model->load($post)){
                if($model->update()){
                    Yii::$app->session->setFlash('success', 'Пользователь успешно обновлен! :)');
                }
            }
            $modelSer = ServiceTime::findByMaster($model->id);
            if (ServiceTime::loadMultiple($modelSer, Yii::$app->request->post())) {
                foreach ($modelSer as $ser) {
                    $ser->status = isset($ser->status)?"active":"passive";
                    $ser->save(true);
                }
            }
        }
        return $this->render('create',['model'=>$model,'modelSer'=>$modelSer,'service'=>$service,'timeInterval'=>$timeInterval]);
    }


JS code:
$("#add_time").click(function () {
        $inp = $('#timesinterval-time-main').clone();
        let name = $inp.attr('name','TimesInterval[0][time]').attr('readonly',true);
        let id = $inp.removeAttr('id');
        let div = $('<div class="col-sm-4 mb-2" data-delete-time-c><div class="input-group input-group-joined"></div></div>').children('div')
            .append($inp)
            .append('<div class="input-group-append"><span class="d-flex align-items-center px-3"><button class="btn btn-red" type="button" data-delete-time><i class="fad fa-calendar-times"></i></button></span></div>').end();
        $("#add_time_container").append(div);
        $('#timesinterval-time-main').val('').focus();
    });

    $(document).on('click','[data-delete-time]',function () {
        $(this).closest('[data-delete-time-c]').remove();
    })
$('form[data-validate]').submit(function () {
        let valid = $(this).attr('data-validate');
        $('input[data-inp-validate-'+valid+']').each(function (index,elem) {
            let name = $(this).attr('name').split(/\d+/);
            $(this).attr('name',name[0]+index+name[1]);
        })
        console.log("AAAAAAAAAAAA");
    })


In order not to clutter up the question, I did not add the code of the three models, if necessary, I will add it later.
Also, the piece of code for updating the "Working Hours" in the controller is not completed at the moment, but I don't think yii2 is so smart that it doesn't want to submit the form without this piece...

Thanks in advance for the answer! Hope it's not my stupidity.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kirzzzz, 2021-01-24
@kirzzzz

After spending a lot of hours, I realized that the error was not from Yii2 at all.
Greedy Library

input mask
does not allow submitting the form if the mask is set to readonly\disabled fields (This is a bug).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question