cakephp 3 prefill user details in textbox

Viewed 85

I have update profile view and my form is fully working. I need when i load this form it should be pre filled with database values instead of empty. i dont want to use sessions. I am using cakephp 3.8 version and in update_profile.ctp file i have below code

echo $this->Form->control('firstname',['minlength'=>3, 'value'=>'' ]);
echo $this->Form->control('lastname',['minlength'=>3]);
2 Answers

We can pass data into update_profile.ctp file from controller using $this->set(compact('Users'));

here users is a table into database which has fields named 'firstname', 'lastname'

Then we can use it in our form like

echo $this->Form->control('firstname',['minlength'=>3, 'value'=>'' ]);
echo $this->Form->control('lastname',['minlength'=>3]);

Given below is the updated function

public function updateProfile() {
     $user = $this->Users->newEntity();
     $userId = $this->Auth->user('id');
     $userData = $this->Users->get($this->Auth->user('id'));
     if ($this->request->is('post')) {
       //has post data execution code
     }
     //$this->set(compact('user'));
     $this->set(compact('userData'));
 }

You had passed "user" variable, you have to pass userData since in this variable u have fetched the user details. Once this is passed form will get pr-populated.

Related