After reading the docs, I've understood the strucutre in resources/lang/xx/validation.php to add custom error messages.
'custom' => [
'name' => [
'required' => 'Please fill the name of the company'
]
],
My problem here is that after setting this up, I won't be able to use a field called name to another resource, for instance a Please fill out your name message. When overriding the method messages() inside the FormRequest, I'm able to be more specific in these messages, but then I lose the Language customization.
My question is: How can I go about setting up custom messages for different Form Requests and still keep the language system working in my favor?
Edit
To better explain what I want, here is a little bit of code.
class CompanyRequest extends Request {
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules() {
return [
'name' => 'required|max:20'
];
}
public function messages() {
// Here I have specific message for Company name field, but I'm
// stuck with one single language.
return [
'name.required' => 'Can you please fill the company name.',
'name.max' => 'The name of the company cannot be bigger than 20 characters'
];
}
}
Now the UserRequest
class UserRequest extends Request {
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules() {
return [
'name' => 'required|max:40',
'email' => 'required|email'
];
}
public function messages() {
// Here I have specific message for User name field, but I'm stuck
// with one single language.
return [
'name.required' => 'Please provide your full name',
'name.max' => 'If your name is bigger than 40 characters, consider using only first and last name.'
];
}
}
Now, this works perfectly, but I'm stuck with English. I can solve that with the resources/lang/en/validation.php file. Now, I'll erase the messages method and use the validation file instead.
'custom' => [
// Here I can have multiple validation files for different languages,
// but how would I go about to define the field name for other entities
// (such as the user message).
'name' => [
'required' => 'Please fill the name of the company',
'max' => 'The field cannot have more than 20 characters',
]
]
Now, how can I differ the messages for the field name for Company entity and the field name for User entity?
To wrap it up:
The Form Request provides me a way of overriding a messages() method that allow me to use the term name for multiple forms.
The Validation file provides me a way of using multiple languages (I can create a resources/lang/fr/validation.php, but I can only define the field "name" once. I cannot have different messages for a field name.
Related
I am working with the laravel validations in form request and this time I need to validate a data array something like this:
public function rules()
{
$rules = [
'products.*.quantity' => 'required|integer',
'products.*.dimension' => 'required|min:5|max:16',
'products.*.description' => 'required|min:5',
];
return $rules;
}
where products is the array where I have each of the items, this works however it gives me a message more or less like this: The products.1.quantity field is required.
I need to change the name of the attribute, I know it is possible to change the name inside the messages method giving a new value to products.*. quantity for example products.*. quantity => "quantity", however I would also like to specify the key of the item that is failing and at the end have a message like this:
The quantity in item 1 field is required.
then is it possible to achieve this?
Search for this file resources/lang/xx/validation.php and modify custom entry with your custom messages
'custom' => [
'products.*.quantity' => [
'required' => 'Your custom message',
]
]
I have a small question concerning validation.
there is an api route POST /api/document/{document}/link it accepts an array of document IDs ({"ids": [1, 2, 3]}) to be linked to the Document bound to the route. I validate this array as follows
public function rules()
{
return [
'ids' => 'required|array',
'ids.*' => 'numeric|exists:documents,id'
];
}
The thing is the Document model has a partner attribute and it's not possible to link together documents from different partners. What I want is to check if the documents passed (by their IDs) belong to the same partner as the bound Document. I would like to validate this within the FormRequest. Is it possible?
You can use these for your rules:
'ids' => [
'required',
'array'
],
'ids.*' => [
'required',
'exists:documents,id'
],
'ids.*.partner_id' => [
Rule::in([$document->partner_id])
]
this wil validate your id matches with the numbers in the array, since we only put the id from the route given $document in there it should match or return failed.
So, here is what I ended up with:
public function rules()
{
/** #var Document $document */
$document = $this->route('document');
return [
'ids' => ['required', 'array'],
'ids.*' => ['required', 'numeric', Rule::exists('documents','id')->where('partner_id', $document->partner_id)],
];
}
As it turned out the case is described in Laravel docs here https://laravel.com/docs/5.8/validation#rule-exists. I just needed to customize the query executed to ensure that both the passed id and partner_id exist.
I can do something like this to validate something on the controller.
$this->validate($request,[
'myinput'=>'regex:some pattern'
]);
and the output of this would be something like this
The myinput format is invalid.
What i want was to show something of my own message
Only some pattern allowed
How do i achieve this on laravel?
There are many techniques to customize validator messages.
Validate inside the controller
It would be looked like this
$validate = Validator::make($request->all(), [
'name'=>'required|max:120',
'email'=>'required|email|unique:users,email,'.$id,
'password'=>'nullable|min:6|confirmed'
],
[
'name.required' => 'User name must not be empty!',
'name.max' => 'The maximun length of The User name must not exceed :max',
'name.regex' => 'Use name can not contain space',
'email.required' => 'Email must not be empty!',
'email.email' => 'Incorrect email address!',
'email.unique' => 'The email has already been used',
'password.min' => 'Password must contain at least 6 characters',
'password.confirmed' => 'Failed to confirm password'
]);
The first param is inputs to validate
The second array is validator rules
The last params is the customized validator messages
In which, the synctax is [input_variable_name].[validator_name] => "Customized message"
Second apprach: using InfyOm Laravel Generator
I like this approach the most. It provides useful tools for generating such as Controller, Models, Views, API etc.
And yet, create and update Request file. in which the Request file is using Illuminate\Foundation\Http\FormRequest where this class is extended from Illuminate\Http\Request.
It is mean that we can access Request in this file and perform validating for the incoming requests.
Here is the most interesting part to me.
The generated request file contains rules function, for example like this
public function rules() {
return [
'name' => 'required|unique:flights,name|max:20',
'airline_id' => 'nullable|numeric|digits_between:1,10',
];
}
This function actually returns the validator-rules and validate them against the inputs.
And you can override function messages from Illuminate\Foundation\Http\FormRequest to customize the error message as you need:
public function messages()
{
return [
'required' => "This field is required",
\\... etc
];
}
Nonetheless, you can do many nore with the generated request files, just refer to file in vendor folder vendor/laravel/framework/src/Illuminate/Foundation/Http from your project.
Here is the Infyom github link InfyOmLabs - laravel-generator
You can add custom validation messages to language files, like resources/lang/en/validation.php.
Another way to do that, from docs:
'custom' => [
'email' => [
'regex' => 'Please use your company email address to register. Webmail services are not permitted.'
],
'lawyer_legal_fields' => [
'number_of_areas' => 'You\'re not allowed to select so many practice areas'
],
],
You may customize the error messages used by the form request by overriding the messages method.
public function messages()
{
return [
'title.required' => 'A title is required',
'body.required' => 'A message is required',
];
}
https://laravel.com/docs/5.3/validation#customizing-the-error-messages
I have created a a registration form where a farmer will input his name. The name may contain hyphen or white spaces. The validation rules are written in the app/http/requests/farmerRequest.php file:
public function rules()
{
return [
'name' => 'required|alpha',
'email' => 'email|unique:users,email',
'password' => 'required',
'phone' => 'required|numeric',
'address' => 'required|min:5',
];
}
But the problem is the name field is not allowing any white spaces because of the alpha rule. The name field is varchar(255) collation utf8_unicode_ci.
What should I do, so that user can input his name with white spaces?
You can use a Regular Expression Rule that only allows letters, hyphens and spaces explicitly:
public function rules()
{
return [
'name' => 'required|regex:/^[\pL\s\-]+$/u',
'email' => 'email|unique:users,email',
'password' => 'required',
'phone' => 'required|numeric',
'address' => 'required|min:5',
];
}
You can create a custom validation rule for this since this is a pretty common rule that you might want to use on other part of your app (or maybe on your next project).
on your app/Providers/AppServiceProvider.php
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//Add this custom validation rule.
Validator::extend('alpha_spaces', function ($attribute, $value) {
// This will only accept alpha and spaces.
// If you want to accept hyphens use: /^[\pL\s-]+$/u.
return preg_match('/^[\pL\s]+$/u', $value);
});
}
Define your custom validation message in resources/lang/en/validation.php
return [
/*
|--------------------------------------------------------------------------
| Validation Language Lines
|--------------------------------------------------------------------------
|
| The following language lines contain the default error messages used by
| the validator class. Some of these rules have multiple versions such
| as the size rules. Feel free to tweak each of these messages here.
|
*/
// Custom Validation message.
'alpha_spaces' => 'The :attribute may only contain letters and spaces.',
'accepted' => 'The :attribute must be accepted.',
....
and use it as usual
public function rules()
{
return [
'name' => 'required|alpha_spaces',
'email' => 'email|unique:users,email',
'password' => 'required',
'phone' => 'required|numeric',
'address' => 'required|min:5',
];
}
You can use This Regular Expression to validate your input request. But, you should carefully to write RegEx rules to implement.
Here, you can use this Regex to validate only alphabet & space are allowed.
public function rules()
{
return [
'name' => ['required', 'regex:/^[a-zA-Z\s]*$/']
];
}
I know, this answer may little bit changes with others. But, here is why I make some changes :
Using array in rules. As mention in Laravel Docs, you should better using array as rules when using Regex.
Using specified Regex for validate input request. Of course, you can selected answer above, but I recently found some bug with it. It allow Empty Characters to pass validation. I know, this might little bit paranoia, but if I found the better answer, why not using it?.
Don't get me wrong. I know, other answer is good. But I think it's better to validate everything as specific as we needed, so we can secure our app.
These are my rules in my class:
class AppointmentsController extends Controller
{
protected $rules = [
'appointment' => ['required', 'min:5'],
'slug' => ['required', 'unique:appointments'],
'description' => ['required'],
'date' => ['required', 'date_format:"Y-m-d H:i"'],
];
This is in the laravel official docs:
Sometimes, you may wish to ignore a given ID during the unique check.
For example, consider an "update profile" screen that includes the
user's name, e-mail address, and location. Of course, you will want to
verify that the e-mail address is unique. However, if the user only
changes the name field and not the e-mail field, you do not want a
validation error to be thrown because the user is already the owner of
the e-mail address. You only want to throw a validation error if the
user provides an e-mail address that is already used by a different
user. To tell the unique rule to ignore the user's ID, you may pass
the ID as the third parameter:
'email' => 'unique:users,email_address,'.$user->id.',user_id'
I tried using this in my rules:
'slug' => ['required', 'unique:appointments,id,:id'],
This indeed ignores the current row BUT it ignores it completely. What I want to accomplish is, I want it to ignore the current row only if the slug is unchanged. When it is changed to something that is already unique in another row, I want it to throw an error.
The Unique validator works like that
unique:table,column,except,idColumn
So in your case, you can do it like that:
Get the id you want to validate against, you can get it from the route or with any other way that works for you; something like that
$id = $this->route('id');
'slug' => ['required','unique:appointments,slug,'.$id],
For example we need to update contact info into Users table.
In my model User I created this static method:
static function getContactDataValidationRules( $idUserToExcept ) {
return [
'email' => 'required|email|max:255|unique:users,email,' . $idUserToExcept,
'pec' => 'required|email|max:255',
'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:8|max:20',
'mobile' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:8|max:20',
'phone2' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:8|max:20',
'recovery_email' => 'required|email|max:255',
];
}
and in my UsersController, into the method that update User I've:
$id = $request->input('id');
$request->validate(User::getContactDataValidationRules( $id ));
:-)