Laravel check on a checkbox called an array

I have some areas in my form:

<ul>
<li>
<input type="checkbox" name="post_categories[]" value="16">English First Main Category<br>
<ul>
<li><input type="checkbox" name="post_categories[]" value="17">English First Subcategory<br></li>
</ul>
</li>
</ul>

When I try to check them as required fields or something else, Laravel did not confirm the rules. My rules are something like below (In / application / models / posts.php):

public static $rules = array(

    'post_title' => 'required',
    'post_body' => 'required',
    'content_language'=>'required|alpha',
    'post_categories'=>'array_numeric',
    'post_sequence_number'=>'numeric'

    );


public static function validate($data){ 

    return Validator::make($data, static::$rules);

}

In / application / library / validate.php I have a function that checks the array as numeric or not:

Class Validator extends Laravel\Validator {

        public function validate_array_numeric($attribute, $value, $parameters){
            $numeric_values = array_filter($value, create_function('$item', 'return (is_numeric($item));'));
            return count($numeric_values) == count($value);
        }

    }

Rules work fine except for post_categories []. I get an error message:

Method [array_numeric] does not exist.

Greetings.

0
source share
3 answers

I do not know if this problem was resolved in Laravel 4. Maybe you can try. But what I am doing now extends the validation class.

, . , . application/libraries:

class Validator extends Laravel\Validator {

    public function validate_arraynumeric($attribute, $value, $parameters){
        $numeric_values = array_filter($value, create_function('$item', 'return (is_numeric($item));'));
        return count($numeric_values) == count($value);
    }

}

. application/language/en/validation.php. :

"arraynumeric"   => "The :attribute contains non-numeric values",

application/config/application.php:

'Validator' => 'Laravel\\Validator'

:

public static $rules = array(
'post_categories'=>'array_numeric'
);

. , . , 1.

+3

. :

, Laravel\Validator. , (, ). , "_array" . , , , , ( ), Laravel.

<?php

class Validator extends Laravel\Validator {

  public function __call($method, $parameters)
  {
      if (substr($method, -6) === '_array')
      {
          $method = substr($method, 0, -6);
          $values = $parameters[1];
          $success = true;
          foreach ($values as $value) {
              $parameters[1] = $value;
              $success &= call_user_func_array(array($this, $method), $parameters);
          }
          return $success;
      }
      else 
      {
          return parent::__call($method, $parameters);
      }
  }

  protected function getMessage($attribute, $rule)
  {
      if (substr($rule, -6) === '_array')
      {
          $rule = substr($rule, 0, -6);
      }

      return parent::getMessage($attribute, $rule);
  }
}

, , Validator :

/config/application.php:

'Validator' = > 'Laravel\Validator'

, Laravel "_array". :

public static $rules = array(
  'post_categories'=>'required_array|alpha_array'
);
+5

You do something weird here.

Using post_categories[]as forms names generates an array. This means that you cannot confirm it with 'post_categories[]'=>'required|numeric'. Instead, you need to iterate over the array and check each field of its own.

0
source

Source: https://habr.com/ru/post/1537644/


All Articles