Search code examples
laravelvalidationrule

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.


Solution

  • 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',
                ];
            }