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

[Phalcon 2.0.3]When using a multiple select, will be an "PHP Notice: Array to string conversion" #10543

Closed
hanfeng2000 opened this issue Jun 24, 2015 · 1 comment

Comments

@hanfeng2000
Copy link

My Scripts

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
Contributor

Fixed in the 2.0.x branch, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants