laravel 打印验证错误信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24264771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:39:43  来源:igfitidea点击:

laravel print the validation error message

phplaravellaravel-4

提问by Anastasie Laurent

I read the official page about validation in laravel http://laravel.com/docs/validation#working-with-error-messagesand I followed them. So my code is

我阅读了有关 laravel 中验证的官方页面 http://laravel.com/docs/validation#working-with-error-messages并关注了他们。所以我的代码是

 $input = Input::all();
        $validation = Validator::make($input, Restaurant::$rules);

        if ($validation->passes())
        {
            Restaurant::create($input);

            return Redirect::route('users.index');
        }
        else{
            $messages = $validation->messages();
            foreach ($messages->all() as $message)
            {
                echo $message + "</br>";
            }
        }

I can see the error message but it is just 00. Is there a better way to know in which form's field the error is and what is the error description?

我可以看到错误消息,但它只是00. 有没有更好的方法来知道错误在哪个表单的字段中以及错误描述是什么?

I already have rulesand I now the input is breaking the rules but I need to read the error message

我已经有了rules,现在输入违反了规则,但我需要阅读错误消息

回答by William Kinaan

 $messages = $validation->messages();

            foreach ($messages->all('<li>:message</li>') as $message)
            {
                echo $message;
            }

Official Documentation

官方文档

After calling the messages method on a Validator instance, you will receive a MessageBag instance, which has a variety of convenient methods for working with error messages.

在 Validator 实例上调用 messages 方法后,您将收到一个 MessageBag 实例,该实例具有多种处理错误消息的便捷方法。

According to the MessageBag documentationthe function allGet all of the messages for every key in the bag.

根据MessageBag 文档,函数all获取包中每个键的所有消息。

回答by Arda

You can access errors through the errors()object, and loop through the all rules' keys.

您可以通过errors()对象访问错误,并遍历所有规则的键。

Something like this:

像这样的东西:

Route::get('error', function(){

    $inputs = array(
        'id'        => 5,
        'parentID'  => 'string',
        'title'     => 'abc',
    );

    $rules = array(
        'id'        => 'required|integer',
        'parentID'  => 'required|integer|min:1',
        'title'     => 'required|min:5',
    );

    $validation = Validator::make($inputs, $rules);

    if($validation->passes()) {
        return 'passed!';
    } else {
        $errors = $validation->errors(); //here's the magic
        $out = '';
        foreach($rules as $key => $value) {
            if($errors->has($key)) { //checks whether that input has an error.
                $out.= '<p>'$key.' field has this error:'.$errors->first($key).'</p>'; //echo out the first error of that input
            }
        }

        return $out;
    }

});