Hamed Yarandi
Hamed Yarandi

Reputation: 1211

Laravel : Need to dynamic validation rules (read from config file or setting)

I have a request rule like this :

public function rules()
{
    return [
        'title' => 'required',
        'recipients' => 'required',
        'attachments' =>'mimes:jpeg,png,pdf,doc,xls|max:10410',
    ];
}

So I'm looking for a way to make rules dynamic, with read config file or read from database.

For instance :

I have made a helper function named setting , it can load setting from my DB and i want to read this data and set on my rule like this :

public function rules()
    {
        $max_upload_size = setting('max_document_upload_size'));
        return [
            'title' => 'required',
            'recipients' => 'required',
            'attachments' =>'mimes:jpeg,png,pdf,doc,xls|max:$max_upload_size',
        ];
    }

Is it possible or what should i do for cover this?

Thanks in advance.

Upvotes: 2

Views: 769

Answers (2)

Илья Зелень
Илья Зелень

Reputation: 8108

This question refers to PHP, not Laravel. There are many options to combine a string with a variable.

If you need to have a lot of settings on the string, then you can use this syntax:

return [
    'title' => 'required',
    'recipients' => 'required',
    'attachments' => "mimes:$mimes|max:$max_upload_size"
]

Upvotes: 0

parastoo
parastoo

Reputation: 2469

please write after max : '.

 public function rules()
        {
            $max_upload_size = setting('max_document_upload_size'));
            return [
                'title' => 'required',
                'recipients' => 'required',
                'attachments' =>'mimes:jpeg,png,pdf,doc,xls|max:'.$max_upload_size',
            ];
        }

Upvotes: 1

Related Questions