Laravel 8 Validation



Example 1 :
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
public function store(Request $request)
{
$validator = Validator::make($request->all(),[
"flight_title" =>'required',
"flight_name" => 'required',
"flight_detail" => 'required',
"flight_no" => 'required',
"flight_remark" => 'required',
]);
dd($request->fails());
dd($validator->validated());
}
public function store(Request $request)
$validator = Validator::make($request->all(),[
"flight_title" =>'required',
"flight_name" => 'required',
"flight_detail" => 'required',
"flight_no" => 'required',
"flight_remark" => 'required',
])->validateWithBag('postStore');
// dd($request->fails());
    // dd($validator->validated());
}
@if ($errors->postStore->any())
{{ dd($errors->postStore->all()) }}
@endif

Example 2: 
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class DeceasedInformationRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        $postAttr = $this->request;
        $rules = [
            "status" =>'required',
            "flight_title" =>'required',
        ];
        if($postAttr->get('status')=='store') {
            return $rules;
        }
        return [];
    }
    public messages() {
        return [
            'status.required' => 'Status is required!',
            'flight_title.required' => 'Flight Title is required!',
        ];
    }
    public function attributes(){
       return [
           'status'             => 'Status (*)',
           'flight_title'     => 'Flight Title(#)',
       ];
   }
}

Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment