This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

lucas.sanner54070
lucas.sanner54070

Here:

class Member extends Model
{       
    use \October\Rain\Database\Traits\Validation;

    ...

    public $rules = [
        'attestation' => 'required',
    ];

    ...

    public $attachMany = [
        // Deletes the linked files once a model is removed.
        'attestations' => ['System\Models\File', 'order' => 'created_at desc', 'delete' => true]
    ];

    ...
}

I also tried :

    public $rules = [
        'attestation' => 'required|mimes:jpg,jpeg,png,gif',
    ];

but it doesn't change anything.

mjauvin
mjauvin

I am using a very simple test page that replicates the same thing, without a component, and it's working as expected. Try this and let me know:

title = "my form"
url = "/form"
layout = "default"
is_hidden = 0
==
function onUpdate()
{
    $data = Input::all();
    $rules = [
        'file_name' => 'required',
    ];

    $validation = Validator::make($data, $rules);
    if ($validation->fails()) {
        throw new ValidationException($validation);
    }
    Flash::success('Update success!');
}
==
<form data-request="onUpdate" data-request-files data-request-flash>
    {{ form_file('file_name') }}
    {{ form_submit('Submit') }}
</form>
lucas.sanner54070
lucas.sanner54070

Your code works fine indeed.
After many research I finally found out the problem.
I think I should have shown you the rest of the code in the onUpdate function

public function onUpload()
{   
    $rules = (new Member)->rules;

    $validation = Validator::make(Input::all(), $rules);
    if ($validation->fails()) {
        throw new ValidationException($validation);
    }

    $member = $this->loadMember();

    if (Input::hasFile('attestation')) {
        $member->attestations = Input::file('attestation');
        $member->save(); // <= THE PROBLEM IS COMING FROM HERE
    }

}

It looks like the save() method checks the rules again or something similar and makes the validator fail.
To prevent this behavior I replaced the save() method with the forceSave() method and now it works.
It was a tough one !
Thanks for your help and your patience.

21-23 of 23

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.