变量验证

时间:2018-09-26 03:38:56

标签: laravel bootstrap-4 laravel-blade

我想创建一个验证变量,因为我将验证相同的输入但具有不同的最大数值。所以这就是问题所在,例如,我有一个最大为400或1000的变量验证。在我将500拒绝应该超过400的字段中输入后,它工作得很好。但是在验证了400个最大值之后,我尝试验证应该允许最多1000个的字段,现在该字段限制为400个。

这是存储功能:

public function store(Request $request)
{
    $client_id = $request->client_id; 
    $client_package_id = $request->client_package_id;
    $sale_type_id = $request->sale_type_id;
    $max_topup_amount = $request->max_topup_amount;

    if($sale_type_id == 3){
        $validator = Validator::make($request->all(), [
            'amount' => 'numeric|min:0|max:' . $max_topup_amount,
        ]);

        if ($validator->fails()) {
            return redirect('/sales' . '/' . $client_id)
                        ->withErrors($validator)
                        ->withInput();
        }

        $Invoices = new Invoices;
        $Invoices->client_id = $client_id;
        $Invoices->sale_type_id = $sale_type_id;
        $Invoices->item_id = $client_package_id;
        $Invoices->total_price = $request->amount;
        $Invoices->status = 'Payment Pending';
        $Invoices->save();
    };

    return redirect('/sales' . '/' . $client_id);

这是视图:

@if(count($Client_Packages) > 0)
                            @foreach($Client_Packages as $c)
                                <tbody>
                                    <tr>
                                        <td>{{$c->id}}</td>
                                        <td>{{$c->package}}</td>
                                        <td>{{$c->balance}}</td>
                                        <td>{{$c->status}}</td>
                                        {!!Form::open(['action' => ['SalesController@destroy', $c->id], 'method' => 'POST'])!!}
                                        {{Form::hidden('_method', 'DELETE')}}
                                        {{Form::hidden('type', 'package')}}
                                        {{Form::hidden('status', $c->status)}}
                                        {{Form::hidden('client_id', $c->client_id)}}
                                        <td>
                                            <button type="button" class="btn btn-primary fa" data-toggle="modal" data-target="#topup{{$c->id}}">
                                                &#xf217;
                                            </button>  
                                            <input type="button" class="btn btn-success fa" value="&#xf218;" onclick="location.href = '/sales/{{$c->id}}';">
                                            {{Form::submit('&#xf2ed;', ['class' => 'btn btn-danger fas'])}}
                                        </td>
                                        {!!Form::close()!!}
                                    </tr>
                                </tbody>
                                @include('Sales.edit')
                            @endforeach
                        @else
                            <p>No Clients Found</p>
                        @endif

以下是包含表单(Sales.edit)的模式:

{!! Form::open(['action' => 'InvoicesController@store', 'method' => 'POST']) !!}
<div class="modal fade" id="topup{{$c->id}}">
<div class="modal-dialog">
    <div class="modal-content">  
        <!-- Modal Header -->
        <div class="modal-header bg-success text-white">
            <h4 class="modal-title">Top up Package Serial No. {{$c->id}}</h4>
            {{Form::hidden('client_package_id', $c->id)}}
            <button type="button" class="close" data-dismiss="modal">&times;                                </button>
        </div>
        <!-- Modal body -->
        <div class="modal-body">
            <div class="row mt-3">
                <div class="col-lg-12">
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="form-group">
                                {{Form::label('amount', 'Amount($)')}}
                                {{Form::text('amount', '', ['class' => 'form-control', 'placeholder' => 'SGD'])}}
                                <p>Max Amount: {{$c->payable}} </p>
                                {{Form::hidden('max_topup_amount', $c->payable)}}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- Modal footer -->
        <div class="modal-footer">
            {{Form::hidden('sale_type_id', 3)}}
            {{Form::hidden('client_id', $Clients->id)}}
            {{Form::submit('Top-up', ['class'=>'btn btn-success'])}}
            <button type="button" class="btn btn-danger" data-

dismiss="modal">Cancel</button>
            </div>
        </div>
    </div>
</div>
{!! Form::close() !!}

谢谢!

2 个答案:

答案 0 :(得分:1)

这些行说很多让我们看一下

在此行中,您定义的最大输入值为400

$max_topup_amount = $request->max_topup_amount;

在您的代码的这一部分中,您实际上给出了$ max_topup_amount作为最大值 但是,您没有在其他任何地方进行更改,因此,不管一开始的值是什么,它都会保持不变。

  if($sale_type_id == 3){
        $validator = Validator::make($request->all(), [
            'amount' => 'numeric|min:0|max:' . $max_topup_amount,
        ]);

答案 1 :(得分:1)

从验证中删除-> withInput()成功。

发件人:

 $validator = Validator::make($request->all(), [
        'amount' => 'numeric|min:0|max:' . $max_topup_amount,
    ]);

    if ($validator->fails()) {
        return redirect('/sales' . '/' . $client_id)
                    ->withErrors($validator)
                    ->withInput();
    }

收件人:

 $validator = Validator::make($request->all(), [
        'amount' => 'numeric|min:0|max:' . $max_topup_amount,
    ]);

    if ($validator->fails()) {
        return redirect('/sales' . '/' . $client_id)
                    ->withErrors($validator);
    }