Laravel 5.1 validation rule alpha cannot take whitespace

laravel validation rules
laravel validation string length
space not allowed validation in laravel
image validation in laravel
laravel request validation multiple rules
laravel validation regex
laravel unique validation with condition
laravel validate float

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',
    ];
}

Laravel 5.1 validation rule alpha cannot take whitespace, 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� Custom Validation Rules. Using Rule Objects. Laravel provides a variety of helpful validation rules; however, you may wish to specify some of your own. One method of registering custom validation rules is using rule objects. To generate a new rule object, you may use the make:rule Artisan command. Let's use this command to generate a rule that

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',
    ];
}

Validation - Laravel, Master, 7.x, 6.x � 5.8 � 5.7 � 5.6 � 5.5 � 5.4 � 5.3 � 5.2 � 5.1 � 5.0 � 4.2 Available Validation Rules; Conditionally Adding Rules; Validating Arrays; Custom Next, let's take a look at a simple controller that handles these routes. The field under validation may have alpha-numeric characters, as well as dashes and underscores. Introduction. Laravel provides several different approaches to validate your application's incoming data. By default, Laravel's base controller class uses a ValidatesRequests trait which provides a convenient method to validate incoming HTTP request with a variety of powerful validation rules.

custom validation rule laravel Code Example, Master, 7.x � 6.x � 5.8 � 5.7 � 5.6 � 5.5 � 5.4 � 5.3 � 5.2 � 5.1 � 5.0, 4.2 The first argument passed to the make method is the data under validation. The second argument is the validation rules that should be applied to the data. The field under validation may have alpha-numeric characters, as well as dashes and underscores. The field that has "required" rule works fine and validates the white spaces. But with the optional field, it doesn't work. The form will save "spaces" into the database (the field contains white spaces only). I tried regex rule, alpha rule and it still passes the validation. So how to validate white spaces in a proper way?

request validate laravel 5.1 Code Example, Get code examples like "custom validation rule laravel" instantly right from your alpha_num # array. 9 'email.required'=>'You cant leave Email field empty', request validate laravel 5.1 � laravel request validation list cheach if session variable exists php � check if string contains only whitespace php� alpha will not allow underscores for example. – scrubmx Jun 14 '16 at 3:49 alpha_dash can work, it all depends on the characters you want to allow. String like: "(╯° °)╯" will fail alpha_dash validation too. – scrubmx Jun 14 '16 at 3:58

Laravel validator for alphabetic characters and spaces, Get code examples like "request validate laravel 5.1" instantly right from your google search results with the Grepper Chrome Extension. alpha. The field under validation must be entirely alphabetic characters. alpha_dash. The field under validation may have alpha-numeric characters, as well as dashes and underscores. alpha_num. The field under validation must be entirely alpha-numeric characters. array. The field under validation must be a PHP array. before:date

php, Elena Kolevska has a short tip for creating a custom Laravel validator. Laravel doesn't have a built in validator for alphabetic characters and spaces, but it does Custom rules created in app/validators.php | */ "alpha_spaces" => "The : attribute may Get helpful Laravel tutorials, news and freebies straight into your inbox. 32 Blade view not reflecting changes May 28 '16 29 React Native Task :app:validateSigningDebug FAILED Jul 19 '19 22 Laravel 5.1 validation rule alpha cannot take whitespace Dec 5 '15

Comments
  • where to find this AppServiceProvider.php
  • And don't forget to add use Illuminate\Support\Facades\Validator; at the top of your app/Providers/AppServiceProvider.php ;)
  • You 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.');
  • Did you try using string in your rules ? 'name' => 'required|string',