Zend Forms - populate() and setDefaults()

后端 未结 4 1825
盖世英雄少女心
盖世英雄少女心 2021-02-15 11:33

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 N         


        
相关标签:
4条回答
  • 2021-02-15 11:53

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

    $data = array( 'first_name' => 'Mickey', 'last_name' => 'Mouse' );
    
    0 讨论(0)
  • FYI - in Zend_Form, $form->populate($data) just makes a call to $form->setDefaults($data).

    0 讨论(0)
  • 2021-02-15 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();
    
    $form->populate($row->toArray());
    
    0 讨论(0)
  • 2021-02-15 12:08

    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

    $form->populate($data);
    
    0 讨论(0)
提交回复
热议问题