php Laravel 5.1 验证规则 alpha 不能使用空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34099777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:45:08  来源:igfitidea点击:

Laravel 5.1 validation rule alpha cannot take whitespace

phpformsvalidationlaravellaravel-5.1

提问by Noob Coder

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.phpfile:

我创建了一个登记表,农民将在其中输入他的名字。该名称可能包含连字符或空格。验证规则写在app/http/requests/farmerRequest.php文件中:

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 namefield is not allowing any white spaces because of the alpharule. The namefield is varchar(255) collation utf8_unicode_ci.

但问题是name由于alpha规则,该字段不允许有任何空格。该name字段是varchar(255) collation utf8_unicode_ci

What should I do, so that user can input his name with white spaces?

我该怎么做,以便用户可以用空格输入他的名字?

回答by Bogdan

You can use a Regular Expression Rulethat 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',
    ];
}

回答by Chris Landeza

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

在您的应用程序/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

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

回答by Fendi Setiawan

You can use This Regular Expressionto validate your input request. But, you should carefully to write RegEx rules to implement.

您可以使用此正则表达式来验证您的输入请求。但是,您应该仔细编写 RegEx 规则来实现。

Here, you can use this Regex to validate only alphabet & space are allowed.

在这里,您可以使用此 Regex 来验证仅允许使用字母和空格。

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 Charactersto pass validation. I know, this might little bit paranoia, but if I found the better answer, why not using it?.
  • 在规则中使用数组。正如Laravel Docs 中提到的,在使用 Regex 时,您应该更好地使用数组作为规则。
  • 使用指定的正则表达式来验证输入请求。当然,你可以选择上面的答案,但我最近发现了一些错误。它允许空字符通过验证。我知道,这可能有点偏执,但如果我找到了更好的答案,为什么不使用它呢?

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.

不要误会我的意思。我知道,其他答案很好。但我认为最好根据我们的需要验证所有内容,这样我们才能保护我们的应用程序。