Zend framework 2: form binding does not set values ​​retrieved from the database

I want to create a form with Zend Framework 2 for my application, and I have everything in place and the form is displayed, but my problem is that I cannot bind the initial values ​​of the forms that come from the database

$myUserDetails = <details of my user coming from DB>; $form = $form->bind($myUserDetails); //This should set the values for the form to display but it doesnt 

My display logic is simple as below

 $form = $this->form; $form->setAttribute('action', $this->url('<routename>',array('action'=>'<actionname>'))); $form->prepare(); echo $this->form()->openTag($form) . PHP_EOL; echo $this->formRow($form->get('email_id')) . PHP_EOL; echo $this->formRow($form->get('dob')) . PHP_EOL; echo $this->formRow($form->get('gender')) . PHP_EOL; echo $this->formRow($form->get('user_page_name')) . PHP_EOL; echo $this->formInput($form->get('submit')) . PHP_EOL; echo $this->form()->closeTag($form) . PHP_EOL; 

Now I tried to set the data from my object, which I bind to the form in my controller action.

 $myUserDetails = <details of my user coming from DB>; $form = $form->bind($myUserDetails); $data = $myUserDetails->getArrayCopy(); $form->setData($data['data']); 

It seems to work somehow and display my values ​​in the view. So I just want to know what I did wrong in my first approach? Thanks in advance:)

+4
source share
1 answer

Thanks @Stoyan Dimov I solved the problem. This was mainly with the getArrayCopy function in my model, as I was returning an array that was enclosed in index data

So, I changed myArrayCopy to

 public function getArrayCopy() { $data = get_object_vars($this); return $data['data']; } 

And voila, it worked.

Thanks again Stoyan Dimov

+3
source

All Articles