简体   繁体   English

通过Ajax提交表单并显示错误

[英]Submitting A Form Through Ajax And Displaying Errors

I'm attempting to submit step one of my 3 page form via ajax for my Laravel application and I have it set up so that when validation fails on this submission it will submit back with the validation errors which it dos however it still proceeds to the next step in the form process. 我正在尝试通过ajax为我的Laravel应用程序提交我的3页表单的第一步,并且已对其进行设置,以便在对该提交进行验证失败时,它将提交带有验证错误的验证错误,但是它仍继续处理表单处理的下一步。 What am I doing wrong for that to happen still? 我仍然在做错什么呢? I understand that I'm not doing anything when there are errors in the ajax response but why is it still doing what's in the success if there is errors. 我知道当ajax响应中有错误时,我什么也不做,但是如果有错误,为什么它仍然继续成功呢? Does it check errors first then success? 它先检查错误,然后检查成功吗?

/*
* Create User After they complete the first part of the form. 
*
*/
public function createUserFromOrder(Request $request)
{
    $validation = $this->validate($request, [
        'first_name' => 'required',
        'last_name' => 'required',
        'email' => 'required|confirm|unique:users,email',
        'email_confirmation' => 'required'
    ]);

    if ($validation->fails()) {
        return Response()::json([
            'success' => false,
            'errors' => $validation->errors()->toArray()
        ], 200);
    }

    $randomPassword = str_random(7);
    $createdSuccessfully = "<div class='alert alert-success' role='alert'><span class='status-available'> User Profile Created.</span></div>";
    $userData = [
        'email' => $request->email,
        'password' => $randomPassword,
        'first_name' => $request->first_name,
        'last_name' => $request->last_name,
    ];

    $user = Sentinel::registerAndActivate($userData);
    $role = Sentinel::findRoleByName('patient');
    $role->users()->attach($user);

    Sentinel::login($user, true);

    $order = Order::create([
        'program_id' => $request->program_id,
        'program_type_id' => $request->program_type_id,
        'amount' => 433,
        'order_type' => 0,
        'paid' => 0,
        'status' => 0
    ]);
    $user->complete($order);
    $order->addAddon($request->input('addons'));

    return $createdSuccessfully;
}


$.ajax({
        type: "POST",            
        url: '{{action('OrderProcessController@createUserFromOrder') }}',
        data:
            {
                email: email,
                first_name: first_name, 
                last_name: last_name,
                program_id: program_id,
                program_type_id: program_type_id,
                amount: amount,
                addons: addons
            },
        success:function(data){            
            $("#user-created-confirmation").html(data);
        },
        error:function (){}
    }, function(){
        setTimeout(function() {

        })
    });

I believe this is because you may be misunderstanding the "success" property of .ajax() It just means that the AJAX request was made successfully, and not weather your form passed validation. 我相信这是因为您可能会误解.ajax()的“ success”属性,这仅意味着AJAX请求已成功发出,而不是使您的表单通过了验证。 You should put your error handler function inside success function like this: 您应该将错误处理程序函数放入成功函数中,如下所示:

success:function(data){  
        if(AreThereErrors() == false) {
            $("#user-created-confirmation").html(data);
        } else {
            HandleErrors();
        }
    },

If your status code is other than 200 then error block of ajax call is called. 如果您的状态码不是200则将调用ajax调用的error块。 Try to execute your code there. 尝试在那里执行您的代码。

For example: 例如:

error: function(xhr, status, error) {
  var err = JSON.parse(xhr.responseText);
  console.log(err);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM