婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > laravel 表單驗證實現(xiàn)多個字段組合后唯一

laravel 表單驗證實現(xiàn)多個字段組合后唯一

熱門標簽:新河科技智能外呼系統(tǒng)怎么樣 福州人工外呼系統(tǒng)哪家強 百度商鋪地圖標注 常州地圖標注服務商 衡水外呼系統(tǒng)平臺 地圖標注平臺怎么給錢注冊 釘釘打卡地圖標注 注冊400電話申請 安裝電銷外呼系統(tǒng)

Laravel 表單驗證器的幾種使用方法

1、使用控制器的 validate 方法進行參數(shù)驗證

/**
 * 保存一篇新的博客文章。
 *
 * @param Request $request
 * @return Response
 */
public function store(Request $request)
{
  $this->validate($request, [
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
  ]);

  // 文章內容是符合規(guī)則的,存入數(shù)據(jù)庫
}

2、手動創(chuàng)建驗證器實例進行驗證

使用默認的驗證信息

/**
 * 保存一篇新的博客文章。
 *
 * @param Request $request
 * @return Response
 */
public function store(Request $request)
{
  $rules = [
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
  ];
  $validator = Validator::make($request->all(), $rules);
  if ($validator->fails()) {
    return redirect('post/create')->withErrors($validator)->withInput();
  }

  // 文章內容是符合規(guī)則的,存入數(shù)據(jù)庫
}

使用自定義的驗證信息

/**
 * 保存一篇新的博客文章。
 *
 * @param Request $request
 * @return Response
 */
public function store(Request $request)
{
  $rules = [
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
  ];
  $messages = [
    'title.required' => '請?zhí)顚懳恼聵祟}',
    'title.unique' => '文章標題不能重復',
    'title.max' => '文章標題不能超過255個字符',
    'body.required' => '請?zhí)顚懳恼聝热?,
  ];
  $validator = Validator::make($request->all(), $rules, $messages);
  if ($validator->fails()) {
    return redirect('post/create')->withErrors($validator)->withInput();
  }

  // 文章內容是符合規(guī)則的,存入數(shù)據(jù)庫
}

3、創(chuàng)建表單請求進行驗證

創(chuàng)建表單請求文件:php artisan make:request ExampleRequest
表單請求文件內容:

?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;

class ExampleRequest extends FormRequest
{
  /**
   * 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 [
      'title' => 'required|max:20',
      'name' => ['required', new Uppercase()],
    ];
  }

  /**
   * 獲取已定義的驗證規(guī)則的錯誤消息。
   *
   * @return array
   */
  public function messages()
  {
    return [
      'title.required' => 'A title is required',
      'title.max' => 'The title may not be greater than 20 characters.',
    ];
  }

  /**
   * 兼容 form 表單請求與 ajax 請求或者 json api 請求
   * 驗證失敗,返回錯誤信息
   *
   * @param Validator $validator
   * @throws
   */
  protected function failedValidation(Validator $validator)
  {
    if ($this->wantsJson() || $this->ajax()) {
      throw new HttpResponseException(
        new JsonResponse([
          'code' => 500,
          'msg' => $validator->errors()->first(),
          'data' => new \stdClass()
        ])
      );
    } else {
      parent::failedValidation($validator);
    }
  }
}

在控制器中使用 ExampleRequest

?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\ExampleRequest;

class ExampleController extends Controller
{
  public function valid(ExampleRequest $request)
  {
    $params = $request->all();
    dd($params);
  }
}

在laravel 表單驗證中,常會遇到需要幾個字段組合起來做唯一限制。

解決方案如下:

where[] = ['parentId','=',where[]=[′parentId ′,′ = ′,this->request->get('parentId')];
return [

    'menuTitle' => ['required', 'max:32','min:2',Rule::unique('admin_menu','menuTitle')->where(function($query)use($where){
      $query->where($where)->whereNull('deleted_at');
      })->ignore($id) ],
    'menuTitleEn' => ['required', 'max:32','min:2',Rule::unique('admin_menu','menuTitleEn')->where(function($query)use($where){
      $query->where($where)->whereNull('deleted_at');
      })->ignore($id) ],
    'menuRoute' => ['required',Rule::unique('admin_menu','menuRoute')->ignore($id)],
    'menuIcon' => ['required', 'min:2','max:32'],
    'routeName' => ['sometimes', 'min:2','max:32'],
    'parentId' => ['required','numeric'],
    'order'=>['sometimes','numeric']
    
  ];

到此這篇關于laravel 表單驗證實現(xiàn)多個字段組合后唯一的文章就介紹到這了,更多相關laravel 表單驗證內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Laravel 驗證碼認證學習記錄小結
  • laravel 數(shù)據(jù)驗證規(guī)則詳解
  • Laravel實現(xiàn)登錄跳轉功能
  • laravel 解決強制跳轉 https的問題
  • Laravel重定向,a鏈接跳轉,控制器跳轉示例
  • 解決Laravel使用驗證時跳轉到首頁的問題

標簽:唐山 遼陽 克拉瑪依 鶴崗 白城 柳州 六安 鷹潭

巨人網(wǎng)絡通訊聲明:本文標題《laravel 表單驗證實現(xiàn)多個字段組合后唯一》,本文關鍵詞  laravel,表單,驗證,實現(xiàn),多個,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《laravel 表單驗證實現(xiàn)多個字段組合后唯一》相關的同類信息!
  • 本頁收集關于laravel 表單驗證實現(xiàn)多個字段組合后唯一的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 汝城县| 澄迈县| 霍邱县| 虞城县| 宝山区| 泗水县| 曲周县| 独山县| 天峨县| 毕节市| 广平县| 陇南市| 金秀| 新和县| 垦利县| 德兴市| 治多县| 海丰县| 英超| 昌乐县| 水城县| 东乡族自治县| 赣榆县| 施甸县| 西昌市| 巴里| 临武县| 长武县| 宁明县| 逊克县| 和林格尔县| 淮阳县| 翁牛特旗| 通道| 凤冈县| 精河县| 伊宁市| 墨玉县| 淮南市| 淅川县| 兴宁市|