Yii Framework Forum

Ajax Form validation failed


(tomtrp) #1

Hi !
When I submit a form in my modal using Ajax. If the submission failed, I’m redirected on the action page instead of reloaded by Ajax.

I use an advanced application, I have this problem in my backend, not in my frontend … The code is the same (controller, JS…)… I don’t understand.

Thanks for your help !


(Alexander Makarov) #2

What do you have in logs and response?


(tomtrp) #3

I don’t see any problem in my log, request, response.

I have try to implemente the same modal form with Ajax in the SiteController and it works !
I don’t understand why the action works with SiteController and not with the others ???


(Fabrizio Caldarelli) #4

Could you post ajax request source code?


(tomtrp) #5

Controller :

public function actionUpdateHours($id) {
    $day = Day::findOne($id);
    $hours = $day->getTimetables()
    ->orderBy([
        'begin_hour' => SORT_ASC,
    ])->all();

    if (Model::loadMultiple($hours, Yii::$app->request->post()) && Model::validateMultiple($hours)) 
    {
        foreach ($hours as $hour) {
            $hour->save(false);
        }
        return $this->redirect(['setting/index']);
        
    } else {
        return $this->renderAjax('update-hours', [
            'hours' => $hours,
        ]);
    }
} 

JS :

$("#modalButton").click(function () {
        $(".modal").modal('show')
            .find('#modalContent')
            .load($(this).attr('value'));
    });
    $(document).on("click", '.btn-update-hours', function () {
        var id = $(this).attr('data-id');

        $.get('index.php?r=setting%2Fupdate-hours', {'id':id}, function (data) {
            $('.modal').modal('show')
                .find('#modalContent')
                .html(data);
        });
    });