2009-08-19 18:45 阅读 16

Zend Forms - populate()和setDefaults()

Let's say I have a form that collects a first name and a last name:

$first_name = new Zend_Form_Element_Text('first_name');
$first_name->setLabel("First Name")

$last_name = new Zend_Form_Element_Text('last_name');
$last_name->setLabel("Last Name")

$form = new Zend_Form();

If I want to use the "populate($data)" or the "setDefaults($data)" method on the form, how does the array need to be organized? What kind of an array do these functions expect? I haven't been able to find this information in the docs.

Also, I know that I can set the value when creating the element itself, but this is not what I need.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    dqt66847 dqt66847 2009-08-31 21:11

    Array keys are the field names, array values are the field values.

    $data = array( 'first_name' => 'Mickey', 'last_name' => 'Mouse' );
    点赞 评论 复制链接分享
  • doucongmishang2385 doucongmishang2385 2009-08-31 12:02

    The form->populate() method takes an array where the keys are the names of the form fields.

    The Zend_Db_Table_Row object implements a toArray() method which can be used here (as do many other objects). So you can do stuff like:

    $form = new MyForm;
    $table = new MyTable;
    $rowset = $table->find($id);
    $row = $rowset->current();
    点赞 评论 复制链接分享
  • dsgrgaz321973284 dsgrgaz321973284 2009-11-11 03:11

    FYI - in Zend_Form, $form->populate($data) just makes a call to $form->setDefaults($data).

    点赞 评论 复制链接分享
  • duanci9305 duanci9305 2014-12-22 14:39

    simple, create an array

    $data = array('nameInput'=> 'your value');

    Add your form to your View

    $this->view->form = $form;

    then you add data to the form

    点赞 评论 复制链接分享