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

Noo*_*der 19 php forms validation laravel laravel-5.1

我创建了一个登记表,农民将输入他的名字.名称可能包含连字符或空格.验证规则写在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',
    ];
}
Run Code Online (Sandbox Code Playgroud)

但问题是name由于alpha规则,该字段不允许任何空格.这个name领域是varchar(255) collation utf8_unicode_ci.

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

Bog*_*dan 39

您可以使用仅明确允许字母,连字符和空格的正则表达式规则:

public function rules()
{
    return [
        'name'     => 'required|regex:/^[\pL\s\-]+$/u',
        'email'    => 'email|unique:users,email',
        'password' => 'required',
        'phone'    => 'required|numeric',
        'address'  => 'required|min:5',
    ];
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*eza 33

您可以为此创建自定义验证规则,因为这是一个非常常见的规则,您可能希望在应用程序的其他部分(或者可能在您的下一个项目中)使用该规则.

在您的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); 

    });

}
Run Code Online (Sandbox Code Playgroud)

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.',
 ....
Run Code Online (Sandbox Code Playgroud)

像往常一样使用它

public function rules()
{
    return [
        'name'     => 'required|alpha_spaces',
        'email'    => 'email|unique:users,email',
        'password' => 'required',
        'phone'    => 'required|numeric',
        'address'  => 'required|min:5',
    ];
}
Run Code Online (Sandbox Code Playgroud)

  • 并且不要忘了在app / Providers / AppServiceProvider.php顶部添加use use Illuminate \ Support \ Facades \ Validator;;) (3认同)
  • 您是否尝试在规则中使用字符串?`'name' => 'required|string',` (2认同)

Fen*_*wan 5

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

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

public function rules()
{
    return [
        'name'     => ['required', 'regex:/^[a-zA-Z\s]*$/']
    ];
}
Run Code Online (Sandbox Code Playgroud)

我知道,这个答案可能会因其他人而有所不同。但是,这就是我做出一些改变的原因:

  • 在规则中使用数组。正如Laravel Docs 中提到的,在使用 Regex 时,您应该更好地使用数组作为规则。
  • 使用指定的正则表达式来验证输入请求。当然,你可以选择上面的答案,但我最近发现了一些错误。它允许空字符通过验证。我知道,这可能有点偏执,但如果我找到了更好的答案,为什么不使用它呢?

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