Kohana 3.3& Kostache - 无法在表单中显示ORM验证错误

时间:2013-02-15 16:41:25

标签: kohana-3 kohana-orm kostache

我正在尝试使用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();

}

signup.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>

非常感谢您提供给我的任何指示。 我花了好几个小时才开始工作:(

1 个答案:

答案 0 :(得分:1)

更改

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

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