0

I have a form :

$houserent = new Zend_Form_Element_Text('houserent');

$houserent      ->setLabel('House Rent :');
$this   ->addElement($houserent);

$tax = new Zend_Form_Element_Text('tax');
$siteName        ->setLabel('Tax :');
$this    ->addElement($tax);

$internet = new Zend_Form_Element_Text('internet');
$internet->setLabel('Internet :');
$this    ->addElement($internet);

and my data table "test" is

id   name         value
1   house rent    100
2   tax            10
3   internet       10

I want to populate the form using the above data from tha database table. But I don't know how to this. Please help me. Thanks

2 Answers 2

1

You can overwrite the populate function in your form.

public function populate($data)
{
  foreach($data as $field => $value)
  {
    $this->{$field}->setValue($value);
  }
  return $this;
}

where $data is an associated array of name => value.

[edit]

So you form is now:

<?php
  class Form_Admin_Settings_Add extends Zend_Form
  {
    public function init()
    {
      $houserent = new Zend_Form_Element_Text('houserent');
      $houserent->setLabel('House Rent :');
      $this->addElement($houserent);

      $tax = new Zend_Form_Element_Text('tax');
      $siteName->setLabel('Tax :');
      $this->addElement($tax);

      $internet = new Zend_Form_Element_Text('internet');
      $internet->setLabel('Internet :');
      $this->addElement($internet);
    }

    public function populate($data)
    {
      foreach($data as $field => $value)
      {
        $this->{$field}->setValue($value);
      }
      return $this;
    }
  }

In your controller:

<?php
  //instantiate form and model
  $form = new Form_Admin_Settings_Add();
  $model = new Model_Test();

  //get results
  $results = $model->fetchAll()->toArray();
  $data = array();

  //put results into our data array as name => value
  foreach($results as $r)
  {
    $data[$r['name']] = $r['value'];
  }

  //populate our form
  $form->populate(data);
  echo $form;
Sign up to request clarification or add additional context in comments.

4 Comments

If I add your code in my form class I gives the following errors. As I'm novice in zend can you describe it little bit detail. Strict Standards: Declaration of Form_Admin_Settings_Add::populate() should be compatible with that of Zend_Form::populate() in /opt/lampp/htdocs/BusinessForum/application/forms/Admin/Settings/Add.php on line 3 Fatal error: Call to a member function setValue() on a non-object in /opt/lampp/htdocs/BusinessForum/application/forms/Admin/Settings/Add.php on line 49
The first error suggests that the function declaration is different to that in Zend_Form. Looking at Zend Framework 1.11, the functions look the same (public function populate($data)). What version of Zend Framework are you using? The second error message is that the form element did not exist in the form, which I may have resolved in the updated answer?
Parnaby -King , can please tell me is it possible to write populate function inside controller instead of form class. Thanks
Looking at my supplied answer, you do not need to write the populate function in the form as it is all being done in the controller.
0

Try some like this:

class Form extends Zend_Form {

   public funcition Form(){
      $houserent = new Zend_Form_Element_Text('houserent');
      $houserent->setLabel('House Rent :')
                ->setValue($this->_Data['houserent']);
      $this->addElement($houserent);
   }

   public function setData($Data){
        $this->_Data = $Data;
        return $this;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.