为非必填字段应用数字验证

问题描述 投票:0回答:1

我想对表单输入字段应用数字验证。我为此使用了单独的请求类。我尝试过somtimes nullable,但他们没有给我答案。即使我没有应用required验证该字段不会让我空白。我展示了与此问题有关的一些问题。但是他们没有给出答案。我正在使用laravel 5.7.28

我正在使用的规则如下

    {

        $rules = [
        'transaction_rate_type' => ['required'],
        'discount_in_percentage' => ['numeric', 'between:0,99.99'],
        'account_type' => ['required'],

        ];

        return $rules;
    }


migration is

public function up()
    {
        Schema::create('billing_programmes', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('transaction_rate_type');
            $table->decimal('discount_in_percentage');
            $table->integer('account_type');
            $table->uuid('created_by');
            $table->uuid('updated_by');
            $table->foreign('transaction_rate_type')->references('id')->on('transaction_rate_types');
            $table->foreign('created_by')->references('id')->on('users');
            $table->foreign('updated_by')->references('id')->on('users');
            $table->timestamps();
        });
    }
laravel
1个回答
0
投票

有时我使用正则表达式验证,尤其是特殊字符和数字

$rules = [
        'transaction_rate_type' => ['required'],
        'discount_in_percentage' => ['regex:/^[0-9]+$/', 'between:0,99.99'],
        'account_type' => ['required'],

];

希望此帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.