Laravel Form Validator

Command

php artisan make:request StoreBlogPost

Available Rules

https://laravel.com/docs/5.4/validation#available-validation-rules

Example

namespace App\Http\Requests;
 
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;
 
class NewMemberReq extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        if(Auth::guard('store')->check()){
            return true;
        }else{
            return false;
        }
    }
 
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'action' => 'required|' . Rule::in(["new", "update"]), 
            'name' => 'required', 
            'pid' => 'required', 
            'email' => 'required|email|unique:member', 
            'phone' => 'required', 
            'mobile' => 'required|numeric|digits:10', 
            'city' => 'required', 
            'area' => 'required|numeric', 
            'address' => 'required', 
            'bank' => 'required', 
            'branch_id' => 'required', 
            'bank_account' => 'required', 
            'password' => 'required|min:5', 
            'cat' => 'required|numeric', 
            'invoice_company' => 'required_if:invoice_type,==,c'
        ];
    }
 
    public function messages()
    {
        return [
            'action.in' => '動作代碼錯誤', 
            'name.required' => '姓名為必填',
            'pid.required' => '身份證號為必填',
            'email.required' => 'Email為必填',
            'email.email' => 'Email格式不正確',
            'email.unique' => 'Email已經存在',
            'phone.required' => '電話為必填',
            'mobile.required' => '手機號碼為必填',
            'mobile.numeric' => '手機號碼為10位數字',
            'mobile.digits' => '手機號碼為10位數字',
            'city.required' => '縣市為必填',
            'area.required' => '地區為必填',
            'area.numeric' => '地區為數字',
            'address.required' => '地址為必填',
            'bank.required' => '銀行為必填',
            'branch_id.required' => '分行為必填',
            'bank_account.required' => '銀行帳號為必填',
            'password.required' => '密碼為必填',
            'password.min' => '密碼至少要5碼',
            'cat.required' => '產品分類為必填',
            'cat.numeric' => '產品分類為數字',
            'invoice_company.required_if' => '請輸入發票抬頭',
        ];
    }
}