This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
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
If you don't apply the validation, does the image get attached properly?
What version of October do you use?
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
Ok, your problem is that you are using post()
and the file data is NOT returned with this.
You must use Input::all()
instead.
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 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: ''}) }}
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
I think this is the PR that fixed this: https://github.com/octobercms/october/issues/5270
So you need v470+
This thread was for the same problem: https://octobercms.com/forum/post/error-uploading-a-file-from-frontend-and-associating-it-to-a-model
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 ?
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 ?
what do you get if you do this in your onUpload() metod?
trace_log(Input::all());
Last updated
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 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);
}
...
}
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.