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
Related
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.
Is there a way to set a validation on multiple inputs with similar name? For ex -
public function rules()
{
return [
'zone1' => 'required|numeric',
'zone2' => 'required|numeric',
'zone3' => 'required|numeric',
];
}
Can I do something like 'zone*' => 'required|numeric'
You can use an asterisk as a wildcard but it may not be a great idea. With a rule like 'zone*' => 'required|numeric' as long as there's a single value that matches the condition the request will pass validation. For example, if a request has a valid value for zone2 but zone1 and zone3 are missing or non-numeric the validation will still pass
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' => ''
);
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.
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