cant populate form

For programming and general questions on Zend Framework

cant populate form

Postby ierpe on Tue Jul 07, 2009 11:35 am

Hi,

I am impleting a CRUD controller at the moment, and in my edit action, I have to retrieve the data from the db and then insert the values in the form. But it doesnt work. From what I read I guess it could be linked with the form filtering, but I didnt find how to manage it.
I am using a Dojo form.

Code: Select all
//in the editAction
[...]

//retrieve data from db
$data = $obj->select();

$form->populate($data); //this doesnt work!!!
$this->view->form = $form;
return $this->render('/edit');



Your help would be much appreciated!
ierpe
 
Posts: 17
Joined: Tue Jun 02, 2009 11:53 am

Re: cant populate form

Postby massimilianoc on Tue Jul 07, 2009 4:03 pm

What is the code of the function 'populate()' and what is the type of the parameter you pass to it?

It seems to me this post is just about Zend Framework components, it does not relate to Zend Platform.

I guess you should be moving it into the appropriate forum.

Thanks in advance,
Massi.
Best regards,
Massi.
massimilianoc
 
Posts: 699
Joined: Thu Mar 12, 2009 11:58 am

Re: cant populate form

Postby ierpe on Tue Jul 07, 2009 9:33 pm

Hmm ok sorry Im new here.

How can I move this post to the right part of the forum?

Ive seen the populate function in the chapter: 23.4.2.1. Populating and Retrieving Values on the zend doc: http://framework.zend.com/manual/en/zend.form.forms.html

my $data is an array containing the info of one object, retrieved from a db, like [db_column_name => value]
I don't understand why there isn't a simple way to pass an array containing [form_input_name => value_to_insert] (e.g. my $data) to insert the data in the form inputs... I expected the populate function to fill the form with the values from $data...?

Does it make sense?
ierpe
 
Posts: 17
Joined: Tue Jun 02, 2009 11:53 am

Re: cant populate form

Postby massimilianoc on Wed Jul 08, 2009 10:00 am

I think I am missing something here, are you try to generate a form page or you are trying to populate with inputs a form?

Thanks in advance,
Massi.
Best regards,
Massi.
massimilianoc
 
Posts: 699
Joined: Thu Mar 12, 2009 11:58 am

Re: cant populate form

Postby ierpe on Wed Jul 08, 2009 10:36 am

Ok lets take an object Member for example, which has a username and place of birth.

Now lets say the member wants to update his information.
I have a form with a field for its username and a field for its place of birth.

My update action is fetching the data from the DB and so I have an array(username=>"john",placeofbirth=>"london"), the form fields are called "username" and "placeofbirth".

So what I am trying to do here is to insert these values into the form, so that when the form is displayed, the fields username and placeofbirth already contain the values "john" and "london".

Do you get me? Im just building a CRUD system at the moment and this is the last missing piece...
ierpe
 
Posts: 17
Joined: Tue Jun 02, 2009 11:53 am

Re: cant populate form

Postby massimilianoc on Wed Jul 08, 2009 12:23 pm

Now the picture is clear.

What I suggest you is to create a Member_Form_Update extending Zend_Form, which will define the form using the 'init()' function and will load the data you need through the 'populate()' function.

Something like this:
Code: Select all
class My_Form_Login extends Zend_Form
{
    public function init()
    {
        $username = new Zend_Form_Element_Text('username');
        $username->class = 'formtext';
        $username->setLabel('Username:')
                 ->setDecorators(array(
                     array('ViewHelper',
                           array('helper' => 'formText')),
                     array('Label',
                           array('class' => 'label'))
                 ));

        $password = new Zend_Form_Element_Text('placeofbirth');
        $password->class = 'formtext';
        $password->setLabel('Place Of Birth:')
                 ->setDecorators(array(
                     array('ViewHelper',
                           array('helper' => 'formText')),
                     array('Label',
                           array('class' => 'label'))
                 ));

        //... All the other elements you need in the form ...

        $this->addElements(array(
            $username,
            $password
             //Add all the defined elements
         ));

        // ... Add decorators if needed ...
    }
}


Then you can call
Code: Select all
Member_Form_Update->populate(array('username' => 'john', 'placeofbirth' => 'london', ... The rest of the elements ...))


The 'populate' function is a proxy for 'setDefaults' which code is:
Code: Select all
       foreach ($this->getElements() as $name => $element) {
            if (array_key_exists($name, $defaults)) {
                $this->setDefault($name, $defaults[$name]);
            }
        }
        foreach ($this->getSubForms() as $name => $form) {
            if (array_key_exists($name, $defaults)) {
                $form->setDefaults($defaults[$name]);
            } else {
                $form->setDefaults($defaults);
            }
        }
        return $this;


Best regards,
Massi.
Best regards,
Massi.
massimilianoc
 
Posts: 699
Joined: Thu Mar 12, 2009 11:58 am

Re: cant populate form

Postby ierpe on Wed Jul 08, 2009 12:50 pm

Thanks for your help, what you described is pretty much what I already had, and it wasn't working...
But actually I found my mistake, it was only a problem with my array, i was not pointing to the right part of the array... :-/ stupid mistake! Sorry for the wasted time...

Anyway, its working now, the data is being inserted into the form, and a new problem occurs :p

I have a text area called "Description", the value from the db is inserted in it, but it is not editable anymore, i just see it as plain text, there is no text area "box" and and i cant edit the text...

Im using a Dojo form so maybe its linked...?

Edit: Actually i tried to extend my form from Zend_Form instead of Zend_Dojo_Form and the text area is displayed normally, so it is definitely linked with Dojo... will try to figure it out and post the solution!
ierpe
 
Posts: 17
Joined: Tue Jun 02, 2009 11:53 am

Re: cant populate form

Postby massimilianoc on Wed Jul 08, 2009 1:31 pm

Probably you need just to raise the editable flag in the configuration array of the form.

Glad you could overcome your other issues.

Regards,
Massi.
Best regards,
Massi.
massimilianoc
 
Posts: 699
Joined: Thu Mar 12, 2009 11:58 am

Re: cant populate form

Postby ierpe on Wed Jul 08, 2009 2:11 pm

That's what I was thinking too but I cant find anything about an editable flag.

Also I solved it by removing the:

<?php $this->dojo()->addStyleSheetModule('dijit.themes.tundra'); ?>
<?php echo $this->dojo() ?>

in the header of my layout.phtml.

but i guess now I'm not going to be able to use Dojo the right way... Im just completely new to Dojo and ZF and I couldn't find any good source of documentation for this integration... The zend doc is pretty poor about it and Dojo's doc is only about the toolkit and not the zend integration...

Any suggestion?
ierpe
 
Posts: 17
Joined: Tue Jun 02, 2009 11:53 am

Re: cant populate form

Postby massimilianoc on Thu Jul 09, 2009 1:19 pm

I will move this thread to the Zend Framework forum since it belongs to there.

Best regards,
Massi.
Best regards,
Massi.
massimilianoc
 
Posts: 699
Joined: Thu Mar 12, 2009 11:58 am


Return to Zend Framework

Who is online

Users browsing this forum: No registered users and 2 guests