validate data with the request

0

example I have a view with

{!! Form::text('pagado',$pago, null, ['class' => 'form-control', 'placeholder' => 'Ingrese Cantidad $$$' , 'required']) !!} 
  • the variables I bring from a database $ payment.

I have a request (PaymentRequest.php)

'pagado' => 'min:2|max:10|integer|required'
  • because when I enter a minimum amount of 2 characters validates me and throws the error and does not let me move forward with the save, I mean everything is fine.

then my query is as follows, how can I validate that the entry in Form :: text ('paid') if it is less than the variable $ payment, do not let me save in the form ??. that is not written in the PaymentRequest.php or that validation should only be in the view?

------------------- form of view (paid.blade.php) -------------

@section('title', 'Pagar')
@extends('layouts.admin_template')

@section('content')
@if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

{!! Form::model('payment', ['route' => ['payments.update', $id],'method' =>'PUT', 'onsubmit' => 'return confirm("¿Estas Seguro?")']) !!}
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Pagar</div>
                <div class="panel-body">
    <!-- Nav tabs -->
<div role="tabpanel">
<br>
    <!-- Tab panes -->
<div class="form-group">
            {!! Form::label('forma_pago', 'Saldo a Favor: $') !!}
            {!! Form::label('0', $saldo) !!} <br>
            {!! Form::label('forma_pago', 'Abono: $') !!}
            {!! Form::label('abono', $pago_abono) !!} <br><br>
            {!! Form::text('pagado',$pago, null, ['class' => 'form-control', 'placeholder' => 'Ingrese Cantidad $$$' , 'required']) !!} <br><br>
            {!! Form::select('estado', ['pagado' => 'Pagar', 'abono' => 'Abonar'],null,['class'=> 'form-control']) !!} <br>
            {!! Form::checkbox('name', 'value') !!}
            {!! Form::label('forma_pago', ' Utilizar Saldo?') !!}
                </div>



                </div>
    <!-- Tab fin -->

<div class="form-group">
                {!! Form::submit('Guardar',['class' => 'btn btn-primary'] ) !!}
</div>

@endsection
    
asked by Luis Cárdenas 24.08.2016 в 18:26
source

1 answer

1

In this case you would have to create a customized validation, because you need to compare with a value in the database.

There are several ways to do it, in another question I did an extensive explanation on how to create a custom validation method: Validation of date type in laravel

In any case, so as not to repeat the same, I will show another method that can be useful in this situation:

app \ Http \ Requests \ Request.php

namespace app\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Validator;

abstract class Request extends FormRequest
{
  public function validator()
  {

    $v = Validator::make($this->input(), $this->rules(), $this->messages(), $this->attributes());

    if (method_exists($this, 'validateBiggerThanDb')) {
        $this->validateBiggerThanDb($v);
    }

    return $v;
  }
}

app \ Http \ Requests \ PaymentRequest.php

namespace app\Http\Requests;

// utilizar modelo o repositorio que obtiene el valor de $pago
use app\Models\Payment;

class PaymentRequest extends Request
{
  // métodos rules, messages, authorize

  public function validateBiggerThanDb($validator)
  {
     $validator->after(function ($validator) {

       // solo un ejemplo, tú obtienes el valor de $pago de la forma que te convenga
       $minPayment = Payment::obtenerPagoMinimo();

       if (Request::get('pagado') < $minPayment) {
         $validator->errors()->add('pago_minimo', 'El pago mínimo no es suficiente');
       }

     });
  }
    
answered by 24.08.2016 / 19:14
source