Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉维尔';s";要求“如果”;字段值包含在数组中_Php_Validation_Laravel_Laravel 4 - Fatal编程技术网

Php 拉维尔';s";要求“如果”;字段值包含在数组中

Php 拉维尔';s";要求“如果”;字段值包含在数组中,php,validation,laravel,laravel-4,Php,Validation,Laravel,Laravel 4,在控制器的“存储”(POST)函数中,我想验证一个特定的输入字段。我有一个字段listPrice,只有当另一个字段vendor的值在数组中时才需要该字段。供应商阵列需要通过单独的服务调用填充。因此,我的问题有两个: 如果vendor的值包含在一个数组中,我如何编写验证规则以要求listPrice 何时填充供应商的阵列?它是否在store()函数中,并在每次调用该函数时运行?验证规则位于控制器类的私有数组中,因此我相信这只会创建一次,而不是每次调用store()函数时 一,。 如果您可以从lara

在控制器的“存储”(POST)函数中,我想验证一个特定的输入字段。我有一个字段
listPrice
,只有当另一个字段
vendor
的值在数组中时才需要该字段。供应商阵列需要通过单独的服务调用填充。因此,我的问题有两个:

  • 如果
    vendor
    的值包含在一个数组中,我如何编写验证规则以要求
    listPrice
  • 何时填充供应商的阵列?它是否在
    store()
    函数中,并在每次调用该函数时运行?验证规则位于控制器类的私有数组中,因此我相信这只会创建一次,而不是每次调用
    store()
    函数时
  • 一,。 如果您可以从laravel的validator访问$This[数据],这将是相当简单的,但是如果您只是使用validator::extend作为您的自定义规则,我相信您不能。您也不能从您的规则中访问其他验证规则,这在这里可能很有用。因此,最干净的方法可能是扩展验证器类

    class CustomValidator extends Illuminate\Validation\Validator {
    
        public function validateIfInVendorArray($attribute, $value, $parameters)
        {
        $other = $parameters[0];
        $vendor = $this->data[$other];
    
        //populate your array from your service call, then check if present
        $vendorArray = your service call or wherever you have it;
    
        //if in array, return the result of the Validator's validateRequired method, which we can access since it's protected in Validator        
        if (in_array($vendor,$vendorArray))
            return $this->validateRequired($attribute,$value);
    
        //if it wasn't in the array, return true to pass validation even if it doesn't exist
        return true;
        }
    }
    
    记住也要

    要使用规则,只需将供应商字段的属性名称作为参数传入即可

    二,。 我认为这取决于需要阵列的位置和频率

    如果这个验证只需要它,我会在您的自定义验证规则中进行,以避免为控制器积累更多的工作

    就我个人而言,我会将验证规则移动到自定义验证器类,然后您可以将供应商数组作为该自定义验证器的字段。然后,这个自定义验证器类将成为控制器使用的服务,请参阅以获取一个好的示例

    我自己还没有测试过,但我相信它应该能工作!抱歉,这不是最优雅的解决方案。

    1。 如果您可以从laravel的validator访问$This[数据],这将是相当简单的,但是如果您只是使用validator::extend作为您的自定义规则,我相信您不能。您也不能从您的规则中访问其他验证规则,这在这里可能很有用。因此,最干净的方法可能是扩展验证器类

    class CustomValidator extends Illuminate\Validation\Validator {
    
        public function validateIfInVendorArray($attribute, $value, $parameters)
        {
        $other = $parameters[0];
        $vendor = $this->data[$other];
    
        //populate your array from your service call, then check if present
        $vendorArray = your service call or wherever you have it;
    
        //if in array, return the result of the Validator's validateRequired method, which we can access since it's protected in Validator        
        if (in_array($vendor,$vendorArray))
            return $this->validateRequired($attribute,$value);
    
        //if it wasn't in the array, return true to pass validation even if it doesn't exist
        return true;
        }
    }
    
    记住也要

    要使用规则,只需将供应商字段的属性名称作为参数传入即可

    二,。 我认为这取决于需要阵列的位置和频率

    如果这个验证只需要它,我会在您的自定义验证规则中进行,以避免为控制器积累更多的工作

    就我个人而言,我会将验证规则移动到自定义验证器类,然后您可以将供应商数组作为该自定义验证器的字段。然后,这个自定义验证器类将成为控制器使用的服务,请参阅以获取一个好的示例


    我自己还没有测试过,但我相信它应该能工作!如果这不是最优雅的解决方案,很抱歉。

    Hi@pfrendly:我遵循您建议的解决方案。这很好,但面临一个问题。Fox示例:如果我像这样使用它:“field\u name”=>“required”;“if\u in\u vendor\u array:vendor\u field”它正在工作,但是“field\u name”=>“if\u in\u vendor\u array:vendor\u field”不工作……原因可能是什么?Hi@pfrendly:我正在遵循您建议的解决方案。这很好,但面临一个问题。Fox示例:如果我像这样使用它:“field\u name”=>“required”;“if\u in\u vendor\u array:vendor\u field”它正在工作,但是“field\u name”=>“if\u in\u vendor\u array:vendor\u field”不工作……原因可能是什么?