I wrote this code for storing products :
public function rules()
{
return [
'trans_data.*.name' => 'required', //translation data
'trans_data.*.description' => 'required',
'url' => 'required',
'action' => 'required',
];
}
'*' is for lots of languages and we have 5 languages , I wrote this code for error message but I think it needs refactor.
public function messages()
{
return [
'trans_data.en.name.required' => 'English name is required',
'trans_data.nl.name.required' => 'Dutch name is required',
'trans_data.en.description.required' => 'English description is required',
'trans_data.nl.description.required' => 'Dutch description is required'
.
.
.
];
}
I want to summarize these lines, and languages be dynamic like :
:attribute name is required
Is it possible?
Related
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);
In ContactsRequest.php
public function rules()
{
return [
'org_id' => 'required',
'name' => 'required',
'office' => 'required',
'mobile' => 'required_without:home',
'home' => 'required_without:mobile'
];
}
So basically what i want is , i have a form which will be taking the attributes specified in the code. But i want to modify code so that entering either one of 'home' or 'mobile' will allow me to create the new user.
What should be done.Any help would be appreciated
I have a request rule like this :
public function rules()
{
return [
'title' => 'required',
'recipients' => 'required',
'attachments' =>'mimes:jpeg,png,pdf,doc,xls|max:10410',
];
}
So I'm looking for a way to make rules dynamic, with read config file or read from database.
For instance :
I have made a helper function named setting , it can load setting from my DB and i want to read this data and set on my rule like this :
public function rules()
{
$max_upload_size = setting('max_document_upload_size'));
return [
'title' => 'required',
'recipients' => 'required',
'attachments' =>'mimes:jpeg,png,pdf,doc,xls|max:$max_upload_size',
];
}
Is it possible or what should i do for cover this?
Thanks in advance.
please write after max : '.
public function rules()
{
$max_upload_size = setting('max_document_upload_size'));
return [
'title' => 'required',
'recipients' => 'required',
'attachments' =>'mimes:jpeg,png,pdf,doc,xls|max:'.$max_upload_size',
];
}
This question refers to PHP, not Laravel. There are many options to combine a string with a variable.
If you need to have a lot of settings on the string, then you can use this syntax:
return [
'title' => 'required',
'recipients' => 'required',
'attachments' => "mimes:$mimes|max:$max_upload_size"
]
I have a users table which has a unique validate rule on email and username. When i am trying to update not ignore unique validation. Please see my code below.
UserRequest.php
public function rules()
{
return [
'name' => 'required',
'mobile' => 'required',
'email' => 'required|unique:users,email'.$id,
'usercategory' => 'required',
'username' => 'required|unique:users,username'.$id,
];
}
Please try this
public function rules()
{
$id = $this->request->get('id') ? ',' . $this->request->get('id') : '';
return [
'name' => 'required',
'mobile' => 'required',
'email' => 'required|unique:users,email'.$id,
'usercategory' => 'required',
'username' => 'required|unique:users,username'.$id,
];
}
In laravel 5.5 you should do like this:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
Check laravel documentaion about rule-unique.
You needed to skip id if you validate for update, like as below
public function rules($id='')
{
$id = $id ? ','.$id.',id':'';
return [
'name' => 'required',
'mobile' => 'required',
'email' => 'required|unique:users,email'.$id,
'usercategory' => 'required',
'username' => 'required|unique:users,username'.$id,
];
}
In Laravel docs, you have provide 3rd and 4th param in unique rule
unique:table,column,except,idColumn
You could use something like this:
$id = $this->isMethod('put') ? ',' . auth()->id() : '';
assuming you use put method for update
before line with return
for somebody else faced this issue:
public function rules()
{
return [
'name' => 'required',
'mobile' => 'required',
'email' => 'required|unique:users,email'.$this->user->id,
'usercategory' => 'required',
'username' => 'required|unique:users,username'.$this->user->id,
];
}
I fixed this by using this:
public function rules()
{
$id = $this->user->id ?? null;
return [
"name" => "required",
"mobile" => "required",
"email" => "required|unique:users,email, $id",
"usercategory" => "required",
"username" => "required|unique:users,username, $id",
];
}
Note that for other models other than the User model, the user in $this->user->id will be the model name in lowercase
I use Laravel 5.1, I want to return translated validation error in this request class. Please any help how to return translated data.
class ContactRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required',
'g-recaptcha-response' => 'required|captcha',
'email' => 'required|email',
'message' => 'required',
'phone' => 'required'
];
}
}
In the resources/lang/en folder, there is a file named validation.php. Put the translated error messages in the file as described in the laravel documentation.
How your ru/validation.php file should look (but with russian text):
return [
'required' => ':attribute is required',
'email' => 'You need to enter a valid :attribute',
'captcha' => 'This :attribute is invalid'
]
First install this package : laravel langs
(Copy languages folders you wish integrate into resources/lang)
Change locale variable in config/app to 'ru' for example, and that's all :)