Laravel 5.1验证规则alpha不能包含空格。

29

我已经创建了一个注册表单,农民可以在其中输入他的姓名。姓名可能包含连字符或空格。验证规则已写在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',
    ];
}

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

我该怎么做,才能让用户输入带有空格的名称?

3个回答

65
你可以使用一个只允许字母、连字符和空格的正则表达式规则
public function rules()
{
    return [
        'name'     => 'required|regex:/^[\pL\s\-]+$/u',
        'email'    => 'email|unique:users,email',
        'password' => 'required',
        'phone'    => 'required|numeric',
        'address'  => 'required|min:5',
    ];
}

58

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

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

    });

}

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.',
 ....

然后像往常一样使用它

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

在哪里可以找到 AppServiceProvider.php 文件? - Yousef Altaf
7
不要忘记在你的app/Providers/AppServiceProvider.php文件的顶部添加use Illuminate\Support\Facades\Validator; ;) - tsveti_iko
1
在服务提供者中,您还可以使用 $this->app,并添加第三个参数作为错误消息。当不需要进行翻译时非常有用。$this->app ['validator']->extend('my_rule',function($ attributes,$ value,$ parameters){return(bool)preg_match(...);},':attribute我的自定义消息。'); - Maarten Kuijper
2
你尝试在规则中使用字符串了吗? 'name' => 'required|string', - nsg

8

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

在这里,您可以使用这个正则表达式来验证只允许字母和空格。

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

我知道,这个答案可能与其他人略有不同。但是,我做出了一些改变的原因如下:
  • 在规则中使用数组。如Laravel文档所述,当使用正则表达式时,最好使用数组作为规则。
  • 使用指定的正则表达式验证输入请求。当然,您可以选择上面的答案,但我最近发现它存在一些错误。它允许空字符通过验证。我知道,这可能有点多疑,但如果我找到更好的答案,为什么不使用呢?

别误会我的意思。我知道,其他答案也很好。但我认为,验证每个内容都尽可能具体,这样我们就可以保护我们的应用程序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接