laravel 自定义验证类的基本使用

2018-05-14 04:13:07   php分享记录

 

  1. namespace App\Http\Controllers;
  2. use Illuminate\Http\Request;
  3. use App\Http\Requests;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Support\Facades\Validator;
  6. class MobileController extends Controller
  7. {
  8. /**
  9. * Display a listing of the resource.
  10. *
  11. * @return \Illuminate\Http\Response
  12. */
  13. public function login(Request $request)
  14. {
  15. if($request->ajax()){
  16. $validate = Validator::make($request->all(),[
  17. 'work_num'=>"required|max:11",
  18. 'password'=>"required"
  19. ],[
  20. 'work_num.required'=>"工号不可为空",
  21. 'work_num.max'=>"工号长度有误",
  22. 'password.required'=>"密码不可为空"
  23. ]);
  24. if($validate->fails()){
  25. return response()->json($validate->messages());
  26. }
  27. }else{
  28. return view('mobile.index');
  29. }
  30. }
  31. }