0

I'm using Kohana 3.3 in my project and I'm trying to get the User Registration and Login working. I am using ORM's Auth and Kostache for managing my layout/templates.

How do I:

  • Check if Username already exists? If it does return to error_msg.mustache a message "User already Exists"
  • Check if username and email is valid according to my model rules? If not return error message to error_msg.mustache indicating what validation failed

In my controller I have:

class Controller_User extends Controller {

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

    public function action_createuser()
    {
        try {
            $user = ORM::factory('User');
            $user->username = $this->request->post('username');
            $user->password = $this->request->post('password');
            $user->email = $this->request->post('email');

            // How do I:
            // Check if Username already exists? If it does return to  error_msg.mustache a message "User already Exists"
            // Check if email is valid? If not return error message to error_msg.mustache indicating "email is not valid"

            $user->save();
        }
        catch (ORM_Validation_Exception $e)
        {
            $errors = $e->errors();
        }
    }
}

In my Model:

<?php

class Model_User extends Model_Auth_User
{
    public function rules()
    {
        return array(
            'username' => array(
                array('not_empty'),
                array('min_length', array(':value', 4)),
                array('max_length', array(':value', 32)),
                array('regex', array(':value', '/^[-\pL\pN_.]++$/uD')),
            ),
            'email' => array(
                array('not_empty'),
                array('min_length', array(':value', 4)),
                array('max_length', array(':value', 127)),
                array('email'),
            ),
        );
    }
}

Thanks a lot in advance!

Michal M
  • 9,322
  • 8
  • 47
  • 63
user1155413
  • 169
  • 3
  • 11

2 Answers2

2

You can do the uniqueness check using Validation and an already-written callback. This has the advantages of keeping your validation logic together, and of being very concise:

public function rules()
{
    return array(
        'username' => array(
            array(array($this, 'unique'), array(':field', ':value')),
        // ...

As simple as that!

I originally answered this question with my own solution, which is slightly different from the pre-rolled version, but now that I know about that obviously I'll use it instead of this:

public function rules()
{
    return array(
        'username' => array(
        // ...
            array('Model_User::unique_field', array(':field', ':value', $this->pk())),
        ),
        // ...
    );
}

public static function unique_field($field, $value, $user_id = NULL)
{
    return (ORM::factory('User')->where($field, '=', $value)->find()->pk() === $user_id);
}
Jonathan
  • 1,089
  • 1
  • 7
  • 10
  • The rule I've implemented above says, "If there's a user in the database with such-and-such username, then it has to be you." I'll explain now why that logic is correct. Suppose that you create the user with username `bob`. Then suppose that you want to edit some field of `bob`'s. N.B. There is now a user with username `Bob`, so if your validation did a naïve check for existence of such a user, it would fail even if all you were trying to do was increment `bob`'s login count. – Jonathan Feb 14 '13 at 21:19
  • great! now my question is how do I show a custom error message when this happens and send it back to the
    using kostache
    – user1155413 Feb 15 '13 at 16:12
1

Unfortunately I can't help you with the Kostache, but in order to check whether a username already exists you have to actually try and load it:

$user = ORM::factory('User')->where('username', '=', $this->request->post('username'));

if ($user->loaded())
{
    // The username already exists
}

You probably want to do this before actually opening the try/catch block.

To use the proper error messages you need to define them in /application/messages folder as described in the ORM Validation guide.

Michal M
  • 9,322
  • 8
  • 47
  • 63