Laravel 5 & validating multiple email addresses in a single field - php

I know in Laravel you can setup validation rules for input fields, for example:
$return = [
'first_name' => 'required|max:300|min:3',
'last_name' => 'required|max:300|min:3',
'email' => 'required|email,
];
Is there a way to easily set this kind of validation for a single field with comma separated email addresses (test#email.com, wibble#test.com) or do I need to manually validate these by splitting them apart and checking each email address individually?

This may be what you are looking for:
$rules = array(
'email_addresses' => 'required',
'email_addresses.*' => 'email'
);
$messages = array(
'email_addresses.required' => 'Email Addresses Required',
'email_addresses.email' => ''
);

Related

Laravel Validation rules: required_without

I have two fields: Email and Telephone
i want to create a validation where one of two fields are required and if one or both fields are set, it should be the correct Format.
I tried this, but it doesnt work, i need both though
public static array $createValidationRules = [
'email' => 'required_without:telephone|email:rfc',
'telephone' => 'required_without:email|numeric|regex:/^\d{5,15}$/',
];
It is correct that both fields produce the required_without error message if both are empty. This error message clearly says that the field must be filled if the other is not. You may change the message if needed:
$messages = [
'email.required_without' => 'foo',
'telephone.required_without' => 'bar',
];
However, you must add the nullable rule, so the format rules don't apply when the field is empty:
$rules = [
'email' => ['required_without:telephone', 'nullable', 'email:rfc'],
'telephone' => ['required_without:email', 'nullable', 'numeric', 'regex:/^\d{5,15}$/'],
];
Furthermore: It is recommended writing the rules as array, especially when using regex.

Laravel Request

I am trying to validate some form inputs using laravel request. The name may contain hyphen or dots (for example Mr. Example-of-name). The validation rule alpha cannot take any hyphen or dots. how to do this validation then? and also for the address, user may use comma between Road No, Sector No. etc. For phone number if I use exactly:11. it cannot take phone number with 11 digits. I get error message that the phone should be 11.
public function rules()
{
return [
'name' => 'required|max:60',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:4|confirmed',
'phone' => 'required|numeric',
'address' => 'required|min:5',
'profession' => 'required|min:3',
'conditions' => 'required'
];
}
Use Laravel's build-in regular expressions (regex) validation rule for this:
Regular Expressions for Requests

Laravel Validation of email and regex not working

Problem
I am trying to validate a form using the Laravel built in validation. I want to make sure that the email only has a .edu in it. However, there Laravel continues to throw a preg_match(): No ending delimiter '/' found error. I heard this has something to do with a pipe delimiter instead of an array one, but I am unsure what this mean / how to fix it. The code I have is below.
$validator = Validator::make(Input::all(),
array(
'email' => 'Required|Max:50|Email|Unique:users|Regex:/(\.edu(\.[a-z]+)?|\.ac\.[a-z]+)$/',
'first-name' => 'required|max:20|min:3|',
'last-name' => 'required|max:30|min:3|',
'username' => 'required|max:30|min:3|unique:users|',
'city' => '',
'state' => '',
'password-init' => 'required|min:6|AlphaNum',
'password-check', 'required|min:6|AlphaNum|same:password-init'
)
);
Any help would be appreciated
Thanks!
Since you're using pipes | to separate your different validation rules, Laravel gets confused when it sees a pipe in the middle of your Regex. So break up just the "email" rule like this:
$validator = Validator::make(Input::all(),
array(
'email' => array(
'required',
'max:50',
'email',
'unique:users',
'regex:/(\.edu(\.[a-z]+)?|\.ac\.[a-z]+)$/'
),
'first-name' => 'required|max:20|min:3|',
...
That's what it means when it talks about an array instead of pipes in the doc:
http://laravel.com/docs/4.2/validation#rule-regex

Laravel Validation one of two fields must be filled

I have two fields:
QQ
Email
How do I set up a Validator object so that one of these fields must be filled? It doesn't matter which.
$messages = array(
'email.required_without:qq' => Lang::get('messages.mustenteremail'),
'email.email' => Lang::get('messages.emailinvalid'),
'qq.required_without:email' => Lang::get('messages.mustenterqq'),
);
required_without should work.
It means that the field is required if the other field is not present. If have more than two fields and only one is required, use required_without_all:foo,bar,...
$rules = array(
'Email' => 'required_without:QQ',
'QQ' => 'required_without:Email',
);
In the example above (given by lucasgeiter) you actually only need one of the conditions not both.
$rules = array(
'Email' => 'required_without:QQ'
);
If you have both rules then filling neither in will result in TWO error messages being displayed. This way it will check that at least one field is filled in and will only display one error message if neither are filled.
Laravel >= 8.32 only support.
Both (mobile or email) are presented simultaneously -> throw an error.
Both (mobile or email) is not present simultaneously -> throw an error.
Allowed
Only one parameter can be allowed.
'email' => [ 'prohibited_unless:mobile,null,','required_without:mobile','email', 'max:255', ],
'mobile' => [ 'prohibited_unless:email,null','required_without:email', 'digits_between:5,13', 'numeric' ],
EDIT: You can also use prohibits:email and prohibits:mobile in combination of required_without:... rule

Checking for the Unique Column except the Own Column

Here is my Rule :
Table Name is : company_info
I have only two fields CompanyID and Telephone
In the update section, i want to check whether the Telephone Number exists for other columns and if the own field have it i don't want to check it. (Currently it checks the own data and returning with Telephone number was taken already).
'Telephone' => 'unique:company_info',
Then i tried with the below rule
But i miss in the
'Telephone' => 'unique|unique:company_info,CompanyID,'.$companyid)
or
'Telephone' => 'unique|unique:company_info,Telephone,'.$companyid)
or
'Telephone' => 'unique|unique:company_info,Telephone,'.$Telephone)
Here is my Code :
$companyid = Input::get('CompanyID');
$Telephone = Input::get('Telephone');
$rule = array(
'Telephone' => 'unique|unique:company_info,CompanyID,'.$companyid
)
$validator = Validator::make($data,$rule);
if ($validator->fails())
{
$messages = $validator->messages();
return "0"."||".$messages = $validator->messages()->first('Telephone');
}
While the update query i need to check for the unique rule except the given id
I refered this one http://laravel.com/docs/4.2/validation#rule-unique
But i am not getting return on $validator = Validator::make($data,$rule);
How can i check for the unique value except the own column
I believe you have the wrong syntax for unique validation
it should be
'Telephone' => 'unique:company_info,CompanyID,'.$companyid
or
'Telephone' => 'required|unique:company_info,CompanyID,'.$companyid
and not
'Telephone' => 'unique|unique:company_info,CompanyID,'.$companyid
Can try this as the Laravel Validation provides us various features
$companyid = Input::get('CompanyID');
$Telephone = Input::get('Telephone');
$data = array('companyid'=>$companyid, 'Telephone'=>$Telephone );
//FOR INSERTING NEW DATA
$rule = array(
'Telephone' => 'required|unique:company_info,Telephone,{:id}'
);
$validator = Validator::make($data,$rule);
//FOR UPDATING AN EXISTING DATA
public static function rule ($id, $merge=[]) {
return array_merge(
[
'Telephone' => 'required|unique:company_info,Telephone,'.$id,
],
$merge);
}
$validator = Validator::make($data,self::rule($id));
Comment for errors...
Try following code
'Telephone' => 'unique:company_info,Telephone,'.$companyid.', CompanyID';
{rule} =>
'unique:{table_name},{unique_column_name},{except_column_value},{except_column_name}'

Categories