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

I'm having a problem with the validation rules regarding file uploading.

Model:

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]
    ];

    ...
}

Component:

class Account extends ComponentBase
{   

    ...

    public function onUpdate()
    {   
        $data = post();
        $rules = (new Member)->rules;

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

        parent::onUpdate();
    }

    ...

}   

Form:

...

 <div class="form-group">
   <label for="inputAttestation">Attestation</label>
   <input type="file" name="attestation" class="form-control" id="inputAttestation">
 </div>

...

If I don't select any file the rule is applied and prevent the form to be sent. So far so good.
But now if I select a file, the rule is applied anyway and the form still can't be sent.
Is there a specific way to set the validation rules when it comes to file uploading ?

Last updated

mjauvin
mjauvin

If you don't apply the validation, does the image get attached properly?

What version of October do you use?

mjauvin
mjauvin

Also, make sure you have the "files" = true parameter on your form, something like:

{{ form_ajax('onUpdate', {files:true}) }}

Or, using this format:

<form data-request="onUpdate" data-request-files data-request-flash>

Last updated

mjauvin
mjauvin

Ok, your problem is that you are using post() and the file data is NOT returned with this.

You must use Input::all() instead.

lucas.sanner54070
lucas.sanner54070

If you don't apply the validation, does the image get attached properly?
Yes, everything works fine.
What version of October do you use?
469
You must use Input::all() instead
I replaced post() by Input::all(), but unfortunately it still doesn"t work.
Any idea ?

mjauvin
mjauvin

did you add data-request-files to the form as I wrote?

lucas.sanner54070
lucas.sanner54070

mjauvin said:

did you add data-request-files to the form as I wrote?

Yes, I did it that way:

{{ form_ajax('onUpload', {files: 1, flash: ''}) }}

mjauvin
mjauvin

try with the second format to be sure.

If not, it may be you need to update october to latest build 471, I think there was a bug with this around version 469

<form data-request="onUpload" data-request-files data-request-flash>

Last updated

mjauvin
mjauvin

I think this is the PR that fixed this: https://github.com/octobercms/october/issues/5270

So you need v470+

mjauvin
lucas.sanner54070
lucas.sanner54070

mjauvin said:

try with the second format to be sure.

If not, it may be you need to update october to latest build 471, I think there was a bug with this around version 469

It doesn't work either with the second format.
In the settings it reads CURRENT BUILD 469 and No new updates were found. when I click the refresh button.
How can I update october to latest build 471 ?

mjauvin
mjauvin

You need to set edgeUpdates to true in config/cms.php

lucas.sanner54070
lucas.sanner54070

Thanks for the tip. I'm gonna try with the latest build. I'll keep you informed.

lucas.sanner54070
lucas.sanner54070

The problem still occurs even with the latest build.
I really can't figure out where this problem comes from, I have checked everything.
Do you think it's a bug ?

mjauvin
mjauvin

what do you get if you do this in your onUpload() metod?

trace_log(Input::all());

Last updated

lucas.sanner54070
lucas.sanner54070

mjauvin said:

what do you get if you do this in your onUpload() metod?

trace_log(Input::all());
Array
(
    [_session_key] => 5zTaiH3tKMaE7cxUxY5dVUameVvJk9J2z7RBKRej
    [_token] => SWmgmF0Y9snMaFFzhvJKSoNQz7f8ubXalnDdVnNl
    [attestation] => Illuminate\Http\UploadedFile Object
        (
            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>     my-attestation.jpg
            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 21279
            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
            [hashName:protected] => 
            [pathName:SplFileInfo:private] => /tmp/phpbCkFtY
            [fileName:SplFileInfo:private] => phpbCkFtY
        )
)
mjauvin
mjauvin

Ok, then this should work, everything is in there... show your onUpload() code again?

lucas.sanner54070
lucas.sanner54070

mjauvin said:

Ok, then this should work, everything is in there... show your onUpload() code again?

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

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

    ...   

}
mjauvin
mjauvin

Can you show the rules?

mjauvin
mjauvin

Either I'm missing something or there is a problem with 1.0 branch.

I suggest upgrading to the new stable branch (1.1.x) using composer, it's the way forward now.

1-20 of 23

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