douxi1738 2016-10-27 18:21
浏览 59
已采纳

Laravel 5.2返回错误 - 如何判断提交的表单

I'm using Laravel 5.2. On my index page, I've got a button to add a new entry, which brings up the form in a lightbox. This form then submits via the create method.

I also have each entry listed on this page, with the ability to edit them inline, which submits via the update method.

I've setup validation via a Request. This means when someone misses something on the add, it redirects to the index method with errors. The errors only show though, when the lightbox is triggered by the user.

I know I can use $errors to see any errors, but I don't see how I can differentiate between the create and update forms for the sake of forcing the lightbox to appear on reload with create errors. Is there a way to do that?

Update:

Suggestion was made to use AJAX to bypass the reload issue, but now I'm getting a 422 return:

AJAX call:

(function(){
    var submitAjaxRequest = function(e){
        var form = $(this);
        var method = form.find('input[name="_method"]').val() || 'POST';

        $.ajax({
            type: method,
            url: form.prop('action'),
            data: form.serialize(),
            success: function(data){
                console.log(data)
            }
        });

        e.preventDefault();
    }
    $('form[data-remote]').on('submit', submitAjaxRequest);
})();

Request:

public function response(array $errors)
{
    $response = parent::response($errors);

    if ($this->ajax() || $this->wantsJson()) {
        return $response;
    }

    return $response->with('requestMethod', $this->method());
}

I've also tested the ajax call and it works fine when the validation rules are met. It only fails if the validation comes back with something incorrect in the input.

  • 写回答

1条回答 默认 最新

  • dougong8012 2016-10-27 21:10
    关注

    You could override the response method so that you can flash the type of request.

    In you Request class you could add

        public function response(array $errors)
    {
        $response = parent::response($errors);
    
        if ($this->ajax() || $this->wantsJson()) {
            return $response;
        }
    
        return $response->with('requestMethod', $this->method());
    }
    

    (With ajax you wouldn't need to worry about the page reload so we can just return the original response.)

    In the above I'm assuming you're using POST for your create methods and PUT or PATH for your update methods. If this is not the case you could use a way that make sense to you to differentiate between the requests.

    Then in your view you could do something like:

    @if(session('requestMethod') == 'POST')
    

    https://laravel.com/docs/5.2/responses#redirecting-with-flashed-session-data

    If you are going to use ajax, as I mentioned in the comment above, you will need to make sure you use the error method within the ajax call:

    $.ajax({
        type: method,
        url: form.prop('action'),
        data: form.serialize(),
        success: function (data) {
            console.log('success', data)
        },
        error: function (data) {
            console.log('error', data)
        }
    });
    

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog