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 :)
Related
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?
I am trying to have one validation function for both store and update. So I don't repeat code. It works well. The problem is testing 'unique'. I worked around with this idea. But feels long-winded. Is there a better way to do it?
I want to check for unique at the store.
At update, unique check ignores own id.
I don't want different validations like I did as the user will be
first notified of the unique error, he will fix it. then something
else might be wrong and he has to fix again.
.
public function validateRequest($request){
if($request->method() == "PUT")
{
$val = $this->validate($request, [
'name' => 'unique:customers,id',
'phone' => 'unique:customers,id',
]);
}
if($request->method() == "POST"){
$val = $this->validate($request, [
'name' => 'unique:customers',
'phone' => 'unique:customers'
]);
}
$validation = $this->validate($request, [
'name' => 'required',
'phone' => 'required|integer|gt:0',
'phone2' => 'nullable|integer|gt:0|',
'email' => 'email|nullable',
'note' => 'nullable',
],
[
'phone.integer' => "Invalid phone format. Use international format. Eg: 971550000000",
'phone2.integer' => "Invalid phone format. Use international format. Eg: 971550000000",
'required' => "Required Field",
]);
return $validation;
}
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 need to do validation in one of my controllers — I can't use a request class for this particular issue — so I'm trying to figure out how to define custom validation messages in the controller. I've looked all over and can't find anything that suggests it's possible. Is it possible? How would I do it?
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
// Can I create custom error messages for each input down here? Like...
$this->validate($errors, [
'title' => 'Please enter a title',
'body' => 'Please enter some text',
]);
}
You should have a request class like below. message overwrite is what you are looking for.
class RegisterRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'UserName' => 'required|min:5|max:50',
'Password' => 'required|confirmed|min:5|max:100',
];
}
public function response(array $errors){
return \Redirect::back()->withErrors($errors)->withInput();
}
//This is what you are looking for
public function messages () {
return [
'FirstName' => 'Only alphabets allowed in First Name',
];
}
}
This did it
$this->validate($request, [
'title' => 'required',
'body' => 'required',
], [
'title.required' => 'Please enter a title',
'body.required' => 'Please enter some text',
]);