简体   繁体   English

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

[英]Kohana 3.3 & Kostache - Unable to display ORM Validation Errors in Form

I'm trying to create a user registration page for my site using Kohana 3.3 and Kostache as my template system. 我正在尝试使用Kohana 3.3和Kostache作为模板系统为我的网站创建一个用户注册页面。 I'm having a hard time getting to work the Form Validation to display validation errors on the same page. 我很难使用表单验证来在同一页面上显示验证错误。 Right now when i click on the form submit button and sending all empty values in the form (username, email and password) all i get is the form to refresh, when I should be getting validation errors such as username is empty, email is empty etc (im using Model_Auth_User). 现在,当我单击表单提交按钮并发送表单中的所有空值(用户名,电子邮件和密码)时,我得到的只是刷新表单,当我应该收到验证错误时,例如用户名为空,电子邮件为空等等(即使用Model_Auth_User)。

I have no clue what am I doing wrong. 我不知道我在做什么错。

Model: 模型:

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'),
            ),
        );
    }
}

Controller: 控制器:

<?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'));
    }
}

View 视图

<?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>

Thanks a lot in advance for any pointers you can give me. 在此先非常感谢您提供给我的任何指导。 I've spent hours on this and still can't get it working :( 我已经花了几个小时,但仍然无法正常工作:(

Change 更改

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM