deck john
deck john

Reputation: 647

Cakephp 2.0 form helper issue

I am new to CakePHP 2.0. It has too many changes from its previous version. I have many problems with implementing basic functionality in CakePHP 2.0. I did the same code in CakePHP 1.3 and 2.0 but the output is different so it creates a problem for me.

I have created one form as shown below:

<form name="User" method="post" action="http://192.168.1.24/project/api/documents/sub" ENCTYPE="multipart/form-data"> 
 <table>
  <tr><td><label>username:</label></td><td><input type="text" name="username"></td></tr>
  <tr><td><label>password:</label></td><td><input type="text" name="password"></td></tr>
  <tr><td><label>email:</label></td><td><input type="text" name="email"></td></tr>
  <tr><td><label>Image:</label></td><td><input type="file" name="image"></td></tr>
  <tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit" />

I used this form for both my applications in CakePHP 1.3 and 2.0. The controller code of CakePHP 1.3 is:

function api_sub()
{
  $this->layout = false;
  $this->data['Document'] = $this->params['form'];
  pr($this->data); die();
}

The output of the above code is:

Array
(
    [Document] => Array
        (
            [username] => mack
            [password] => meack
            [email] => [email protected]
            [submit] => Submit
            [image] => Array
                (
                    [name] => 01manta.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\wamp\tmp\phpF586.tmp
                    [error] => 0
                    [size] => 636306
                )
        )
)

The controller code in CakePHP 2.0 is as shown below:

public function api_sub()
{
  //$this->layout = false;
  $this->request->data['Document']= $this->request->data;
  pr($this->request->data); die();
}

The output of the above code is as shown below:

Array
(
    [username] => mack
    [password] => meack
    [email] => [email protected]
    [submit] => Submit
    [User] => Array
        (
            [username] => mack
            [password] => meack
            [email] => [email protected]
            [submit] => Submit
        )
)

Now the problem is that in CakePHP 2.0, the image array is missing in the output. Can anyone tell me how I can get that image array in CakePHP 2.0? What changes do I have to do in my coding?

Upvotes: 0

Views: 397

Answers (2)

Bardiel W. Thirtytwo
Bardiel W. Thirtytwo

Reputation: 379

Instead of writing the HTML code for the form, you should do as follow:

echo $this->Form->create('ModelName');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('email');
echo $this->Form->input('image');
echo $this->Form->end('Submit!');

Note that in this example asumes you'll validate the image wihtin your model and it will probably be stored in your database. Shoud you wish not to store the image in the database, you could replace the last input with:

echo $this->Form->input('image', array('type' => 'file'));

All of this will be accessible in your Controller through:

$this->request->data

This will be an array that should look like this:

array(
    'ModelName' => array(
        'username' => 'whatever_the_user_wrote',
        'password'=> '********',
        'email' => '[email protected]',
        'image' => the image
    )
)

Upvotes: 1

mensch
mensch

Reputation: 4411

I could be because you're not using the FormHelper. Were you to use said helper the data would probably be properly populated in $this->data or $this->request->data. I can't find anything definitive on the matter the documentation though.

In any case, generic form data is always present in $this->request->params['form'], so the file data for your form would be in $this->request->params['form']['file'].

Upvotes: 1

Related Questions