Hi all.
I couldn't get select options with selected attribute for my model. Simple example (I omitted some code for brevity):
<?php
// Form
class AttributeForm extends \Phalcon\Forms\Form
{
public function initialize($entity, $options)
{
$group = new Select('groups[]', Attrgroup::find(), [
'using' => ['id', 'name'],
'class' => 'form-control',
'multiple' => 'multiple',
'size' => 5
]);
$group->setLabel('Attribute Group');
$this->add($group);
}
public function open()
{
$html = '';
foreach ($this->getElements() as $element) {
$html .= $element->render();
}
return $html;
}
}
// Controller
class AttributeController extends \Phalcon\Mvc\Controller
{
public function updateAction($id)
{
$model = Attribute::findFirst($id);
$form = new AttributeForm($model, ['edit' => true]);
if ($this->request->isPost()) {
// validation & saving
} else {
$form->setEntity($model);
}
$this->view->setVars([
'form' => $form,
'attribute' => $model
]);
}
}
// Model
class Attribute extends \Phalcon\Mvc\Model
{
public function initialize()
{
self::setup(['notNullValidations' => false]);
$this->hasManyToMany(
'id',
__NAMESPACE__ . '\AttrgroupsAttributes',
'attribute_id', 'group_id',
__NAMESPACE__ . '\Attrgroup',
'id',
['alias' => 'groups']
);
}
}
Okay. This work fine. But select options don't have selected attribute. View example:
{{ form('class': 'form-horizontal', 'role' : 'form', 'accept-charset' : 'utf-8') }}
{{ form.open() }}
{{ submit_button('Save', 'class': 'btn btn-primary') }}
{{ end_form() }}
How to assign selected
attribute for related options?