adamwathan / bootforms

Rapid form generation with Bootstrap 3 and Laravel.
MIT License
417 stars 103 forks source link

Bug in select ? #20

Closed DominiqueFERET closed 10 years ago

DominiqueFERET commented 10 years ago

Hello,

I try to make a select with this code:

...
$formatlist=array("0" => "Select a format", "1"=>"A4", "2"=>"A3"); 
...

{{ BootForm::select('Format','format',$formatlist)}}

and the result is:

<select id="format" class="form-control" name="format">
<option value="Select a format">Select a format</option>
<option value="A4">A4</option>
<option value="A3">A3</option>
</select>

instead of

<select id="format" class="form-control" name="format">
<option value="0">Select a format</option>
<option value="1">A4</option>
<option value="2">A3</option>
</select>

Where is the mistake ? can you help me please ?

Best regards,

Dominique

adamwathan commented 10 years ago

Hey Dominique, this is because of some magic I try to do to detect associative vs. non-associative arrays.

Unfortunately in PHP there is absolutely no way to tell the difference between this:

[
  "0" => "Select a format",
  "1" => "A4",
  "2" => "A3"
];

...and just this (no keys):

[
  "Select a format",
  "A4",
  "A3"
];

Any other numbers would work fine, as long as they are not all in order and starting at 0.

BootForms tries to do you a favor and sets the value attribute to be the same as the text inside the option tag if it thinks you didn't set any custom keys, so that you don't have to type this:

[
  "Select a format" => "Select a format",
  "A4" => "A4",
  "A3" => "A3"
];

...but maybe it's not worth it. Probably going to just rip out that functionality and force you to set keys if you don't want the array indexes as your values.

DominiqueFERET commented 10 years ago

You have solved my problem. 0 is not a selection. other values ​​are an id to another table. I was just blocked because I did not understand the reason ... I just have to replace the 0 with a space and then check if the value returned is numeric to be sure that choice was made by the user.

i have tried and this work fine =>

...
$formatlist=array(" " => "Select a format", "1"=>"A4", "2"=>"A3"); 
...
{{ BootForm::select('Format','format',$formatlist)}}

So, i want to say many thanks for your help and for this great job. best regards,

Dominique

adamwathan commented 10 years ago

Glad you were able to come up with a solution and glad you are getting some use out of the package. Thanks for the kind words!

nCrazed commented 10 years ago

This is something that has caused issues for me on multiple occasions.

...but maybe it's not worth it. Probably going to just rip out that functionality and force you to set keys if you don't want the array indexes as your values.

Adding am optional flag to disable functionality would probably be a good compromise.

adamwathan commented 10 years ago

Nah I'm just gonna rip it out, we'll do it live, fuck it.

image

adamwathan commented 10 years ago

Done.

https://github.com/adamwathan/form/commit/9eecb96f9df9d14e3c6aa215c1cad89b15044ebb

Will tag a new version of Form and BootForms soon with this change!

adamwathan commented 10 years ago

Even improved my score on Scrutinizer

Wins all around!