laravel 如何在 Laravel5 请求类中验证金钱

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33624710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:40:05  来源:igfitidea点击:

How to validate Money in Laravel5 request class

phpvalidationlaravellaravel-5.1

提问by Vikram Anand Bhushan

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_amountas Money field Like if anything other than money is entered it should be validated

我想验证recieved_amount为 Money 字段 就像输入金钱以外的任何内容一样,它应该被验证

Can any one help me in this

任何人都可以帮助我吗

回答by Amarnasan

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.

正则表达式将适用于诸如“12”或“12.5”或“12.05”之类的数量。如果您想要的小数点多于两个,请将“2”替换为您需要的允许小数。