Question

I'm trying to create a user registration page for my site using Kohana 3.3 and Kostache as my template system. 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).

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 :(

Was it helpful?

Solution

Change

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

to

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top