Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate Money in Laravel5 request class

My Validation class looks like this

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class PaymentRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = array(

                       'invoiceid'=>'required',
                       'recieved_amount'=>'required',
                       'ref_no'=>'required',
                       'date'=>'required',
                       'comment'=>'required'
                       );


    }
}

I want to validate recieved_amount as Money field Like if anything other than money is entered it should be validated

Can any one help me in this

like image 450
Vikram Anand Bhushan Avatar asked Sep 05 '25 16:09

Vikram Anand Bhushan


1 Answers

You can use this, for example (being 'amount' the money quantity):

public static $rules = array(
  'amount' => "required|regex:/^\d+(\.\d{1,2})?$/"
));

The regex will hold for quantities like '12' or '12.5' or '12.05'. If you want more decimal points than two, replace the "2" with the allowed decimals you need.

like image 144
Amarnasan Avatar answered Sep 07 '25 08:09

Amarnasan