CodeIgniter Laravel PHP Example HTML Javascript jQuery MORE Videos New

How to Implement application with Laravel Validation


How to Implement application with Laravel Validation actually it plays a vital role to implement application with correct data.

we use the following predefined Class which come with laravel package
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

example

$rules = [
'id' => 'sometimes|required|integer|exists:apilists,id',
'label_name' => [
'required',
'string',
'min:3',
'max:255',
Rule::unique('variables')->ignore($id)
],
'name' => 'required|string|min:3',
'baseurl' => 'required|url',
'endpoint' => 'required|string',
'methodtype' =>['required',
Rule::in(['GET', 'POST','PATCH','PUT','DELETE']),
],
'field_type' => 'required|integer,
'options' => [
'array',
Rule::requiredIf($request->input('field_type') == 3 || $request->input('field_type') == 4 || $request->input('field_type') == 5 )
],
'params'=> 'nullable|regex:/^[a-zA-Z0-9\/_]+$/',
'bodydata' => 'nullable',
'servicetype' =>'required|integer|exists:App\ServiceType,id',
'variablefor' => 'required|array',
'amount' => 'required|numeric|gt:0',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'certificate'=>'required|file|mimes:pdf,doc,docx|max:2048',
];

example

public function update(Request $request, $id)
{
$rules = [
'id'=> [Rule::exists('variables')->where(function ($query) {
$query->where('id', $id);
}),
]
];
$validator = Validator::make($request->all(),$rules);
if ($validator->fails()) {
return redirect('/setting/variable/edit/'.$id)
->withInput()
->withErrors($validator);
// if we have to send response(ajax call)
$contents = array("message"=>$validator->errors());
$status=400;
$response = response($contents, $status);
return $response;
//
}
else{
...
}

}