Support the ongoing development of Laravel.io →
Requests Input Validation
Last updated 1 year ago.
0

I found a partial answer to my question:

The code of the Form Request class:

public function rules()
{
    $request = $this->instance()->all();
    $rules = [];
    $images = $request['files'];
    $images_rules = 'required|image|mimes:jpeg,png|max:5120';
    if (count($images) > 0) {
        foreach ($images as $key => $image) {
            $rules['files.'.$key] = $images_rules;
        }
    }
    return $rules;
}

protected function createMessages()
{
    $request = $this->instance()->all();
    $messages = [];
    $images = $request['files'];
    if (count($images) > 0) {
        foreach ($images as $key => $image) {
            $messages['files.'.$key.'.required'] = 'Select files!';
            $messages['files.'.$key.'.image'] = 'Only images!';
            $messages['files.'.$key.'.mimes'] = 'Only jpeg and png!';
            $messages['files.'.$key.'.size'] = 'File max 5M';
        }
    }
    return $messages;
}

public function messages()
{
    return $this->createMessages();
}

The rules works fine. I have a problem with errors messages: in my view I get an error message only when input is empty (required error messsage). When the other rules are not respected I get no message.

I have tested this system with an other input array (a select list). This work fine also for messages.

Have you an idea about this problem?

Thanks.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

MWDD5 mwdd5 Joined 9 Feb 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.