Skip to content

Instantly share code, notes, and snippets.

@elena-kolevska
Last active June 24, 2021 14:44
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save elena-kolevska/8580401 to your computer and use it in GitHub Desktop.
Save elena-kolevska/8580401 to your computer and use it in GitHub Desktop.
Custom alphabetic validator that allows spaces
<?php
/* app/validators.php */
Validator::extend('alpha_spaces', function($attribute, $value)
{
return preg_match('/^[\pL\s]+$/u', $value);
});
/*
* add the validators.php file in start/global.php: require app_path().'/validators.php'
* and use it as usual: $rules = array('name' => 'required|alpha_spaces',);
*/
@jrean
Copy link

jrean commented Apr 16, 2014

Thank for sharing this chunk! I used the regex.

You can also achieve it by using the Service provider ; You can create a folder validator/ within the app/ folder.
Then you create CustomValidator.php and a CustomValidatorServiceProvider.php files.

In the CustomValidator.php add the following:

<?php namespace Validators;

use Illuminate\Validation\Validator;

class CustomValidator
extends Validator
{
    public function validateAlphaSpaces($attribute, $value, $params)
    {
        return preg_match('/^[\pL\s]+$/u', $value);
    }
}

In the CustomValidatorServiceProvider add the following:

<?php namespace Validators;

use Illuminate\Support\ServiceProvider;
use Validators\CustomValidator;

class CustomValidatorServiceProvider
extends ServiceProvider
{
    public function register(){}

    public function boot()
    {
        $this->app->validator->resolver(function($translator, $data, $rules, $messages)
        {
            return new CustomValidator($translator, $data, $rules, $messages);
        });
    }

}

Don't forget to set your error message and to update/autoload files with composer.

Regards,

@saul-lopez
Copy link

I'm having trouble getting the Service Provider suggested by @jrean to work .

Where do I have to register the files in composer?

Regards.

@abdullahbutt
Copy link

here is how I did "laravel validator for alphabetic numeric characters and dash and underscore" based on "Elena's" gist
But I am not sure how to match unicode characters, so poor João Gabriel won't have his name marked as invalid anymore

//laravel validator for alphabetic numeric characters and dash and underscore
//This is how I do it:

/*

  • app/validators.php
    */

Validator::extend('alpha_num_dash', function($attribute, $value)
{
return preg_match('/^[a-zA-Z0-9-_]+$/', $value);
});

/*
Define your custom validation message in lang/xx/validation.php:
*/

*
Custom Validation Rules
--------------------------------------------------------------------------

|
| Custom rules created in app/validators.php
|
*/
"alpha_num_dash" => "The :attribute may only contain letters, numeric and dash",

/*
Use it as usual:
*/

$rules = array(
'name' => 'required|alpha_num_dash',
);
And don't forget to require the validators.php file in start/global.php somewhere at the end:

require app_path().'/validators.php';

@waleedrehmankhan
Copy link

Well this didn't worked for me.
So i did this way.

Just Paste this method in Validator.php

public function validateAlphaSpaces($attribute, $value, $params)
{
return preg_match('/^[\pL\s]+$/u', $value);
}

And your message in Validation.php

/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
|--------------------------------------------------------------------------
|
| The following language lines are used to swap attribute place-holders
| with something more reader friendly such as E-Mail Address instead
| of "email". This simply helps us make messages a little cleaner.
|
*/
"alpha_spaces" => "The :attribute may only contain letters and spaces.",

And Its all done :).

Now call it as usual

public function rules()
{
return [
'applicantName' => 'required|alpha_spaces',
];
}

@insidert
Copy link

According to 5.2 docs, do this way.

  1. Make provider via Artican CLI
    " php artisan make:provider YourServiceProvider "
  2. Register it in config/app.php file to providers array,
    " ........
    App\Providers\RouteServiceProvider::class,
    App\Providers\YourServiceProvider::class, "
  3. Edit the code in App\YourServiceProvider.php
    <?php namespace App\Providers; use Validator; use Illuminate\Support\ServiceProvider; class YourServiceProvider extends ServiceProvider { public function boot() { Validator::extend('alpha_spaces', function($attribute, $value) { return preg_match('/^[\pL\s]+$/u', $value); }); } public function register() { // } }

4.Add custom message in language file : resources\lang\en\validaton.php
'alpha_num' => 'The :attribute may only contain letters and numbers.', 'alpha_spaces' => 'The :attribute may only contain letters and spaces.',

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment