使用laravel进行ajax验证后的新用户

时间:2016-06-15 02:17:05

标签: php ajax validation laravel laravel-5.2

我希望在验证使用ajax发送的数据后创建记录。想法是在没有错误的情况下运行CreateNewUser函数,但不将数据存储在DB中。会发生这种情况吗?

谢谢。     

web

模型:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Modals\NewUser;

class UserController extends Controller
{
    public function index()
    {
        return view('home');
    }
    public function NewUser(Request $request)
    {
        $validator= Validator::make($request->all(),[

            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:8|same:cpassword',
            'cpassword'=> 'required',

        ]);

        if($validator->fails()){
            return response()->json([
                'success' => false,
                'error' => $validator->errors()->toArray()
            ]);


        }else{
            return response()->json(['success' => true ]);

            $this->CreateNewUser($request);
        }

    }

    private function CreateNewUser(Request $requestt){

        $user = new NewUser;
        $user->name = $requestt->name;
        $user->email = $requestt->email;
        $user->passwosrd = $requestt->password;
        $user->rememberToken = $requestt->_token;
        $user->save();
    }


}

JS:

<?php

namespace App\Modals;

use Illuminate\Database\Eloquent\Model;

class NewUser extends Model
{
    protected $table = 'users';
}

错误:

function send(event){

    event.preventDefault();

    $.ajax({


        type: 'post',
        url: userAjax,
        dataType: 'json',
        data: {
            name: $('#name').val(),
            email: $('#correo').val(),
            password: $('#password').val(),
            cpassword: $('#cpassword').val(),
            _token: $('#new-user').attr('data-token')
        },
        beforeSend: function()
        {
            $("#nameError").fadeOut();
            $("#emailError").fadeOut();
            $("#passwordError").fadeOut();
            $("#cpasswordError").fadeOut();

        },

        success: function(data){



           if(!data.success){
               console.log(data);
               if(typeof data.error.name !== 'undefined'){

                   $('#name').css('border-color', 'red');
                   $('#nameError').fadeIn(10, function () {
                       $("#nameError").html(data.error.name);

                   })
               }
               if(typeof data.error.email !== 'undefined'){
                   $('#correo').css('border-color', 'red');
                   $('#emailError').fadeIn(10, function () {
                       $("#emailError").html(data.error.email);

                   })
               }
               if(typeof data.error.password !== 'undefined'){
                   $('#password').css('border-color', 'red');
                   $('#passwordError').fadeIn(10, function () {
                       $("#passwordError").html(data.error.password[0]);


                   })
               }
               if(typeof data.error.cpassword !== 'undefined'){
                   $('#cpassword').css('border-color', 'red');
                   $('#cpasswordError').fadeIn(10, function () {
                       $("#cpasswordError").html(data.error.cpassword);


                   })
               }


            }else{
               console.log(data);
               $('#Register').modal('hide');
               $("#header").replaceWith($('#header'));
            }

        },
        error: function() {}

    });

}

1 个答案:

答案 0 :(得分:1)

函数CreateNewUser永远不会被调用,因为你在函数调用之前返回它会破坏范围并且永远不会调用$this->CreateNewUser($request);

public function NewUser(Request $request)
    {
        $validator= Validator::make($request->all(),[

            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:8|same:cpassword',
            'cpassword'=> 'required',

        ]);

        if($validator->fails()){
            return response()->json([
                'success' => false,
                'error' => $validator->errors()->toArray()
            ]);


        }else{
            return response()->json(['success' => true ]);

            $this->CreateNewUser($request);
        }

    }

要使其正常工作,请将else代码更改为

else {
      $this->CreateNewUser($request);
  }