PhalconPHP: How to set a Selected option in SELECT tag

拈花ヽ惹草 提交于 2020-01-13 07:03:45

问题


I want to set the <option> with the value coming from Db. So far I tried this but not working:

$site_select = new Select('site_id', $this->_sites);

回答1:


Update

Generating the select using DB columns as Timothy recommended:

new Select('site_id', Sites::find(), array('using' => array('site_id', 'site_name')));

Setting the selected value of the given select:

$site_select->setDefault('YOUR_DB_VALUE');

However there is another lovely trick about Phalcon forms. You could pass your DB entity to the form class and your form will be auto populated.

Form:

class YourFormClass extends Phalcon\Forms\Form
{
    public function initialize($entity = null, $options = null)
    {

Controller:

$entity = YourModel::findFirst();
$form = new YourFormClass ($entity, $options);

Note that Form input names must match DB/Model columns.



来源:https://stackoverflow.com/questions/38239846/phalconphp-how-to-set-a-selected-option-in-select-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!