How can I use fails method validation in controller - laravel

controller validator in laravel :
$validationController = $this->validate(request(), [
'title' => 'min:100',
'text' => 'required',
'image' => 'required',
]);
and we can make validator with :
$validationNormaly = Validator::make(request(), [
'title' => 'min:100',
'text' => 'required',
'image' => 'required',
]);
and i can't use $validationController->fails(). how can i use it?

The first validation that you mentioned will fail automatically as designed by the laravel code base.
The Validator::make() i typically use when doing ajax requests and such to the controller method. You will need to run the validation fails method to invoke the failed response like so:
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}

You Can Use :
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
])->validate();
take advantage of the automatic redirection with error messages

Related

custom validation message in laravel 8

how to give custom laravel message in my coding i am new in laravel
$validatedData = $r->validate([
'preference' => 'required',
'objective_1' => 'required',
'objective_2' => 'required',
'objective_5' => 'required',
]);
if (empty($r->session()->get('sessForm'))) {
$r->session()->put('sessForm', $validatedData);
} else {
$sessForm = $r->session()->get('sessForm');
$r->session()->put('sessForm', $sessForm);
}
You can use custom validator. Check the documentation. You can get messages from $errors variable in blade template.
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
], [
'title.required' => 'Title required custom message',
'title.unique' => 'Title unique custom message',
// ...
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
If you want to send redirect with data. You can use
redirect()->route('route')->with('data', 'test');
You can get after redirect
session('data')

Laravel make custom error validation messages

Greeting, this is my code and I need to make custom error messages for every rule
$validator = Validator::make($request->all(), [
'name' => 'required|min:3|max:100',
'phone' => 'required',
'date' => 'required',
'address' => 'required|min:3|max:100',
'test' => 'required|min:3|max:100',
]);
if ($validator->fails()) {
$errors = $validator->errors();
return response()->json($errors);
}
Its better to create a separate request for validation purpose
public function rules(): array
{
return [
'name' => 'required|min:3|max:100',
'phone' => 'required',
'date' => 'required',
'address' => 'required|min:3|max:100',
'test' => 'required|min:3|max:100',
]
}
public function messages(): array
{
return [
'name' => 'Please enter name'
];
}
you can create your own custom validation messages in two ways:
1- in resources/lang/en/validation.php you can change the validation message for every rule
2- you can pass your custom message for each validation like this:
$validator = Validator::make($input, $rules, $messages = [
'required' => 'The :attribute field is required.',
]);
you can check here for more information
specific to your question:
$messages = [
'required' => 'The :attribute field is required.',
'min' => ':attribute must be more than 3 chars, less than 100'
]
$validator = Validator::make($request->all(), [
'name' => 'required|min:3|max:100',
'phone' => 'required',
'date' => 'required',
'address' => 'required|min:3|max:100',
'test' => 'required|min:3|max:100',
], $messages);

Laravel 8: How to send validation response in JSON format using postman

I have this validation, how can I return an error response in postman?
please help guys.
$rules = [
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6|confirmed',
];
$validator = $this->validate($request, $rules);
$data = $request->all();
use Illuminate\Support\Facades\Validator;
Then write validation like
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6|confirmed',
]);
if ($validator->fails()) {
return response()->json([
'message' => 'Invalid params passed', // the ,message you want to show
'errors' => $validator->errors()
], 422);
}

Is there anyway group the errors of multiple form validations in laravel

In the given scenario
request()->validate([
'type' => 'required',
'category' => 'required'
]);
and Again
request()->validate([
'name' => 'required',
'gender' => 'required
]);
Is it possible to get some sort of centralized or complied error that encompasses both the validations?
Then you should use Validator facade to handle this kind of cases.
for ex.
$validator = Validator::make($request->only('type', 'category), [
'type' => 'required',
'category' => 'required'
]);
$validator2 = Validator::make($request->only('name', 'gender'), [
'name' => 'required',
'gender' => 'required'
]);
if ($validator->fails() || $validator2->fails()) {
// return merge $validator->errors() and $validator2->errors();
}

return validation errors from laravel controller to vue

everybody, I need to return error validation for this validation code to vue
Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
])->validate();
I have tried
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
])->validate();
if ($validator->fails()) {
return $this->sendError('error validation', $validator->errors());
}
but did't work for me
List errors
$validator->messages();

Resources