最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Laravel框架FormRequest中重寫(xiě)錯(cuò)誤處理的方法

 更新時(shí)間:2019年02月18日 15:37:42   投稿:zx  
這篇文章主要介紹了Laravel框架FormRequest中重寫(xiě)錯(cuò)誤處理的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

laravel 框架中默認(rèn)的validate驗(yàn)證,在處理錯(cuò)誤的時(shí)候,默認(rèn)是返回上一頁(yè),當(dāng)為ajax的時(shí)候才會(huì)返回Json。如果我們要一直返回Json的話,那么需要重寫(xiě)錯(cuò)誤處理

如下:在Requests目錄只用 新建BaseRequest

代碼如下

<?php
/**
 * @文件名稱: BaseRequest.php.
 * @author: daisc
 * @email: jiumengfadian@live.com
 * @Date: 2019/1/8
 */

namespace App\Http\Requests\Front;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class BaseRequest extends FormRequest
{

  public function failedValidation($validator)
  {

    $error= $validator->errors()->all();
    // $error = $validator;

    throw new HttpResponseException(response()->json(['code'=>1,'message'=>$error[0]]));

  }

}

重寫(xiě)了failedValidation方法,將拋出錯(cuò)誤處理為了json格式的。

然后在自定義的處理驗(yàn)證類中,繼承該類就行了,

如:RegisterForm

<?php

namespace App\Http\Requests\Front;

class RegisterForm extends BaseRequest
{
  /**
   * Determine if the user is authorized to make this request.
   *
   * @return bool
   */
  public function authorize()
  {
    return true;
  }

  /**
   * Get the validation rules that apply to the request.
   *
   * @return array
   */
  public function rules()
  {
    return [
      'phone'=>'required|regex:"^1\d{10}"',
      'email' => 'required|email',
      'password'=>'required|confirmed'
    ];
  }

  public function messages()
  {
    return [
      'phone.required'=>'手機(jī)號(hào)不能為空',
      'phone.regex'=>'請(qǐng)輸入正確的手機(jī)號(hào)',
    ];

  }

}

當(dāng)我們?cè)诳刂破髦姓{(diào)用RegisterForm的時(shí)候,就回返回Json格式的錯(cuò)誤信息。

不分是否是AJAX

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

兰州市| 曲靖市| 固镇县| 龙口市| 平顺县| 扶风县| 芜湖市| 河源市| 原阳县| 乐都县| 仙居县| 松潘县| 格尔木市| 舞钢市| 晴隆县| 读书| 临清市| 叙永县| 普定县| 奎屯市| 温泉县| 从江县| 汪清县| 杨浦区| 河津市| 山西省| 商南县| 庆安县| 齐齐哈尔市| 黑水县| 大庆市| 彰化县| 龙州县| 开封县| 隆林| 水城县| 巩义市| 五寨县| 南靖县| 任丘市| 社旗县|