【问题标题】:Validate array Laravel 5验证数组 Laravel 5
【发布时间】:2016-09-05 18:08:14
【问题描述】:

我的 ajax 脚本发送数组如下: 这个数组属于Input::get('questions')

 Array
(
    [0] => Array
        (
            [name] => fields[]
            [value] => test1
        )

    [1] => Array
        (
            [name] => fields[]
            [value] => test2
        )

)

在html部分用户可以添加多个fields

你能帮我解决我需要这样的事情吗:

           $inputs = array(
                'fields'    => Input::get('questions')
            );

            $rules = array(
                'fields'    => 'required'
            );
            $validator = Validator::make($inputs,$rules);

                if($validator -> fails()){
                    print_r($validator -> messages() ->all());
                }else{
                    return 'success';
                }

【问题讨论】:

    标签: php validation laravel-5


    【解决方案1】:

    简单:使用 for-each 分别验证每个 question

    // First, your 'question' input var is already an array, so just get it
    $questions = Input::get('questions');
    
    // Define the rules for *each* question
    $rules = [
        'fields' => 'required'
    ];
    
    // Iterate and validate each question
    foreach ($questions as $question)
    {
        $validator = Validator::make( $question, $rules );
    
        if ($validator->fails()) return $validator->messages()->all();
    }
    
    return 'success';
    

    【讨论】:

      【解决方案2】:

      数组元素的 Laravel 自定义验证

      打开以下文件

      /resources/lang/en/validation.php
      

      然后自定义消息添加

      'numericarray'         => 'The :attribute must be numeric array value.',
      'requiredarray'        => 'The :attribute must required all element.',
      

      这样,打开另一个文件

      /app/Providers/AppServiceProvider.php
      

      现在用下面的代码替换开机功能代码。

      public function boot()
      {
          // it is for integer type array checking.
          $this->app['validator']->extend('numericarray', function ($attribute, $value, $parameters)
          {
              foreach ($value as $v) {
                  if (!is_int($v)) {
                      return false;
                  }
              }
              return true;
          });
      
         // it is for integer type element required.
         $this->app['validator']->extend('requiredarray', function ($attribute, $value, $parameters)
          {
              foreach ($value as $v) {
                  if(empty($v)){
                      return false;
                  }
              }
              return true;
          });
      }
      

      现在可以将 requiredarray 用于所需的数组元素。并且还使用 numericarray 进行数组元素整数类型检查。

      $this->validate($request, [
                  'arrayName1' => 'requiredarray',
                  'arrayName2' => 'numericarray'
              ]);
      

      【讨论】:

        猜你喜欢
        • 2015-08-17
        • 2015-10-10
        • 2015-10-04
        • 1970-01-01
        • 2019-08-09
        • 1970-01-01
        • 2015-07-02
        • 2016-10-13
        • 2015-10-15
        相关资源
        最近更新 更多