php - Placing a widget within Yii's CActiveForm loses CActiveForm functionality -
i use cactiveform::dropdownlist()
in many, many places. speed development i'd write widget encapsulates related work. have displays correctly loses validation functionality of cactiveform
.
here's how create dropdown directly. $activemodel
refers model (cactiverecord
) used cactiveform
. $allitems
cactiverecord
array used populated dropdown. code works perfectly.
<div class="row"> <?php echo $form->labelex($activemodel, 'keyid'); ?>: <?php $data = array(); foreach ($allitems $item) { $data[$item->keyid] = chtml::encode($item->keyname); } $options = array( 'prompt' => 'select item', 'options' => array($activemodel->keyid => array('selected' => true)), ); echo $form->dropdownlist($activemodel, 'keyid', $data, $options); ?> <?php echo $form->error($activemodel, 'keyid'); ?> </div>
theoretically, here's how widget included in cactiveform
:
<div class="row"> <?php echo $form->labelex($activemodel, 'keyid'); ?> <?php $this->widget('path.to.customdropdown', array( 'form' => $form, 'items' => $allitems, 'model' => $activemodel, 'prompt' => 'select item', 'selected' => true, )); ?> <?php echo $form->error($activemodel, 'keyid'); ?> </div>
the widget looks this:
<?php /** * echoes populated <select> element */ class customdropdown extends cwidget { public $form; public $items = array(); public $model; public $prompt; public $selected = false; /** * @var cactiveform $form * @var cactiverecord[] $items * @var cactiverecord $model * @var string $prompt * @var bool $selected (optional) */ public function run() { $data = array(); foreach ($this->items $item) { $data[$item->{$item->tableschema->primarykey}] = chtml::encode($item->getname()); } $options = array( 'prompt' => chtml::encode($this->prompt), 'options' => ( $this->selected ? array($this->model->{$item->tableschema->primarykey} => array('selected' => true)) : array() ), ); echo $this->form->dropdownlist($this->model, $this->model->tableschema->primarykey, $data, $options); } } ?>
all of works, cactiveform
loses validation functionality in dropdown. specifically, widget's $form
not mapped cactiveform's validation array, submission error never displayed user.
how integrate type of widget cactiveform
no functionality lost? help.
judging example, calling widget causes writing more code, in $form->dropdownlist()
call. recommend simplify pieces of code , keep using widgetless solution.
why need code?
$data = array(); foreach ($allitems $item) { $data[$item->keyid] = chtml::encode($item->keyname); } $options = array( 'prompt' => 'select item', 'options' => array($activemodel->keyid => array('selected' => true)), );
you repeating chtml::listdata()
does. can create drop-down this:
echo $form->dropdownlist( $activemodel, 'keyid', chtml::listdata( myactiverecordfordropdown::model()->findall(), 'valuefield', 'textfield' ), array( 'prompt' => 'select item' ) );
and selected item selected automatically model attribute value.
Comments
Post a Comment