I don't know how to set the return value.

Asked 2 years ago, Updated 2 years ago, 52 views

I would like to put the value entered in the view file and posted in the $id of the controller file, but unfortunately I don't know how to write the return value, so I am asking you a question.
I would like to put the value entered in the form in this $id.


View File = Personal Information Change Form Input → Personal Information Update

public function profile()
    {
        $this->request->data('username');
        $this->request->data('pass');
        $this->request->data('email');
        // return 

        if(!$id) 
        {
           through new NotFoundException(__('Invalid user'));
        }
        $user=$this->Post->findById($id);
        if(!$user)
        {
           through new NotFoundException(__('Invalid user'));
        }
        if($this->request->is(array('post','put'))))
        {
            $this->post->id=$id;
            if ($this->post->save($this->request->data))
            {
                $this->Flash->secs(__('Your information could be updated'));
                return $this->redirect(array('action'=>'edit'));
            }
                $this->Flash->error(__('Unable to update your information'));
        }
        if(!$this->request->data)
        {
            $this->request->data=$post;
        }
      }  

viewfile

<?php
echo$this->Form->create('users',array('action'=>'profile'));
echo$this->Form->input('username', ['username']);
echo$this->Form->input('pass',['password']);
echo$this->Form->input('email', ['email']);
echo$this->Form->end('Save');  
?>

UserModel File

class User extensions AppModel {
    public function getActivationHash() 
    {
        if(!isset($this->id)){
            return false;
        } else{
            return Security::hash($this->field('modified'), 'md5', true);
        }
    }
}

php cakephp

2022-09-29 20:26

1 Answers

Answer as CakePHP 2.x

FormHelper gives the generated field the name attribute according to the first argument of FormHelper::create (in this case users).

For the code in this question, the input tag is rendered as follows:
(For simplicity, write only the input tag and name attribute.

<input name="data[users][username]">
<input name="data[users][pass]">
<input name="data[users][email]">

The resulting POST data has the following structure:

[
    'users' = > [
        'username' = > '...',
        US>'pass'=>'...',
        'email' = > '...',
    ],
]

The controller takes this out using the data method in the request.

$data=$this->request->data('users');
// $data has the following data structures ['username'=>'...', 'pass'=>'...', 'email'=>...', ]

$email = $this->request->data('users.email');
// $email is the email input value of the form

Note:
Accessing POST Data | Request and Response Objects - 2.x
https://book.cakephp.org/2.0/ja/controllers/request-response.html#post

As a supplement, the view file states $this->Form->create('users'), which is out of the CakePHP 2.x convention.
If it is in accordance with the terms and conditions, it should be $this->Form->create('User').
In this case, extracting the Post value is as follows:

$data=$this->request->data('User');
$email = $this->request->data('User.email');


2022-09-29 20:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.