From 49331d74e24b018fc45a310060aa063093f3a16d Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 7 Sep 2012 14:01:45 +0200 Subject: [PATCH 1/5] Add unit tests for date_format validator rule. --- laravel/tests/cases/validator.test.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/laravel/tests/cases/validator.test.php b/laravel/tests/cases/validator.test.php index bde1cea1..960bf919 100644 --- a/laravel/tests/cases/validator.test.php +++ b/laravel/tests/cases/validator.test.php @@ -483,6 +483,28 @@ class ValidatorTest extends PHPUnit_Framework_TestCase { $this->assertFalse(Validator::make($input, $rules)->valid()); } + /** + * Tests the date_format validation rule. + * + * @group laravel + */ + public function testTheDateFormatRule() + { + $input = array('date' => '15-Feb-2009'); + $rules = array('date' => 'date_format:j-M-Y'); + $this->assertTrue(Validator::make($input, $rules)->valid()); + + $input['date'] = '2009-02-15 15:16:17'; + $rules = array('date' => 'date_format:Y-m-d H\\:i\\:s'); + $this->assertTrue(Validator::make($input, $rules)->valid()); + + $input['date'] = '2009-02-15'; + $this->assertFalse(Validator::make($input, $rules)->valid()); + + $input['date'] = '15:16:17'; + $this->assertFalse(Validator::make($input, $rules)->valid()); + } + /** * Test that the validator sets the correct messages. * From a40aabbb05805cbbc7ea3a755f0131787b09e749 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 7 Sep 2012 14:02:04 +0200 Subject: [PATCH 2/5] Implement date_format validation rule. --- application/language/en/validation.php | 1 + laravel/validator.php | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/application/language/en/validation.php b/application/language/en/validation.php index e5116996..47bf151e 100644 --- a/application/language/en/validation.php +++ b/application/language/en/validation.php @@ -36,6 +36,7 @@ return array( "countbetween" => "The :attribute must have between :min and :max selected elements.", "countmax" => "The :attribute must have less than :max selected elements.", "countmin" => "The :attribute must have at least :min selected elements.", + "date_format" => "The :attribute must have a valid date format.", "different" => "The :attribute and :other must be different.", "email" => "The :attribute format is invalid.", "exists" => "The selected :attribute is invalid.", diff --git a/laravel/validator.php b/laravel/validator.php index ab446860..8b41ffc6 100644 --- a/laravel/validator.php +++ b/laravel/validator.php @@ -758,6 +758,19 @@ class Validator { return (strtotime($value) > strtotime($parameters[0])); } + /** + * Validate the date conforms to a given format. + * + * @param string $attribute + * @param mixed $value + * @param array $parameters + * @return bool + */ + protected function validate_date_format($attribute, $value, $parameters) + { + return date_create_from_format($parameters[0], $value) !== false; + } + /** * Get the proper error message for an attribute and rule. * From e407e67559f695098fe5d1f4aa98b9b2ea15cd53 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 7 Sep 2012 14:03:23 +0200 Subject: [PATCH 3/5] Add documentation for date_format validation rule. --- laravel/documentation/validation.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/laravel/documentation/validation.md b/laravel/documentation/validation.md index 0f17dec0..1805f32c 100644 --- a/laravel/documentation/validation.md +++ b/laravel/documentation/validation.md @@ -196,14 +196,22 @@ Many times, when updating a record, you want to use the unique rule, but exclude #### Validate that a date attribute is before a given date: - 'birthdate' => 'before:1986-28-05'; + 'birthdate' => 'before:1986-28-05', #### Validate that a date attribute is after a given date: - 'birthdate' => 'after:1986-28-05'; + 'birthdate' => 'after:1986-28-05', > **Note:** The **before** and **after** validation rules use the **strtotime** PHP function to convert your date to something the rule can understand. +#### Validate that a date attribute conforms to a given format: + + 'start_date' => 'date_format:H\\:i'), + +> **Note:** The backslash escapes the colon so that it does not count as a parameter separator. + +The formatting options for the date format are described in the [PHP documentation](http://php.net/manual/en/datetime.createfromformat.php#refsect1-datetime.createfromformat-parameters). + ### E-Mail Addresses From f77041ced9f92c2d6dac38acb2b3e0be209bfe1c Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 21 Sep 2012 12:31:49 +0200 Subject: [PATCH 4/5] Add another test with quotes around the validation rule's parameter. --- laravel/tests/cases/validator.test.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/laravel/tests/cases/validator.test.php b/laravel/tests/cases/validator.test.php index 960bf919..b0c4f288 100644 --- a/laravel/tests/cases/validator.test.php +++ b/laravel/tests/cases/validator.test.php @@ -495,7 +495,10 @@ class ValidatorTest extends PHPUnit_Framework_TestCase { $this->assertTrue(Validator::make($input, $rules)->valid()); $input['date'] = '2009-02-15 15:16:17'; - $rules = array('date' => 'date_format:Y-m-d H\\:i\\:s'); + $rules['date'] = 'date_format:Y-m-d H\\:i\\:s'; + $this->assertTrue(Validator::make($input, $rules)->valid()); + + $rules['date'] = 'date_format:"Y-m-d H:i:s"'; $this->assertTrue(Validator::make($input, $rules)->valid()); $input['date'] = '2009-02-15'; From de53feb07e431077d5b78548fddc9919ef5ab09c Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 21 Sep 2012 13:11:54 +0200 Subject: [PATCH 5/5] Fix validation tests one more time. --- laravel/tests/cases/validator.test.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/laravel/tests/cases/validator.test.php b/laravel/tests/cases/validator.test.php index b0c4f288..bd5e355e 100644 --- a/laravel/tests/cases/validator.test.php +++ b/laravel/tests/cases/validator.test.php @@ -494,11 +494,8 @@ class ValidatorTest extends PHPUnit_Framework_TestCase { $rules = array('date' => 'date_format:j-M-Y'); $this->assertTrue(Validator::make($input, $rules)->valid()); - $input['date'] = '2009-02-15 15:16:17'; - $rules['date'] = 'date_format:Y-m-d H\\:i\\:s'; - $this->assertTrue(Validator::make($input, $rules)->valid()); - - $rules['date'] = 'date_format:"Y-m-d H:i:s"'; + $input['date'] = '2009-02-15,15:16:17'; + $rules['date'] = 'date_format:"Y-m-d,H:i:s"'; $this->assertTrue(Validator::make($input, $rules)->valid()); $input['date'] = '2009-02-15';