dongtu9823 2012-05-03 14:23 采纳率: 0%
浏览 7
已采纳

通过控制器的引用设置Zend_Form中元素的默认值

Via the command line I set up a new form called BookSlot:

zf create form BookSlot

Which most of you may know, creates a form directory in the application directory of the Zend framework.

In the init method of the form, I added a text element called time and given it the label 'time':

$time = new Zend_Form_Element_Text('time');
$this->addElement($time);
$time->setLabel('Time');

I need it to have a default value that cannot be changed by the user so I add:

$time->setValue($value);

In my controller I create a function to get the book slot form above:

public function getBookSlotForm(){
        return new Application_Form_BookSlot();
    }

I also have a book slot action in my index controller.

I assign the form to a variable and make it passable to the view in the book slot action:

$form = $this->getBookSlotForm();
    $this->view->form = $form;

In the same book slot action, I have an id variable which is dynamically generated but for this assume its 5:

$id = 5;

How do I get the value of $id to be the default value of $time in my book slot form above? Also, I want to make it so that the user cannot change this value.

  • 写回答

1条回答 默认 最新

  • drdawt9210 2012-05-03 14:40
    关注

    For the user can not change the value use the readOnly attribute. In your form class

    $time->setAttrib('readonly', 'readonly');
    

    To assign your id as the default value of your time field, in your action

    $form->time->setValue($id);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路