视图显示不正确

时间:2011-08-01 09:11:48

标签: cakephp cakephp-1.3

我正在学习Cake并且正在关注IBM's tutorial。我已经从蛋糕网站成功完成了博客教程 我的问题Register视图未显示。而不是显示表单,而是立即执行register()操作,并且检查表单参数是否为空的条件总是失败,所以我总是找不到注册用户消息的失败。


ctp文件看起来像这样(它没有使用帮助程序):

<form action="/users/register" method="post">
<p>Please fill out the form below to register an account.</p>
<label>Username:</label><input name="username" size="40" />  
<label>Password:</label><input type="password" name="password" size="40" />
<label>Email Address:</label><input name="email" size="40" maxlength="255" />
<label>First Name:</label><input name="first_name" size="40" />
<label>Last Name:</label><input name="last_name" size="40" /> 
<input type="submit" value="register" />
</form>

register()操作是:

function register() {
    if (!empty($this->params['form']))
    {
        if ($this->User->save($this->params['form']))
        {
            $this->flash('Your registration infomration was accepted.', '/users/register');
        }
    }
    else
    {
        $this->flash('There was a problem with your registration', '/users/register');
    }
}

1 个答案:

答案 0 :(得分:1)

尝试:

function register() {
    if (!empty($this->data)) {
        $this->User->create();
        if ($this->User->save($this->data)) {
            $this->flash('Your registration infomration was accepted.', '/users/register');
        } else {
            $this->flash('There was a problem with your registration', '/users/register');
        }
    }

}

最大的区别在于使用$this->data代替$this->params[form]并添加$this->User->create();。 else语句也应该是if(用户保存)flash保存,否则flash不保存。

相关问题