Laravel 7 - 如何正确显示旧输入/s?

Posted

技术标签:

【中文标题】Laravel 7 - 如何正确显示旧输入/s?【英文标题】:Laravel 7 - How to display properly the old input/s? 【发布时间】:2021-01-02 06:59:54 【问题描述】:

在将表单输入/值提交给控制器验证之前,当验证失败时,我试图显示旧的/以前的输入,但它只重定向回页面,表单中的所有输入字段为空或不显示以前的输入。

这是我在store函数中的控制器代码(SaleController)

public function store(Request $request)
    
        $validator = Validator::make($request->all(), [
            'invoice_no' => 'required|unique:sales_h|max:255',
        ]);

        if($validator->fails())
            //dd($validator);
            Toastr::warning('Invoice No. cannot be repeated or blank.', 'Warning');
            return redirect()->back()->withInput($request->all);
        
    /* ...... */

这是我的视图刀片文件 (create.blade.php) 的 sn-p 源代码:

<form class="form-horizontal" method = "POST" action = " route('sales.store') ">
            @csrf
            <div class="card-body">
                @if ($errors->any())
                    <div class="alert alert-danger">
                        <ul class="mb-0">
                        @foreach ($errors->all() as $error)
                            <li> $error </li>
                        @endforeach
                        </ul>
                    </div>
                @endif
                <div class="form-group row">
                    <div class="col-md-12">
                        <div class="alert alert-info alert-dismissible">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                            <h5><i class="icon fas fa-info" d></i> Info!</h5>
                            A sales order is a document generated by the seller upon receiving a 
                            purchase order from a <strong>buyer/customer</strong> specifying the details about the product or service 
                            along with price, quantity, buyer details.
                        </div>
                        <strong><font color="red">*</font> Indicates required fields.</strong>
                    </div>
                    <div class="col-md-4">
                        <label for="date" class="col-sm-6 col-form-label">Date <font color="red">*</font></label>
                        <div class="input-group mb-2 $errors->has('date') ? 'has-error' : ''" >
                            <div class="input-group-prepend">
                                <span class="input-group-text"><i class="far fa-calendar-alt"></i></span>
                            </div>
                            <input type="text" 
                                    class="form-control" 
                                    id="date" 
                                    name = "date"
                                    autocomplete="off" 
                                    value=" old('date') "
                                    required>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <label for="invoice_no" class="col-sm-6 col-form-label">Invoice No. <font color="red">*</font></label>
                        <div class="input-group mb-2">
                            <div class="input-group-prepend $errors->has('invoice_no') ? 'has-error' : ''">
                                <span class="input-group-text"><i class="fas fa-hashtag"></i></span>
                            </div>
                            <input type="text" 
                                    class="form-control" 
                                    id="invoice_no" 
                                    name = "invoice_no" 
                                    maxlength = "15"
                                    value=" old('invoice_no') "
                                    onkeyup="this.value=this.value.replace(/[^\d]/,'')" 
                                    required>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <label for="shop_name" class="col-sm-6 col-form-label">Customer Name <font color="red">*</font></label>
                        <div class="input-group mb-2">
                            <div class="input-group-prepend $errors->has('shop_name') ? 'has-error' : ''">
                                <span class="input-group-text"><i class="fas fa-hashtag"></i></span>
                            </div>
                            <input type = "text" 
                                    class="form-control" 
                                    id="shop_name" 
                                    name = "shop_name" 
                                    placeholder = "Customer Name" 
                                    autocomplete = "on"
                                    value=" old('shop_name') "
                                    required>
                            </div>
                        </div>
                    </div>
                </div>
                <div class = "form-group row">
                    <div class="col-md-12">
                        <label for = "info" class = "col-md-4 col-form-label"><strong><i class="fas fa-info-circle"></i></strong> Sales Order Information</label>
                        <div class = "col-sm-12  $errors->has('product_information') ? 'has-error' : ''">
                            <textarea name = "product_information" 
                                    class = "form-control" 
                                    rows = "4"
                                    value=" old('product_information') "
                                    ></textarea>
                        </div>
                    </div>
                </div>

                <div class = "col-md-12 field-wrapper">
                    <div class = "form-group row">
                        <div class="col-md-12">
                            <label for="id_raw_product" class="col-sm-4 col-form-label">Product Name <font color="red">*</font></label>
                        </div>
                        <div class="col-sm-3">
                            <input type="hidden" readonly = "true" class="form-control" id="id_raw_product_1" name = "id_raw_product[]" placeholder = "Product Name" required>
                            <input type="text" 
                                    readonly = "true" 
                                    class="form-control" 
                                    id="name_raw_product_1" 
                                    name = "name_raw_product[]" 
                                    value=" old('id_raw_product_1') "
                                    placeholder = "Product Name">
                        </div>
                        <div class="col-sm-2">
                            <a href = "/transaction/sales/product/popup_media/1" 
                                class = "btn btn-info" 
                                title = "Product" 
                                data-toggle = "modal" 
                                data-target = "#modal-default">Product</a>
                        </div>
                        <div class="col-sm-3">
                            <input type="number" 
                                class="form-control" 
                                id="price_1" 
                                name = "price[]" 
                                value=" old('price_1') "
                                placeholder = "Price" 
                                onkeyup="this.value=this.value.replace(/[^\d]/,'')" required>
                        </div>
                        <div class="col-sm-2">
                            <input type="number" 
                                class="form-control" 
                                id="total_1" 
                                name = "total[]" 
                                value=" old('total_1') "
                                placeholder = "Quantity" 
                                onkeyup="this.value=this.value.replace(/[^\d]/,'')" required>
                        </div>
                        <div class = "col-sm-2">
                            <a href = "javascript:void(0)" class = "btn btn-primary add_Button" title = "Add Row"><i class = "fas fa-plus"></i></a>

                        </div>
                    </div>
                </div>
            </div>
            <!-- /.card-body -->
            <div class="card-footer">
              <button type="submit" class="btn btn-default float-right" name = "submit_create" id = "submit_create">Submit</button>
            </div>
            <!-- /.card-footer -->
          </form>

【问题讨论】:

你不能把Toastr::warning() 放在return 语句之后...代码永远不会到达那里。如果你把它放在之前返回,你可能会有更多的运气。 @TimLewis 尝试将它放在 return 语句之前,但仍然没有运气。无论如何,感谢您注意到它将无法访问:) 【参考方案1】:

像这样编写你的控制器代码 -

public function store(Request $request)

    $validator = Validator::make($request->all(), [
        'invoice_no' => 'required|unique:sales_h|max:255',
    ]);

    if($validator->fails())
        return redirect()->back()->withInput()->withErrors($validator->messages()->all());

    

并在刀片文件中添加此代码 -

@if ($errors->any())
    <div class="alert alert-danger">
        <ul class="mb-0">
           @foreach ($errors->all(':message') as $input_error)
                 <li> $input_error </li>
           @endforeach
        </ul>
    </div>
@endif

【讨论】:

控制器端的代码运行良好。尝试打印 $validator 的内容,它会打印验证失败的输入名称及其相应的错误消息,但它不会在刀片文件中发送并且不显示错误。 您是否添加了我在解决方案中添加的刀片文件代码?请尝试此解决方案,如果它不起作用,请告诉我。我将研究另一种解决方案。谢谢。 要在 laravel 中打印验证错误,需要检索错误消息包,所以我稍微更改了您的控制器代码并尝试在刀片文件中打印它。请试试这个并告诉我。 是的,我还添加了您为刀片文件提供的代码。我将它添加到
下方,因为如果发生错误,它将创建一个新的 div。但仍然没有运气并遇到同样的问题。
编辑了我的帖子。只需要显示旧输入,因为我已经有一个 Toastr 用于验证失败时的错误消息。

以上是关于Laravel 7 - 如何正确显示旧输入/s?的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 4 - 显示带有旧数据输入以及数据库信息的编辑表单

Laravel 重新提交表单验证仍然显示旧输入值

如何在 Laravel 中使用旧输入重定向?

如何让用户在 Laravel 7.x 中更改自己的密码?

在 laravel 5 中的表单请求验证后传递旧输入

Laravel 8 - 旧输入和验证错误消息不起作用