0

I have this situation. From the BiograpiesController, after a query, I get this array:

    Array (
            [0] => Array (
                    [Biography] => Array (
                            [id] => 7
                            [biography] => AAA
                    )
            )
            [1] => Array (
                    [Biography] => Array (
                            [id] => 9
                            [biography] => BBBBB
                )
        )
    )

In the View I want create a Form whit this data. I use:

    echo $this->Form->create( 'Biography' ));
    echo $this->Form->input( '0.Biography.biography', array( 'label' => 'A' ));
    echo $this->Form->input( '1.Biography.biography', array( 'label' => 'B' ));
    echo $this->Form->end( );

The first field is filled with the correct data, the second is empty. Then I try:

    echo $this->Form->input( 'A1', array( 'type' => 'textarea', 'rows' => '10', 'cols' => '40', 'value' => $this->request->data[0]['Biography']['biography'] ));
    echo $this->Form->input( 'B1', array( 'type' => 'textarea', 'rows' => '10', 'cols' => '40', 'value' => $this->request->data[1]['Biography']['biography'] ));

The field A1 is filled with the correct data and the second is empty. Why is this happening? How can I fill the form with the correct data that I have in the controller?

Thank you very much

4
  • are you sure that $this->request->data in view is exactly how you posted above? Your second solutions should works. Try post a debug($this->request->data) Commented Mar 3, 2014 at 11:03
  • Yes, the data are the same. I used to see them: pr($this->request->data). Commented Mar 3, 2014 at 12:55
  • I suppose this is edit form of some kind? I suspect there is a problem with your data being displayed in the form because there are two ids, and the form is most likely being populated with data from first array, in your case id 7. Commented Mar 3, 2014 at 13:34
  • The main index, 0, 1, are the languages. Each language has a text and an ID. View: echo $Form->input( 'O1', array( 'value' => $this->request->data[0]['Biography']['id'] )); - OK echo $Form->input( 'O2', array( 'value' => $this->request->data[1]['Biography']['id'] )); - OK echo $Form->input(...=> $this->request->data[0]['Biography']['biography'] )); - OK echo $Form->input( 'A2', array( 'type' => 'text', 'value' => $this->request->data[1] -> ['Biography']['biography'] )); - EMPTY I want a form with all the data. Thus, in the controller, I can use: $this->Biography->save($this->request->data) Commented Mar 3, 2014 at 13:59

1 Answer 1

2

I don''t have much knowledge of cakephp but as per the document it should work

echo $this->Form->create( 'Biography' ));
echo $this->Form->input( 'Biography.0.biography', array( 'label' => 'A' ));
echo $this->Form->input( 'Biography.1.biography', array( 'label' => 'B' ));
echo $this->Form->end( );

Please note the change at 2 and 3rd line

I have referred following link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#field-naming-conventions

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

3 Comments

I also tried this solution, but I do not get anything
Yogesh is correct, this is correct for creating form fields from arrays of model data. If it's not working then you're array may not be in the correct format
Can you describe the correct model? The array that I get from the Controller should be corrected. The fields that contain the IDs are right (0 and 1), the first textarea field are OK, the second textarea is empty. Why? Thank's

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.