Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [Phalcon 1.3.3] [PHP 5.4+] bug in populating (setDefault) for Select element #2921

Closed
samuraee opened this issue Oct 19, 2014 · 8 comments
Labels
bug A bug report status: medium Medium

Comments

@samuraee
Copy link

Phalcon 1.3.3 / PHP 5.5 / Ubuntu 14.04
I've created an element in my form:

$status = new Select("status",
    [0 => 'Disable', 1 => 'Enable', 2 => 'Suspend'] , [
        'class'      => 'form-control',
        "useEmpty"   => true,
        'emptyValue' => '@'
    ]);

then I populated the element:

$status->setDefault('@');

But the option with 0 value selected as default!!!
$form->render('status') output:

<select id="subscriber_status" name="subscriber_status" class="form-control">
    <option value="@">Choose...</option>
    <option selected="selected" value="0">Disable</option>
    <option value="1">Active</option>
    <option value="2">suspend</option>
</select>

I've tried many conditions but it seems that if we have an option with 0 value like my Disable option setting default value for Select element didn't works.

I removed 0 => Disable option from my values array and everything works correctly. like below:

<select id="subscriber_status" name="subscriber_status" class="form-control">
    <option selected="selected"  value="@">Choose...</option>
    <option value="1">Active</option>
    <option value="2">suspend</option>
</select>

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@duythien
Copy link
Contributor

Maybe this is a bug

@samuraee
Copy link
Author

I've tested many different conditions and I'm sure this is a bug.

@samuraee samuraee changed the title Bug in populating (setDefault) for select Element (Phalcon 1.3.3) [BUG] [Phalcon 1.3.3] [PHP 5.4+] bug in populating (setDefault) for Select element Oct 20, 2014
andresgutierrez added a commit that referenced this issue Jun 5, 2015
@andresgutierrez
Copy link
Sponsor Contributor

This is fixed in 2.0.3

@hanfeng2000
Copy link

multiple select in 2.0.3,have "PHP Notice: Array to string conversion"

@andresgutierrez
Copy link
Sponsor Contributor

@hanfeng2000 Could you please provide a script to reproduce the issue?

@hanfeng2000
Copy link

FirmsForm.php:

$categories = new Select('categories', Constants::$FirmCategories, [
            'id' => 'categories',
            'name' => 'categories[]',
            'multiple' => true,
            'placeholder' => 'Multiple Select'
        ]);
$categories->setLabel('<i class="fa fa-star-o red"></i> Categories);
$this->add($categories);

firms.volt:

<div class="form-group">
    {{ form.label("categories",['class': 'col-sm-3 control-label no-padding-right']) }}
    <div class="col-sm-4">
        {{ form.render("categories",['class': 'form-control select2']) }}
    </div>
</div>

FirmsModel.php:

    public function afterFetch()
    {
        $this->categories = explode(',', $this->categories);

        $this->categories = array_combine($this->categories, $this->categories);

        $this->introduction = htmlspecialchars($this->introduction);
    }

FirmsController.php:

        if (!$this->request->isPost()) {

            $firm = Firms::findFirst(.....);

            if (!$firm) {
                return $this->response->redirect('/firms/index');
            }

            //var_dump($firm->categories);
            $this->view->form = new FirmsForm($firm);
            return;
        }

phalcon/tag/selec.zip (corrected)

        /**
         * Generate the OPTION tags based on an array
         *
         * @param array data
         * @param mixed value
         * @param string closeOption
         */
        private static function _optionsFromArray(data, value, closeOption)
        {
                var strValue, strOptionValue, code, optionValue, optionText, escaped;

                let code = "";

                for optionValue, optionText in data {

                        let escaped = htmlspecialchars(optionValue);

                        if typeof optionText == "array" {
                                let code .= "\t<optgroup label=\"" . escaped . "\">" . PHP_EOL . self::_optionsFromArray(optionText, value, closeOption) . "\t</optgroup>" . PHP_EOL;
                        } else {
                                if typeof value == "array" {
                                        if in_array(optionValue, value) {
                                                let code .= "\t<option selected=\"selected\" value=\"" . escaped . "\">" . optionText . closeOption;
                                        } else {
                                                let code .= "\t<option value=\"" . escaped . "\">" . optionText . closeOption;
                                        }
                                } else {
                                        let strOptionValue = (string) optionValue,
                                                strValue = (string) value;
                                        if strOptionValue === strValue {
                                                let code .= "\t<option selected=\"selected\" value=\"" . escaped . "\">" . optionText . closeOption;
                                        } else {
                                                let code .= "\t<option value=\"" . escaped . "\">" . optionText . closeOption;
                                        }
                                }
                        }
                }

                return code;
        }

@andresgutierrez
Copy link
Sponsor Contributor

@hanfeng2000 Could you please create a new issue? Thanks

@hanfeng2000
Copy link

@andresgutierrez

#10543

Sorry! My English is poor.

@niden niden added bug A bug report status: medium Medium and removed Bug - Medium labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: medium Medium
Projects
None yet
Development

No branches or pull requests

5 participants