Allowing Usernames and Emails longer than 32 Characters in Kohana 3.0

January 7, 2013

This is a simple fix, if you know where to look. By default, Kohana does not allow usernames longer than 32 characters, although it allows for emails of up to 127 characters. The problem with this setup is that the username is set to the email by default. If users have email addresses longer than 32 characters, Kohana will generate an error and prevent a user from logging in.

So if you want to fix the issue, simply edit the structure of your MySQL database column for username to whichever length you wish. But that’s only half the solution. You will also need to edit the Kohana configuration, because it’s built in.

You will find the file to edit here: MODPATH/orm/classes/model/auth/user.php

The applicable lines will look something like this:

	public function rules()
	{
		return array(
			'username' => array(
				array('not_empty'),
				array('max_length', array(':value', 32)),
				array(array($this, 'unique'), array('username', ':value')),
			),
			'password' => array(
				array('not_empty'),
			),
			'email' => array(
				array('not_empty'),
				array('email'),
				array(array($this, 'unique'), array('email', ':value')),
			),
		);
	}

Change the lines to this:

	public function rules()
	{
		return array(
			'username' => array(
				array('not_empty'),
				array('max_length', array(':value', 64)),
				array(array($this, 'unique'), array('username', ':value')),
			),
			'password' => array(
				array('not_empty'),
			),
			'email' => array(
				array('not_empty'),
				array('email'),
				array(array($this, 'unique'), array('email', ':value')),
			),
		);
	}

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive