1. <small id='pceYi'></small><noframes id='pceYi'>

      <bdo id='pceYi'></bdo><ul id='pceYi'></ul>
    <tfoot id='pceYi'></tfoot>
  2. <i id='pceYi'><tr id='pceYi'><dt id='pceYi'><q id='pceYi'><span id='pceYi'><b id='pceYi'><form id='pceYi'><ins id='pceYi'></ins><ul id='pceYi'></ul><sub id='pceYi'></sub></form><legend id='pceYi'></legend><bdo id='pceYi'><pre id='pceYi'><center id='pceYi'></center></pre></bdo></b><th id='pceYi'></th></span></q></dt></tr></i><div id='pceYi'><tfoot id='pceYi'></tfoot><dl id='pceYi'><fieldset id='pceYi'></fieldset></dl></div>
  3. <legend id='pceYi'><style id='pceYi'><dir id='pceYi'><q id='pceYi'></q></dir></style></legend>

      如何在 Laravel 中手动返回或抛出验证错误/异常

      时间:2023-09-21
        <tbody id='qd5ba'></tbody>

        • <small id='qd5ba'></small><noframes id='qd5ba'>

        • <tfoot id='qd5ba'></tfoot>
          <legend id='qd5ba'><style id='qd5ba'><dir id='qd5ba'><q id='qd5ba'></q></dir></style></legend>
          <i id='qd5ba'><tr id='qd5ba'><dt id='qd5ba'><q id='qd5ba'><span id='qd5ba'><b id='qd5ba'><form id='qd5ba'><ins id='qd5ba'></ins><ul id='qd5ba'></ul><sub id='qd5ba'></sub></form><legend id='qd5ba'></legend><bdo id='qd5ba'><pre id='qd5ba'><center id='qd5ba'></center></pre></bdo></b><th id='qd5ba'></th></span></q></dt></tr></i><div id='qd5ba'><tfoot id='qd5ba'></tfoot><dl id='qd5ba'><fieldset id='qd5ba'></fieldset></dl></div>

            • <bdo id='qd5ba'></bdo><ul id='qd5ba'></ul>
              • 本文介绍了如何在 Laravel 中手动返回或抛出验证错误/异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                有一种将 CSV 数据导入数据库的方法.我使用

                Have a method that's importing CSV-data into a Database. I do some basic validation using

                class CsvImportController extends Controller
                {
                    public function import(Request $request)
                    {   
                        $this->validate($request, [
                            'csv_file' => 'required|mimes:csv,txt',
                        ]);
                

                但在那之后,由于更复杂的原因,事情可能会出错,更深入的兔子洞,会引发某种异常.我无法在这里编写与 validate 方法一起使用的正确验证内容,但是,我真的很喜欢 Laravel 在验证失败时的工作方式以及将错误嵌入到刀片中是多么容易查看等,所以...

                But after that things can go wrong for more complex reasons, further down the rabbit hole, that throws exceptions of some sort. I can't write proper validation stuff to use with the validate method here, but, I really like how Laravel works when the validation fails and how easy it is to embed the error(s) into the blade view etc, so...

                是否有一种(最好是干净的)方法可以手动告诉 Laravel我知道我现在没有使用你的 validate 方法,但我真的很喜欢你像我一样在这里公开这个错误"?有什么我可以返回的东西,一个可以用来包装东西的例外,或者什么?

                Is there a (preferably clean) way to manually tell Laravel that "I know I didn't use your validate method right now, but I'd really like you to expose this error here as if I did"? Is there something I can return, an exception I can wrap things with, or something?

                try
                {
                    // Call the rabbit hole of an import method
                }
                catch(Exception $e)
                {
                    // Can I return/throw something that to Laravel looks 
                    // like a validation error and acts accordingly here?
                }
                

                推荐答案

                从 laravel 5.5 开始,ValidationException 类有一个静态方法 withMessages 您可以使用:

                As of laravel 5.5, the ValidationException class has a static method withMessages that you can use:

                $error = IlluminateValidationValidationException::withMessages([
                   'field_name_1' => ['Validation Message #1'],
                   'field_name_2' => ['Validation Message #2'],
                ]);
                throw $error;
                

                我还没有测试过这个,但它应该可以工作.

                I haven't tested this, but it should work.

                更新

                消息不必包装在数组中.你也可以这样做:

                The message does not have to be wrapped in an array. You can also do:

                use IlluminateValidationValidationException;
                
                throw ValidationException::withMessages(['field_name' => 'This value is incorrect']);
                

                这篇关于如何在 Laravel 中手动返回或抛出验证错误/异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:我可以用 PHP 检测和处理 MySQL 警告吗? 下一篇:PHP PDO.查询正确时的错误号“00000"

                相关文章

                最新文章

              • <small id='fVBV1'></small><noframes id='fVBV1'>

                <tfoot id='fVBV1'></tfoot>
                <i id='fVBV1'><tr id='fVBV1'><dt id='fVBV1'><q id='fVBV1'><span id='fVBV1'><b id='fVBV1'><form id='fVBV1'><ins id='fVBV1'></ins><ul id='fVBV1'></ul><sub id='fVBV1'></sub></form><legend id='fVBV1'></legend><bdo id='fVBV1'><pre id='fVBV1'><center id='fVBV1'></center></pre></bdo></b><th id='fVBV1'></th></span></q></dt></tr></i><div id='fVBV1'><tfoot id='fVBV1'></tfoot><dl id='fVBV1'><fieldset id='fVBV1'></fieldset></dl></div>

                  1. <legend id='fVBV1'><style id='fVBV1'><dir id='fVBV1'><q id='fVBV1'></q></dir></style></legend>
                      <bdo id='fVBV1'></bdo><ul id='fVBV1'></ul>