2

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]);
Amol Bhandari SJ
  • 278
  • 2
  • 15
  • Please show your code for loading the existing user entity, and for creating the form (not the form controls, which you have here already, but the actual `create` call). – Greg Schmidt Oct 14 '19 at 19:20
  • public function updateProfile() { $user = $this->Users->newEntity(); $userId = $this->Auth->user('id'); //echo $this->Auth->user('firstname'); $userData = $this->Users->get($this->Auth->user('id')); if ($this->request->is('post')) { //has post data execution code } } $this->set(compact('user')); } – Amol Bhandari SJ Oct 16 '19 at 03:39

2 Answers2

2

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]);
Anik Roy
  • 21
  • 1
2

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.

Akshay Naik
  • 553
  • 3
  • 17