Skip to content

Creating form elements for EAV attributes using the widget EavForm

iAchilles edited this page Oct 8, 2014 · 1 revision

EavForm class provides a way for dynamically building form elements. It can be used as a widget in the view file:

Yii::import('application.components.eavactiverecord.helpers.EavForm');
$this->widget('EavForm', array('model' => $model));

To rendering a form element it looks up a template file, the name of the template file should be the same as the name of the EAV-attribute. There are two variables are available in the context of every template: $attribute and $model. The value of the variable $attribute is a string that contains the name of the EAV attribute. The variable $model is reference to an instance of the class EavActiveRecord.

The following code may be used in the template file:

echo CHtml::error($model, $attribute);
echo CHtml::activeTextField($model, $attribute);

The following code may be used for rendering form elements of a multivalued EAV attribute:

echo CHtml::error($model, $attribute);
echo CHtml::activeLabel($model, $attribute);
if ($model->isEavAttributeMultivalued($attribute))
{
      if (empty($model->$attribute))
      {
         echo CHtml::activeTextField($model, $attribute . '[]', array('value' => ''));
      }
      else
      {
         foreach ($model->$attribute as $value)
         {
            echo CHtml::activeTextField($model, $attribute . '[]', array('value' => $value));
         }
      }
  }