1

I have a form in Zend Framework 1. when I click on edit button I want to display values from databases in the form. but I don't know how to do it.

This is my form code:

    // Add an email element
    $this->addElement('text', 'orgname', array(             
        'required'   => true,
        'filters'    => array('StringTrim'),
        'style'    => array('width:220px'),
        'decorators'=>Array(
        'ViewHelper','Errors'
       )                      
    ));

This is my controller:

 public function editclientcompanyAction()

    $form = new Application_Form_Companyform();
    $form->addform();
    $this->view->form = $form;
    $request = $this->getRequest();
    $editid=$request->getParam('id');
    $edit_show = new Application_Model_Clientcompany;
    $showdetails = $edit_show->editclient($editid);


    $this->view->assign('datas', $showdetails);

How do I display database vlaues in my Zend Form?

3 Answers 3

3

There are two cases.

1) Populating form which has fields same like the database table fields : If you have the form which has same fields like the database fields, then you can populate them easily. First you need to get the data from the database and then call the Zend_Form populate function passing the data as an associative array, where keys will be same like form fields names and values will be values for the form fields, as below in case of your form.

This will be in your controller

$data = array("orgname" => "Value for the field");
$form = new  Application_Form_Companyform();
$form->populate($data); 

Now send will automatically populate the form field orgname. You dont need to modify your form or set the value field in the addElement.

*2)Setting field value manually: * The second case is to set the value manually. First you will need to modify your form and add a constructor to it. Also in your form class you will need to create a property (if you have multiple fields, then you can create an array property or multiple properties for each field. This will be all up to you.). And then set the value key in the addElement. Your form should look like this

class Application_Form_Companyform extends Zend_Form
{
   private $orgname;

   public function __contruct($orgname)
   {
      $this->orgname = $orgname;

      //It is required to call the parent contructor, else the form will not work
      parent::__contruct();
    }

    public function init()
    {
       $this->addElement('text', 'orgname', 
                       array(             
                              'required'   => true,
                              'filters'    => array('StringTrim'),
                              'style'    => array('width:220px'),
                              'decorators'=>Array('ViewHelper','Errors'),
                              'value'=>$this->orgname
                             )       
                          ));
    } //end of init

 } //end of form

Now your controller, you will need to instantiate the form object passing the value of the orgname field like below

$form = new Application_Form_Companyform("This is the value for orgname");

And thats it.

I used such methods and it works like a charm. For your requirements, you may need to adjust the above sample code, as i did not checked it, but it will run fine for sure i hope :P

Thank you

Sign up to request clarification or add additional context in comments.

1 Comment

Much better answer than mine.
0

Ok in either ZF1 or ZF2 just do this.

    // Add an email element
    $this->addElement('text', 'orgname', 
      array(             
        'required'   => true,
        'filters'    => array('StringTrim'),
        'style'      => array('width:220px'),
        'decorators' => Array('ViewHelper','Errors'),
        'value'      => $showdetails->orgname
      )       
    ));

You might want to test first for null/empty values first though, you could use ternary operators for convenience:

    // Add an email element
    $this->addElement('text', 'orgname', 
      array(             
        'required'   => true,
        'filters'    => array('StringTrim'),
        'style'      => array('width:220px'),
        'decorators' => Array('ViewHelper','Errors'),
        'value'      => empty($showdetails->orgname)? null : $showdetails->orgname
      )       
    ));

3 Comments

how to get "$my_database_value". please tell me the code. how to access it
this depends on how you're getting your data out of your database. Most of the time it's by a model. However keep things simple to begin with and to test this code snippit just put $my_database_value="I like Zend Framework" before this code.
tell me the code in zend form how to get the values form the data bases
0

Please have a look in my edit function at /var/www/html/zend1app/application/controllers/CountryController.php :

         public function editAction() {
            $data = $this->getRequest()->getParams();
            $id = (int)$data['id'];
            $options = array();

            $country = $this->getCountryModel()->fetchRow("id=$id");    
            if(!$country)
            {
                throw new Exception("Invalid Request Id!");
            }


              $form = new Application_Form_Country();
              $form->addIdElement();


      if ($this->getRequest()->isPost()) {
          if ($form->isValid($this->getRequest()->getPost())){
    $data = new Application_Model_Country();

    if($data->save($form->getValues()))
    {
    $message = array("sucess" => "Country has been updated!");

    }
    else {
    $message = array("danger" => "Country could not be updated!");
    }
    $this->_helper->FlashMessenger->addMessage($message);
    return $this->_helper->redirector('index');
      }
            }
           $options = array (
                    'id' => $country->id,
                    'name' => $country->name,
                    'code' => $country->code
                );

                 $form->populate( $options ); // data binding in the edit form
                 $this->view->form = $form;
        }

and form class at /var/www/html/zend1app/application/forms/Country.php :

  class Application_Form_Country extends Zend_Form
    {

        public function init()
        {
            // Set the method for the display form to POST
            $this->setMethod('post');

            // Add an email element
            $this->addElement('text', 'name', array(
                'label'      => 'Enter Country Name:',
                'required'   => true,
                'filters'    => array('StringTrim'),
                'validators' => array(
                    array('validator' => 'StringLength', 'options' => array(0, 20))
                )
            ));

            // Add the comment element
            $this->addElement('text', 'code', array(
                'label'      => 'Enter Country Code:',
                'required'   => true,
                'validators' => array(
                    array('validator' => 'StringLength', 'options' => array(0, 20))
                    )
            ));



            // Add the submit button
            $this->addElement('submit', 'submit', array(
                'ignore'   => true,
                'label'    => 'Save',
            ));

            // And finally add some CSRF protection
            $this->addElement('hash', 'csrf', array(
                'ignore' => true,
            ));
        }

        public function addIdElement()
        {
          $this->addElement('hidden', 'id');   
        }
    }

HTH

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.