CakePHP-Bootstrap / cakephp3-bootstrap-helpers

CakePHP 3.x Helpers for Bootstrap 3 and 4.
https://holt59.github.io/cakephp3-bootstrap-helpers/
MIT License
130 stars 79 forks source link

fix for issue with fancy file input where trying to convert array to string #158

Closed lyziane closed 2 years ago

Holt59 commented 6 years ago

This PR needs a bit more background, I don't understand its purpose: What is the original bug? In which context does it happen?

This fix seems also weird, the val attribute has no HTML meaning, why would you use it here instead of value?

lyziane commented 6 years ago

Well I didn't write this fancy input thing, I'm just trying to use it without having a bunch of errors popping up everywhere. It is all written to handle the upload of just one single file so I edited it before, months ago, to make the label show the right thing but there are still errors with it.

The problem I'm fixing this timeis that it handles the array as if it was a single item so I'm just doing a check to see if it's an array.

The val vs value, I'm not completely sure what's going on there. I was hoping you, master of this project, would tell me, haha. But seeing what happens when I edit both this is what I assume: I think val is just an extra attribute created to contain the label without replacing the real value. Anyway the important one is the value on the real input, which is not changed (and I tested to make sure it stays).

TLDR: val is just the text we see in the fake upload box, I edited it so it doesn't show an error when uploading multiple files (Array to string conversion...)

Holt59 commented 6 years ago

Apart from the unset($data['val']);, this PR only change the name of the attribute on the fake <input type="text" /> box from value to val, which seems wrong to me (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/text).

Are you using a custom template for 'fancyFileInput'? The problem may come from this, especially if you've added {{attrs}} to the template. The default template is:

{{fileInput}}
<div class="input-group">
    <div class="input-group-btn">{{button}}</div>
    {{input}}
</div>'
lyziane commented 6 years ago

So I went back into a brand-new-clean-untouched cake project (v3.4 because that's what my real project uses) in order to show you what really happens. I took some screenshots: https://imgur.com/a/KVshee8

So in the first one, I'm not using anything special, only the regular cakephp. In the second one, I'm only loading the Html and the Form helper from this plugin witht the option 'useCustomFileInput' => true. In the third screenshot, my fix has been applied.

When I do unset($data['val']);, it gets rid of the array to string conversion notices. Changing $fakeInputCustomOptions['value'] to $fakeInputCustomOptions['val'] is how the label appears in the field. There is still the key '-text' in my upload array that is annoying but I didn't have the time to go look for it. I just unset it manually before doing anything with my uploads.

I realize that I'm working blind since I don't know why my fix even works but... yeah I needed this fixed. Does this help?

Holt59 commented 6 years ago

I cannot reproduce, except for the -text in the request data. I tried a clear CakePHP 3.4.13 installation, with the helpers version 3.1.2 and the following:

$this->Form->setConfig('useCustomFileInput', true);
echo $this->Form->create(null, ['type' => 'file']);
echo $this->Form->control('uploads.', ['type' => 'file', 'multiple']);
echo $this->Form->submit('Upload');
echo $this->Form->end();
debug($this->request->getData());

I do not get any warning about "Array to string" conversions... Could you add details about the code that generates such warnings?

lyziane commented 6 years ago

The difference between your code and mine is the name of the input: uploads. vs uploads[]. In both cases, we have the same result except with yours there is no array to string conversion... but the fancy upload field still shows as empty in your version too.

Holt59 commented 6 years ago

I can reproduce with uploads[] and I do understand the unset('data'), a better way would be to pass ['val'] as the second argument to formatAttributes.

I will have a hard time getting rid of the -text unfortunately I think, I could however put it outside of the uploads array.

The upload field fill always show empty after an upload I think, it's the case with the default upload field. If you want something that "stays", you need to do some tricks. This can be misleading for users because the input will say "3 files selected" but when they'll click upload, nothing will be uploaded (since post-data are not kept).