Laravel 5.1 validation rule alpha cannot take whitespace
Solution 1
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',
];
}
Solution 2
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',
];
}
Solution 3
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.
Related videos on Youtube
Noob Coder
Updated on January 08, 2020Comments
-
Noob Coder over 4 years
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 thealpha
rule. Thename
field isvarchar(255) collation utf8_unicode_ci
.What should I do, so that user can input his name with white spaces?
-
Yousef Altaf over 7 yearswhere to find this
AppServiceProvider.php
-
tsveti_iko about 7 yearsAnd don't forget to add
use Illuminate\Support\Facades\Validator;
at the top of yourapp/Providers/AppServiceProvider.php
;) -
Maarten Kuijper about 6 yearsYou can also use $this->app in service providers and add a third parameter as error message. Useful when translation isn't needed. $this->app['validator']->extend('my_rule', function ($attributes, $value, $parameters) { return (bool) preg_match(...); }, ':attribute my custom message.');
-
nsg about 5 yearsDid you try using string in your rules ?
'name' => 'required|string',