Request Validation

Doc:

https://laravel.com/docs/5.4/validation#creating-form-requests

Command:

php artisan make:request StoreBlogPost

Example:

namespace App\Http\Requests;
 
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
 
class NewMemberReq 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 [
            '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', 
            //Not required if pay_method == 1, nullable is must for numeric and digits
            'cc_4' => 'required_unless:pay_method,1|numeric|digits:4|nullable', 
	    'charity_others' => 'required_if:charity,4',
	    'receipt_type' => 'required', 
            //Required only if receipt_type != 4
            'id_number' => 'required_unless:receipt_type,4', 
        ];
    }
 
    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' => '產品分類為數字',
            'cc_4.required_unless' => '請輸入帳號/信用卡末4碼',
            'cc_4.numeric' => '帳號/信用卡末4碼必須是數字',
            'cc_4.digits' => '請輸入帳號/信用卡末4碼',
            'charity_others.required_if' => '請輸入受捐贈單位之愛心碼',
            'id_number.required_unless' => '請輸入身份證',
        ];
    }
}
public function create_sku(Request $req){
	$rules = (new ProductReq)->rules();
	//logg($rules);

	$data = $req->all();

	$validator = Validator::make($req->all(), $rules);
	if($validator->fails() == true){
		return $this->fail_response('Please validate your input values', 0, 201, $validator->errors());
	}

	return $this->success_response($data);
}