1

In my model I have attribute - specifications:

    class Category extends CActiveRecord
    {
        private $_specifications = array();

        public function getSpecifications()
        {
                    return $this->_specifications;
        }

        public function setSpecifications($specifications)
        {
                    $this->_specifications = implode(', ', $specifications);
        }

So I want specifications to be an array.

My view file:

<div id="specifications" class="row">
    <?php echo $form->labelEx($model,'specifications'); ?>
    <?php echo $form->textField($model,'specifications',array('rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][0]', 'class' => 'clonedInput')); ?>
    <?php echo $form->textField($model,'specifications',array('rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][1]', 'class' => 'clonedInput')); ?>
    <?php echo $form->error($model,'specifications'); ?>
</div>

When I send form I get an error:

htmlspecialchars() expects parameter 1 to be string, array given
...
public static function encode($text)
84     {
85         return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);
86     }

I've tried to disable encoding:

<?php echo $form->textField($model,'specifications',array('encode'=>false, 'rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][0]', 'class' => 'clonedInput')); ?>
<?php echo $form->textField($model,'specifications',array('encode'=>false, 'rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][1]', 'class' => 'clonedInput')); ?>

But in that case it's another error:

Array to string conversion
...
2216                 $html .= ' ' . $name . '="' . ($raw ? $value : self::encode($value)) . '"';

Can anybody give an advice, what should I do to pass an array from form? Thanks.

3 Answers 3

1

How can you pass an array to a single textfield? What should it display?

You can can create a virtual attribute for that.

In the model:

private $_specifications = array();

public function getSpecifications()
{
    return implode(', ', $this->_specifications);
}

The view can remain untouched.

Edit:

Of course you need a setter too, if you want to be able to write to the attribute.

public function setSpecifications($specifications)
{
    $this->_specifications = explode(', ', $specifications);
}

Please refer to http://www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-get-set-methods/

Sign up to request clarification or add additional context in comments.

10 Comments

So then where should I put calling this function?
Nowhere, because of that i wrote The view can remain untouched.;) Yii automatically checks for variables and for virtual attributes (getXyz) when you specify them. You can also write $model = new MyModel(); echo $model->specifications;
implode(): Invalid arguments passed
I've added: if (isset($this->_specifications)) Got an error: Property "Category.specifications" is read only.
Can you update your post with the new code and updated methods?
|
0

Since your specifications attribute is an array, you should simply make a loop to display corresponding inputs, e.g. :

foreach ($model->specifications as $s)
{
  echo Chtml::textField('Category[specifications][]', $s, array('rows'=>6, 'cols'=>50, 'class' => 'clonedInput'));
}

Comments

0
<?php echo $form->textField($model,'specifications',array('encode'=>false, 'rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][0]', 'class' => 'clonedInput')); ?>
<?php echo $form->textField($model,'specifications',array('encode'=>false, 'rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][1]', 'class' => 'clonedInput')); ?>

I think the specifications will be displayed twice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.