Validation : Laravel - laravel

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

Related

Laravel one validation function for Store and Update

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;
}

Laravel : Need to dynamic validation rules (read from config file or setting)

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"
]

Laravel 5.4 Validation Request , How to handle unique validation on update?

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

Validate whether multiple fields are unique simultaneously in laravel 5

This my request class rules.
return [
'title' => 'required|unique:event_cals,title',
'eventDate' => 'required|date|after:yesterday',
'venue' => 'required',
'time' => 'required',
'type' => 'required',
'unique:event_cals,eventDate,NULL,id,venue,$request->venue,time,$request->time'
];
I want to validate a rule like below.
'eventDate' && 'venue' && 'time' => 'unique'
There I need to check if there any row without same eventDate, venue and time altogether. Anyone knows how to declare such a rule?
This is the snapshot of db table.
Here is the possible solution:
<?php
return [
'title' => 'required|unique:event_cals,title',
'eventDate' => 'required|date|after:yesterday',
'venue' => 'required',
'time' => 'required',
'type' => 'required',
'event_date' => "unique:event_cals,event_date,NULL,id,venue,{$request->venue},time,{$request->time}",
];
I again want to highlight that; if you want that validator to work, you should make sure that the event_date and time should be correctly formatted.
An example unique check with additional wheres from our running project's update requests:
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
$id = $this->route('money');
return $rules = [
'name' => "required|string|min:3|max:255|unique:moneys,name,{$id},id,deleted_at,NULL,access,public",
];
}

Laravel get translated validation errors

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 :)

Resources