diff --git a/after:1986-05-28 b/after:1986-05-28 new file mode 100644 index 00000000..e69de29b diff --git a/after:1986-28-05, b/after:1986-28-05, new file mode 100644 index 00000000..e69de29b diff --git a/application/language/en/validation.php b/application/language/en/validation.php index 74f90264..c07e4a0d 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/before:1986-05-28 b/before:1986-05-28 new file mode 100644 index 00000000..e69de29b diff --git a/before:1986-28-05, b/before:1986-28-05, new file mode 100644 index 00000000..e69de29b diff --git a/laravel/documentation/validation.md b/laravel/documentation/validation.md index 0ca6fbf0..06feb707 100644 --- a/laravel/documentation/validation.md +++ b/laravel/documentation/validation.md @@ -205,6 +205,14 @@ Many times, when updating a record, you want to use the unique rule, but exclude > **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 diff --git a/laravel/tests/cases/validator.test.php b/laravel/tests/cases/validator.test.php index de8fe10f..30700f54 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['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. * diff --git a/laravel/validator.php b/laravel/validator.php index 89cbba53..9e15b3ee 100644 --- a/laravel/validator.php +++ b/laravel/validator.php @@ -759,6 +759,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. *