diff --git a/lib/Validations.php b/lib/Validations.php index 64a58cfa5..fb8e43074 100644 --- a/lib/Validations.php +++ b/lib/Validations.php @@ -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', @@ -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) diff --git a/test/ValidatesLengthOfTest.php b/test/ValidatesLengthOfTest.php index bd93a1d80..ce21c89c9 100644 --- a/test/ValidatesLengthOfTest.php +++ b/test/ValidatesLengthOfTest.php @@ -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); @@ -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); @@ -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; @@ -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; @@ -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'); @@ -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');