Clement Tay
Clement Tay

Reputation: 33

Variable Validation

I want to make a validation variable as I will be validating the same input but with a different max numeric. So this is the problem, for example I have a variable validation of max numeric 400 or 1000. After I enter 500 into the field that is suppose to reject above 400, It works perfectly fine. But after validating the 400 max and I tried to validate the field that is suppose to allow up to 1000, its now limited to 400.

Here is the store function:

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);

Here is the view:

@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

Here is the modal containing the form(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() !!}

Thanks in advance!

Upvotes: 1

Views: 89

Answers (2)

Clement Tay
Clement Tay

Reputation: 33

Removing ->withInput() from the validation worked.

From:

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

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

To:

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

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

Upvotes: 1

Pc Monk
Pc Monk

Reputation: 85

these lines say a lot lets take a look at it

in this line u are defining ur max input value which is 400

$max_topup_amount = $request->max_topup_amount;

in this part of ur code u are actually giving $max_topup_amount as the max value BUT you are not changing it anywhere else, so whatever the value is at the begining it will stay as that.

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

Upvotes: 1

Related Questions