简体   繁体   English

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

[英]Set default value of an element in Zend_Form by reference from the controller

Via the command line I set up a new form called BookSlot: 通过命令行,我建立了一个名为BookSlot的新表单:

zf create form BookSlot

Which most of you may know, creates a form directory in the application directory of the Zend framework. 你们大多数人可能知道的是,在Zend框架的应用程序目录中创建一个表单目录。

In the init method of the form, I added a text element called time and given it the label 'time': 在表单的init方法中,我添加了一个名为time的文本元素,并为其指定了标签“ 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:

$id = 5;

How do I get the value of $id to be the default value of $time in my book slot form above? 在上面的书位表格中,如何获取$id的值作为$time的默认值? Also, I want to make it so that the user cannot change this value. 另外,我要使它不能使用户更改此值。

For the user can not change the value use the readOnly attribute. 对于不能更改该值的用户,请使用readOnly属性。 In your form class 在您的班级上

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

To assign your id as the default value of your time field, in your action 要在操作中将ID分配为时间字段的默认值

$form->time->setValue($id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM