2amigos/yiiwheels

select2

Closed this issue · 6 comments

Hi,

congrats by this great extension. Its really cool.

btw i have a problem with select2. When i have $model and attribute and, at same time, the 'asDropDownList' => false i get this error "htmlspecialchars() expects parameter 1 to be string, array given"

here is my config:

    $pluginOptions =array(
        'multiple' => 'multiple',
        'placeholder'=>GxHtml::encode($model->getRelationLabel('nMunicipioses')),
        'minimumInputLength'=>2,
        'ajax'=>array(
            'url'=> Yii::app()->controller->createUrl('/municipios/ajaxSearch'),
            'dataType'=>'json',
            'data'=>'js:function(term,page){
                                  return {
                                    q: term,
                                    page_limit: 10,
                                  };
                                }',
            'results'=>'js:function(data,page){
                                  return {results: data};
                                }',
        ),
        'formatResult'       => 'js:function(data){
                 return data.name;
             }',
        'formatSelection'    => 'js: function(data) {
                 return data.name;
             }'

    );

$this->widget('yiiwheels.widgets.select2.WhSelect2', array(
'asDropDownList' => false,
'model'=>$model,
'attribute'=>'nMunicipioses',
//'name' => 'Areas[nMunicipioses]',
'pluginOptions' => $pluginOptions
));

And if i set 'name' and unset 'model' the response is a string (csv) with the id's not and array.

i really stuck with these.

thanks

To me looks fine, I am not sure why is happening to you, maybe is due to the model attribute itself? Can you point me to the Line error? I need more debug information than just that line.

Hi Tony,

thanks for your time!

when try with this configuration:

        $this->widget('yiiwheels.widgets.select2.WhSelect2', array(
            'asDropDownList' => false,
            //'model'=>$model,
            //'attribute'=>'nMunicipioses',
            'name' => 'Areas[nMunicipioses]',
            'pluginOptions' => $pluginOptions
        ));

the problem come in the format of postback looks something like these -with the developer tools-

Areas[name]:Nom1
Areas[n_dealer_id]:1
Areas[price_add]:0
Areas[nMunicipioses]:11576,56490
yt0:Guardar

and then GxActiveRecord crash because Areas[nMunicipioses] isn't a array.

if try with this configuration:

<div class="form">


<?php $form = $this->beginWidget('GxActiveForm', array(
    'id' => 'areas-form',
    'enableAjaxValidation' => false,
));
?>

    <p class="note">
        <?php echo Yii::t('app', 'Fields with'); ?> <span class="required">*</span> <?php echo Yii::t('app', 'are required'); ?>.
    </p>

    <?php echo $form->errorSummary($model); ?>

        <div class="row">
        <?php echo $form->labelEx($model,'name'); ?>
        <?php echo $form->textField($model, 'name', array('maxlength' => 45)); ?>
        <?php echo $form->error($model,'name'); ?>
        </div><!-- row -->
        <div class="row">
        <?php echo $form->labelEx($model,'n_dealer_id'); ?>
        <?php echo $form->dropDownList($model, 'n_dealer_id', GxHtml::listDataEx(Dealer::model()->findAllAttributes(null, true))); ?>
        <?php echo $form->error($model,'n_dealer_id'); ?>
        </div><!-- row -->
        <div class="row">
        <?php echo $form->labelEx($model,'price_add'); ?>
        <?php echo $form->textField($model, 'price_add'); ?>
        <?php echo $form->error($model,'price_add'); ?>
        </div><!-- row -->

        <label><?php echo GxHtml::encode($model->getRelationLabel('nMunicipioses')); ?></label>
        <?php

        //echo $form->checkBoxList($model, 'nMunicipioses', GxHtml::encodeEx(GxHtml::listDataEx(Municipios::model()->findAllAttributes(null, true)), false, true));

        $pluginOptions =array(
            'multiple' => 'multiple',
            'placeholder'=>GxHtml::encode($model->getRelationLabel('nMunicipioses')),
            'minimumInputLength'=>2,
            'ajax'=>array(
                'url'=> Yii::app()->controller->createUrl('/municipios/ajaxSearch'),
                'dataType'=>'json',
                'data'=>'js:function(term,page){
                                      return {
                                        q: term,
                                        page_limit: 10,
                                      };
                                    }',
                'results'=>'js:function(data,page){
                                      return {results: data};
                                    }',
            ),
            'formatResult'       => 'js:function(data){
                     return data.name;
                 }',
            'formatSelection'    => 'js: function(data) {
                     return data.name;
                 }'

        );



        $this->widget('yiiwheels.widgets.select2.WhSelect2', array(
            'asDropDownList' => false,
            'model'=>$model,
            'attribute'=>'nMunicipioses',
            //'name' => 'Areas[nMunicipioses]',
            'pluginOptions' => $pluginOptions
        ));

        ?>

<?php
echo GxHtml::submitButton(Yii::t('app', 'Save'));
$this->endWidget();
?>
</div><!-- form -->

then this is the output:

PHP warning

htmlspecialchars() expects parameter 1 to be string, array given

C:\xampp\htdocs\gmp\yii\framework\web\helpers\CHtml.php(103)

091      */
092     private static $_modelNameConverter;
093 
094     /**
095      * Encodes special characters into HTML entities.
096      * The {@link CApplication::charset application charset} will be used for encoding.
097      * @param string $text data to be encoded
098      * @return string the encoded data
099      * @see http://www.php.net/manual/en/function.htmlspecialchars.php
100      */
101     public static function encode($text)
102     {
103         return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);
104     }
105 
106     /**
107      * Decodes special HTML entities back to the corresponding characters.
108      * This is the opposite of {@link encode()}.
109      * @param string $text data to be decoded
110      * @return string the decoded data
111      * @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php
112      * @since 1.1.8
113      */
114     public static function decode($text)
115     {
Stack Trace
#0  
+  C:\xampp\htdocs\gmp\yii\framework\web\helpers\CHtml.php(103): htmlspecialchars(array(), 3, "UTF-8")
#1  
+  C:\xampp\htdocs\gmp\yii\framework\web\helpers\CHtml.php(2610): CHtml::encode(array())
#2  
+  C:\xampp\htdocs\gmp\yii\framework\web\helpers\CHtml.php(158): CHtml::renderAttributes(array("name" => "Areas[nMunicipioses]", "id" => "Areas_nMunicipioses", "type" => "hidden", "value" => array()))
#3  
+  C:\xampp\htdocs\gmp\yii\framework\web\helpers\CHtml.php(2255): CHtml::tag("input", array("name" => "Areas[nMunicipioses]", "id" => "Areas_nMunicipioses", "type" => "hidden", "value" => array()))
#4  
+  C:\xampp\htdocs\gmp\yii\framework\web\helpers\CHtml.php(1609): CHtml::activeInputField("hidden", Areas, "nMunicipioses", array("name" => "Areas[nMunicipioses]", "id" => "Areas_nMunicipioses"))
#5  
–  C:\xampp\htdocs\gmp\Web\gmp\bo\protected\extensions\yiiwheels\widgets\select2\WhSelect2.php(75): CHtml::activeHiddenField(Areas, "nMunicipioses")
70         TbArray::defaultValue('name', $name, $this->htmlOptions);
71 
72         if ($this->hasModel()) {
73             echo $this->asDropDownList ?
74                 CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions) :
75                 CHtml::activeHiddenField($this->model, $this->attribute);
76 
77         } else {
78             echo $this->asDropDownList ?
79                 CHtml::dropDownList($this->name, $this->value, $this->data, $this->htmlOptions) :
80                 CHtml::hiddenField($this->name, $this->value);
#6  
–  C:\xampp\htdocs\gmp\Web\gmp\bo\protected\extensions\yiiwheels\widgets\select2\WhSelect2.php(58): WhSelect2->renderField()
53     /**
54      * Runs the widget.
55      */
56     public function run()
57     {
58         $this->renderField();
59         $this->registerClientScript();
60     }
61 
62     /**
63      * Renders the select2 field
#7  
+  C:\xampp\htdocs\gmp\yii\framework\web\CBaseController.php(173): WhSelect2->run()
#8  
–  C:\xampp\htdocs\gmp\Web\gmp\bo\protected\views\areas\_form.php(71): CBaseController->widget("yiiwheels.widgets.select2.WhSelect2", array("asDropDownList" => false, "model" => Areas, "attribute" => "nMunicipioses", "name" => "Areas[nMunicipioses]", ...))
66             'asDropDownList' => false,
67             'model'=>$model,
68             'attribute'=>'nMunicipioses',
69             'name' => 'Areas[nMunicipioses]',
70             'pluginOptions' => $pluginOptions
71         ));
72 
73         ?>
74 
75 <?php
76 echo GxHtml::submitButton(Yii::t('app', 'Save'));
#9  
+  C:\xampp\htdocs\gmp\yii\framework\web\CBaseController.php(126): require("C:\xampp\htdocs\gmp\Web\gmp\bo\protected\views\areas\_form.php")
#10 
+  C:\xampp\htdocs\gmp\yii\framework\web\CBaseController.php(95): CBaseController->renderInternal("C:\xampp\htdocs\gmp\Web\gmp\bo\protected\views\areas\_form.php", array("model" => Areas, "buttons" => "create"), true)
#11 
+  C:\xampp\htdocs\gmp\yii\framework\web\CController.php(869): CBaseController->renderFile("C:\xampp\htdocs\gmp\Web\gmp\bo\protected\views\areas\_form.php", array("model" => Areas, "buttons" => "create"), true)
#12 
+  C:\xampp\htdocs\gmp\Web\gmp\bo\protected\views\areas\create.php(19): CController->renderPartial("_form", array("model" => Areas, "buttons" => "create"))
#13 
+  C:\xampp\htdocs\gmp\yii\framework\web\CBaseController.php(126): require("C:\xampp\htdocs\gmp\Web\gmp\bo\protected\views\areas\create.php")
#14 
+  C:\xampp\htdocs\gmp\yii\framework\web\CBaseController.php(95): CBaseController->renderInternal("C:\xampp\htdocs\gmp\Web\gmp\bo\protected\views\areas\create.php", array("model" => Areas), true)
#15 
+  C:\xampp\htdocs\gmp\yii\framework\web\CController.php(869): CBaseController->renderFile("C:\xampp\htdocs\gmp\Web\gmp\bo\protected\views\areas\create.php", array("model" => Areas), true)
#16 
+  C:\xampp\htdocs\gmp\yii\framework\web\CController.php(782): CController->renderPartial("create", array("model" => Areas), true)
#17 
+  C:\xampp\htdocs\gmp\Web\gmp\bo\protected\controllers\AreasController.php(30): CController->render("create", array("model" => Areas))
#18 
+  C:\xampp\htdocs\gmp\yii\framework\web\actions\CInlineAction.php(49): AreasController->actionCreate()
#19 
+  C:\xampp\htdocs\gmp\yii\framework\web\CController.php(308): CInlineAction->runWithParams(array())
#20 
+  C:\xampp\htdocs\gmp\yii\framework\web\CController.php(286): CController->runAction(CInlineAction)
#21 
+  C:\xampp\htdocs\gmp\yii\framework\web\CController.php(265): CController->runActionWithFilters(CInlineAction, array())
#22 
+  C:\xampp\htdocs\gmp\yii\framework\web\CWebApplication.php(282): CController->run("create")
#23 
+  C:\xampp\htdocs\gmp\yii\framework\web\CWebApplication.php(141): CWebApplication->runController("areas/create")
#24 
+  C:\xampp\htdocs\gmp\yii\framework\base\CApplication.php(180): CWebApplication->processRequest()
#25 
+  C:\xampp\htdocs\gmp\Web\gmp\bo\index.php(13): CApplication->run()

Will have a closer look when i have time... apologies for the delay

I have exactly the same problem !!!!

hi @zwollander,

there's an easy workaround...

in the action at XXXXcontroller.php:

$relatedData = array(   'nCodigoPostals' => $_POST['Areas']['nCodigoPostals'] === '' ? null : $_POST['Areas']['nCodigoPostals'],);
if (is_array($relatedData['nCodigoPostals'])==false){
                $relatedData['nCodigoPostals']= explode(',',$relatedData['nCodigoPostals']);
}

I hope this will help

Solutions offered by @4ng3lp is actually the solution