<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class SearchContactRequest extends Request {
/**
* 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 [
'country' => '',
'age' => 'integer',
];
}
}
I want give some messages when user input wrong data, but how can I get those messages from session?
Hi, phildawson,
I was wondering how can i get or create the object of $errors and manually add new error message from other page, so when that page detects any error and redirect to the $errors display page, I can still reuse that page.
cheers,
like I have pageA has form and displays form validation errors list. So I was thinking if I have another pageB which has been involved with some logical deciding, I would like define one result as error and if I redirect pageB to pageA, I can reuse the errors list displaying part.
I am not sure it is a good idea or not.
but I guess I would like to know, how can I intervene the $errors object like add new message into it, dynamically.
@Usual If you are talking about flashing custom error messages to the session so that they are available on the next request then it's done the easiest with withErrors.
If you were dealing with the errors messagebag you could merge or add. Here it's injecting a new instance and adding a key/value to achieve the same as using an array above.
use Illuminate\Support\MessageBag;
Route::get('foo', function(MessageBag $errors){
$errors->add('baz', 'FAIL!');
return redirect('bar')->withErrors($errors);
});
@phildawson Yep, I got it. Now I can display my error messages in the web page under your generous help.Thanks a lot.
And once again, I read the official document carefully but found nothing about messages() function and the variable $errors.Is there some more detailed document to look up?
As for the messages() method I've never found it in docs, probably deserves a pull request. If you are ever curious just follow the class it's extending back and have a look at the methods.
/**
* Set custom messages for validator errors.
*
* @return array
*/
public function messages()
{
return [];
}
After the updation of laravel documentation, you don't need to override the response() anymore, all you have to do is, just write your bussiness logics inside the protected failedValidation() inside your custom FormRequest class like follows,
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Contracts\Validation\Validator;
/**
* [failedValidation [Overriding the event validator for custom error response]]
* @param Validator $validator [description]
* @return [object][object of various validation errors]
*/
public function failedValidation(Validator $validator) {
//write your bussiness logic here otherwise it will give same old JSON response
throw new HttpResponseException(response()->json($validator->errors(), 422));
}