0

我正在尝试使用 Kohana 3.3 和 Kostache 作为我的模板系统为我的网站创建一个用户注册页面。我很难使用表单验证来在同一页面上显示验证错误。现在,当我单击表单提交按钮并在表单中发送所有空值(用户名、电子邮件和密码)时,我得到的只是要刷新的表单,当我应该收到验证错误时,例如用户名为空,电子邮件为空等(我使用 Model_Auth_User)。

我不知道我做错了什么。

模型:

class Model_User extends Model_Auth_User
{
    public function rules()
    {
        return array(
            'username' => array(
                array('not_empty'),
                array('alpha_dash'),
                array('min_length', array(':value', 4)),
                array('max_length', array(':value', 20)),
                array(array($this, 'unique'), array('username', ':value')),
            ),
            'email' => array(
                array('not_empty'),
                array('min_length', array(':value', 4)),
                array('max_length', array(':value', 127)),
                array('email'),
            ),
        );
    }
}

控制器:

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_User extends Controller {

    public function action_index()
    {
    }

    public function action_login()
    {
        $renderer = Kostache_Layout::factory();
        $this->response->body($renderer->render(new View_FrontEnd_User, 'frontend/login'));
    }

    public function action_signup()
    {
        $renderer = Kostache_Layout::factory();
        $this->response->body($renderer->render(new View_FrontEnd_User, 'frontend/signup'));
    }


    public function action_createuser()
    {
        $signupView = new View_FrontEnd_User();

        try {
            $user = ORM::factory('User');
            $user->username = $this->request->post('username');
            $user->password = $this->request->post('password');
            $user->email = $this->request->post('email');
            $user->save();
        }
        catch (ORM_Validation_Exception $e)
        {
            $errors = $e->errors();
            $signupView->errors = $errors;
        }

        $renderer = Kostache_Layout::factory();
        $this->response->body($renderer->render(new View_FrontEnd_User, 'frontend/signup'));
    }
}

看法

<?php defined('SYSPATH') or die('No direct script access.');

class View_FrontEnd_User extends View_Main
{
    public $errors = array();

}

注册.mustache

<p>

<form action="user/createuser" method="post">
  <fieldset style="width: 20em;">

    <legend>User Registration</legend>

    <label>Enter your username</label>
    <input type="text" name="username" />

    <label for="username" class="error">
        {{#errors}}{{username}}{{/errors}}
    </label>

    <label>Enter your password</label>
    <input type="password" name="password" />

    <label for="password" class="error">
        {{#errors}}{{password}}{{/errors}}
    </label>

    <label>Email</label>
    <input type="text" name="email" />

    <input type="submit" value="Submit" class="nice blue radius button" />

  </fieldset>
</form>


{{#errors}}{{.}}{{/errors}}

</p>

非常感谢您能给我的任何指示。我已经花了几个小时在这上面,但仍然无法让它工作:(

4

1 回答 1

1

改变

$this->response->body($renderer->render(new View_FrontEnd_User, 'frontend/signup'));

$this->response->body($renderer->render($signupView, 'frontend/signup'));

于 2013-02-15T16:47:52.823 回答