Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.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 Laravel有条件地添加规则,如果其他验证规则无效,则停止一个规则_Php_Laravel - Fatal编程技术网

Php Laravel有条件地添加规则,如果其他验证规则无效,则停止一个规则

Php Laravel有条件地添加规则,如果其他验证规则无效,则停止一个规则,php,laravel,Php,Laravel,如果其他规则在laravel中未成功,如何停止一个验证规则 我有一个日期输入类型,其中有3条验证规则: 日期 date\u格式:m/d/Y 之后:日期('m/d/Y) 当我输入无效数据时,如04/05/2014dsada后日期规则仍在运行。如何停止运行验证后规则 如何在拉雷维尔完成这项工作 以下是我的示例代码: $rules = array('mmad_starting_date' => 'date|required|date_format:m/d/Y|after:'.date("m/d/

如果其他规则在laravel中未成功,如何停止一个验证规则

我有一个日期输入类型,其中有3条验证规则:

  • 日期
  • date\u格式:m/d/Y
  • 之后:日期('m/d/Y)
  • 当我输入无效数据时,如
    04/05/2014dsada
    后日期规则仍在运行。如何停止运行验证后规则

    如何在拉雷维尔完成这项工作

    以下是我的示例代码:

    $rules = array('mmad_starting_date' => 'date|required|date_format:m/d/Y|after:'.date("m/d/Y H:i"));
    
    如果我将使用laravel有条件地添加规则

    $validator->sometimes('mmad_starting_date', 'after:'.date(m/d/Y), function($input){
           //how can i check if the mad_starting_date is a valid date
         return $input->mmad_starting_date = (what should i input here);
    });
    
    您可以尝试以下方法:(基本上是
    date\u格式的内容
    规则…)


    Thnx lukasgeiter你的解决方案奏效了

    我所做的也是在我的corevalidator类中创建一个自定义验证器,并创建两个函数,它就可以工作了

     public function validateAfterDatetoday($attribute, $value, $parameters){
    
        $today = new DateTime(date("m/d/Y H:i"));
    
        if($this->validateThisDate($value)){
            $date = new DateTime($value);
    
            if($today > $date){
                return false;
            }
    
        }
    
        return true;
    }
     function validateThisDate($date){
    
        $d = DateTime::createFromFormat('m/d/Y', $date);
    
        if($d){
    
            return true;
        }
    
        return false;
    }
    

    并在我的mmad_开始日期字段中使用它。不管怎样,你的解决方案更好

    如果先前的规则已经失败,则可以避免将属性传递到下一个规则

    只需扩展illumb\Validation\Validator类并重写passs方法

    在MessageBag中遇到该属性的错误时,立即中断循环

    public function passes()
    {
        $this->messages = new MessageBag;
    
        // We'll spin through each rule, validating the attributes attached to that
        // rule. Any error messages will be added to the containers with each of
        // the other error messages, returning true if we don't have messages.
        foreach ($this->rules as $attribute => $rules)
        {
            foreach ($rules as $rule)
            {
                $this->validate($attribute, $rule);
                /* If the error MessageBag has error for the $attribute, break */
                if($this->messages->has($attribute))
                    break;
            }
        }
    
        // Here we will spin through all of the "after" hooks on this validator and
        // fire them off. This gives the callbacks a chance to perform all kinds
        // of other validation that needs to get wrapped up in this operation.
        foreach ($this->after as $after)
        {
            call_user_func($after);
        }
    
        return count($this->messages->all()) === 0;
    }
    

    在Laravel5.2中,您可以通过添加
    bail
    规则轻松实现这一点。如果将
    bail
    规则添加到属性中,则如果一个规则失败,验证将不会检查其他规则

    在您的情况下,您可以简单地使用

    $rules = array('mmad_starting_date' => 'bail|required|date_format:m/d/Y|after:'.date("m/d/Y H:i"));
    

    请参阅Laravel验证文件。

    Hi!您必须添加一些代码,使用编辑按钮并粘贴到代码中。请添加一些您使用的代码示例。我们目前不知道你需要完成什么。对不起,我错了,拉威尔不会停止执行其他规则。但真正的问题是,,你为什么希望它这样做?我认为如果用户输入了错误的数据,如我上面提供的数据,那么运行验证后规则是不合适的。例如,用户输入了正确的日期,但在日期结束时输入了不适当的信息,如2015oiewq.04。简言之,如果用户在不应运行验证规则。我理解您的问题。但是它运行与否对你有什么影响呢?其实没有什么大的区别,我只是想知道它是否可能。刚刚测试了这个手动验证-Validator::make()方法。看起来,拉威尔5.2完全无视保释规则。
    $rules = array('mmad_starting_date' => 'bail|required|date_format:m/d/Y|after:'.date("m/d/Y H:i"));