Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added simple support utf-8 for size_of validator #558

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/Validations.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Validations
private $options = array();
private $validators = array();
private $record;
// TODO add ability to set charset
private $charset = 'UTF-8';

private static $VALIDATION_FUNCTIONS = array(
'validates_presence_of',
Expand Down Expand Up @@ -517,11 +519,18 @@ public function validates_length_of($attrs)
$message = $options['message'];
else
$message = $options[$messageOptions[$range_option]];


$message = str_replace('%d', $option, $message);
$attribute_value = $this->model->$attribute;
$len = strlen($attribute_value);

//TODO add exception if charset wrong
if (@mb_check_encoding($attribute_value, $this->charset)) {
$len = mb_strlen($attribute_value, $this->charset);
} else {
$len = strlen($attribute_value);
}

$value = (int)$attr[$range_option];

if ('maximum' == $range_option && $len > $value)
Expand Down
30 changes: 23 additions & 7 deletions test/ValidatesLengthOfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function set_up($connection_name=null)
parent::set_up($connection_name);
BookLength::$validates_length_of[0] = array('name', 'allow_blank' => false, 'allow_null' => false);
}

public function test_within()
{
BookLength::$validates_length_of[0]['within'] = array(1, 5);
Expand Down Expand Up @@ -56,7 +56,7 @@ public function test_within_custom_error_message()
$book->is_valid();
$this->assert_equals(array('Name is not between 2 and 5 characters'),$book->errors->full_messages());
}

public function test_valid_in()
{
BookLength::$validates_length_of[0]['in'] = array(1, 5);
Expand Down Expand Up @@ -135,7 +135,7 @@ public function test_invalid_null_within()
$this->assert_true($book->errors->is_invalid('name'));
$this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name'));
}

public function test_invalid_null_minimum()
{
BookLength::$validates_length_of[0]['minimum'] = 1;
Expand All @@ -145,9 +145,9 @@ public function test_invalid_null_minimum()
$book->save();
$this->assert_true($book->errors->is_invalid('name'));
$this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name'));

}

public function test_valid_null_maximum()
{
BookLength::$validates_length_of[0]['maximum'] = 1;
Expand Down Expand Up @@ -327,7 +327,23 @@ public function test_validates_length_of_minimum()
$book->is_valid();
$this->assert_equals(array("Name is too short (minimum is 2 characters)"),$book->errors->full_messages());
}


public function test_validates_length_of_maximum_with_UTF()
{
BookLength::$validates_length_of[0] = array('name', 'maximum' => 5);
$book = new BookLength(array('name' => 'привет'));
$book->is_valid();
$this->assert_equals(array("Name is too long (maximum is 5 characters)"),$book->errors->full_messages());
}

public function test_validates_length_of_minimum_with_UTF()
{
BookLength::$validates_length_of[0] = array('name', 'minimum' => 3);
$book = new BookLength(array('name' => 'ай'));
$book->is_valid();
$this->assert_equals(array("Name is too short (minimum is 3 characters)"),$book->errors->full_messages());
}

public function test_validates_length_of_min_max_custom_message()
{
BookLength::$validates_length_of[0] = array('name', 'maximum' => 10, 'message' => 'is far too long');
Expand All @@ -340,7 +356,7 @@ public function test_validates_length_of_min_max_custom_message()
$book->is_valid();
$this->assert_equals(array("Name is far too short"),$book->errors->full_messages());
}

public function test_validates_length_of_min_max_custom_message_overridden()
{
BookLength::$validates_length_of[0] = array('name', 'minimum' => 10, 'too_short' => 'is too short', 'message' => 'is custom message');
Expand Down