Laravel验证-检查数组是否具有所需值

Laravel验证-检查数组是否具有所需值,laravel,Laravel,Laravel将数组发送到字段名行这基本上如下所示: [row] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => b_number [6] => u_number [7

Laravel将数组发送到字段名
这基本上如下所示:

    [row] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
            [4] => 
            [5] => b_number
            [6] => u_number
            [7] => 
            [8] => 
            [9] => a_name
            [10] => a_value
            [11] => c_name
            [12] => floor
            [13] => stack
        )
这个数组可以在不同的索引处有值,这个数组可以比这个数组大也可以小。但主要的要求是检查这两种情况

b_number,
u_number, 
a_name,
a_value, 
c_name, 
floor, 
stack
这些值存在于数组中。如果缺少某个值,我需要返回error作为

b_number是必需的
c_名称是必需的

到目前为止,我所做的是:

$rules = [
            'row' => [new RequiredCSVColumn()]
        ];
所需CSV列

    public function passes($attribute, $value)
    {
        $arr = array();
        $required_all = array('b_number', 'u_number', 'a_name' ,'a_value', 'c_name', 'floor', 'stack');

        foreach ($required_all as $k => $v) {
            if(!in_array($v,$value))
            {
                $arr[] = $v;
            }
        }
        if(!empty($arr)){
            return false;
        }
        return true;

    }

此处,$arr包含缺少的字段名,我只需返回此消息,因为需要
字段名

您必须像这样返回自定义消息

class RequiredCSVColumn implements Rule
{
    private $message;

    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct($message = null)
    {
        $this->message = $message;
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
         $arr = array();
         $required_all = array('b_number', 'u_number', 'a_name' ,'a_value', 'c_name', 'floor', 'stack');

         foreach ($required_all as $k => $v) {
            if(!in_array($v,$value))
            {
                $arr[] = $v;
                $this->message += $v." is required ";
            }
         }
        if(!empty($arr)){
           return false;
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return $this->message;
    }
}

您必须像这样返回自定义消息

class RequiredCSVColumn implements Rule
{
    private $message;

    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct($message = null)
    {
        $this->message = $message;
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
         $arr = array();
         $required_all = array('b_number', 'u_number', 'a_name' ,'a_value', 'c_name', 'floor', 'stack');

         foreach ($required_all as $k => $v) {
            if(!in_array($v,$value))
            {
                $arr[] = $v;
                $this->message += $v." is required ";
            }
         }
        if(!empty($arr)){
           return false;
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return $this->message;
    }
}