【问题标题】:Laravel 5.1 validation rule alpha cannot take whitespaceLaravel 5.1 验证规则 alpha 不能带空格
【发布时间】:2016-03-10 01:27:51
【问题描述】:

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

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

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

【问题讨论】:

    标签: php forms validation laravel laravel-5.1


    【解决方案1】:

    您可以使用Regular Expression Rule,它只允许显式使用字母、连字符和空格:

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

    【讨论】:

      【解决方案2】:

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

      在您的 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
      • 别忘了在app/Providers/AppServiceProvider.php 的顶部添加use Illuminate\Support\Facades\Validator; ;)
      • 您也可以在服务提供者中使用 $this->app 并添加第三个参数作为错误消息。在不需要翻译时很有用。 $this->app['validator']->extend('my_rule', function ($attributes, $value, $parameters) { return (bool) preg_match(...); }, ':attribute my custom message。 ');
      • 您是否尝试在规则中使用字符串? 'name' => 'required|string',
      【解决方案3】:

      您可以使用This Regular Expression 来验证您的输入请求。但是,你应该仔细编写RegEx规则来实现。

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

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

      我知道,这个答案可能与其他人略有不同。但是,这就是我进行一些更改的原因:

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

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

      【讨论】:

        猜你喜欢
        • 2016-04-08
        • 1970-01-01
        • 2016-01-13
        • 1970-01-01
        • 2017-08-12
        • 2018-12-21
        • 1970-01-01
        • 2018-07-18
        • 2016-12-17
        相关资源
        最近更新 更多