我正在尝试在cakephp中为已登录用户创建“编辑”个人资料页面。这将是添加/编辑用户信息的功能。 我在$ this-> User-> save($ this-> data)函数中遇到错误,我不明白是什么问题。
public function edit() {
$this->User->id = $this->Auth->User('id');
if ($this->request->is('post')) {
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The user has been saved'), 'flash_success');
// $this->redirect($this->Auth->redirect());
} else {
var_dump($this->invalidFields());
$this->Session->setFlash(__('The user could not be saved. Please, try again.'), 'flash_failure');
}
} else {
//autocompleto il form
$this->data = $this->User->read(null, $this->Auth->User('id'));
}
}
观点是:
<?php
echo $this->Form->create('User',array('action' => 'edit'));
echo $this->Form->input('name', array('label'=> 'Name'));
echo $this->Form->input('surname', array('label'=> 'Surname'));
echo $this->Form->input('id', array('type'=> 'hidden'));
echo $this->Form->end(__('Submit'));
?>
答案 0 :(得分:1)
我看到您使用Auth
组件。如果覆盖了Auth::authorize
默认值,请确保为用户授予执行数据写入的适当权限(可能只允许他阅读)。
另一个问题可能是模型中的$validate
声明,您强制用户输入字段值(使用'required' = true
)但实际上此字段甚至不显示在View上。如果在内部定义'on' => 'create'
,则可以在数据编辑时避免此验证规则。
另外,我建议使用CakePHP debug()代替var_dump()进行调试。