From 270169e393e1184a537585e14ce25a7a4c2c022c Mon Sep 17 00:00:00 2001 From: Jacob Bennett Date: Wed, 24 Dec 2014 13:52:03 -0600 Subject: [PATCH 1/5] feat: Add the ability to pass through a context for validation rules --- composer.json | 2 +- src/Laracasts/Validation/FormValidator.php | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 37c8911..4bc85df 100644 --- a/composer.json +++ b/composer.json @@ -20,5 +20,5 @@ "Laracasts\\Validation": "src/" } }, - "minimum-stability": "stable" + "minimum-stability": "dev" } diff --git a/src/Laracasts/Validation/FormValidator.php b/src/Laracasts/Validation/FormValidator.php index ec4fd62..d13e80e 100644 --- a/src/Laracasts/Validation/FormValidator.php +++ b/src/Laracasts/Validation/FormValidator.php @@ -35,13 +35,13 @@ function __construct(ValidatorFactory $validator) * @return mixed * @throws FormValidationException */ - public function validate($formData) + public function validate($formData, $context = null) { $formData = $this->normalizeFormData($formData); $this->validation = $this->validator->make( $formData, - $this->getValidationRules(), + $this->getValidationRules($context), $this->getValidationMessages() ); @@ -56,8 +56,10 @@ public function validate($formData) /** * @return array */ - public function getValidationRules() + public function getValidationRules($context = null) { + if( ! is_null($context) && array_key_exists($context, $this->rules)) return $this->rules[$context]; + return $this->rules; } From 8c67228adcdec76d2f2e89f47e3f711fa2fc960c Mon Sep 17 00:00:00 2001 From: Jacob Bennett Date: Wed, 24 Dec 2014 14:42:32 -0600 Subject: [PATCH 2/5] refactor: Extract context param from validate method, create context property with getter and setter --- src/Laracasts/Validation/FormValidator.php | 30 +++++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Laracasts/Validation/FormValidator.php b/src/Laracasts/Validation/FormValidator.php index d13e80e..9109547 100644 --- a/src/Laracasts/Validation/FormValidator.php +++ b/src/Laracasts/Validation/FormValidator.php @@ -5,6 +5,11 @@ abstract class FormValidator { + /** + * @var string + */ + protected $context; + /** * @var ValidatorFactory */ @@ -35,13 +40,13 @@ function __construct(ValidatorFactory $validator) * @return mixed * @throws FormValidationException */ - public function validate($formData, $context = null) + public function validate($formData) { $formData = $this->normalizeFormData($formData); $this->validation = $this->validator->make( $formData, - $this->getValidationRules($context), + $this->getValidationRules(), $this->getValidationMessages() ); @@ -56,13 +61,30 @@ public function validate($formData, $context = null) /** * @return array */ - public function getValidationRules($context = null) + public function getValidationRules() { - if( ! is_null($context) && array_key_exists($context, $this->rules)) return $this->rules[$context]; + if( ! is_null($this->context) && array_key_exists($this->context, $this->rules)) return $this->rules[$this->context]; return $this->rules; } + /** + * @return FormValidator + */ + public function setContext($context) + { + $this->context = $context; + return $this; + } + + /** + * @return string + */ + public function getContext() + { + return $this->context; + } + /** * @return mixed */ From 9ce6a6d380d8fc1d6f0ffa9577bb32f19311b864 Mon Sep 17 00:00:00 2001 From: Jacob Bennett Date: Wed, 24 Dec 2014 14:42:52 -0600 Subject: [PATCH 3/5] test: Add tests to check that context is working properly --- .../Validation/FormValidatorContextSpec.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 spec/Laracasts/Validation/FormValidatorContextSpec.php diff --git a/spec/Laracasts/Validation/FormValidatorContextSpec.php b/spec/Laracasts/Validation/FormValidatorContextSpec.php new file mode 100644 index 0000000..9ed2354 --- /dev/null +++ b/spec/Laracasts/Validation/FormValidatorContextSpec.php @@ -0,0 +1,38 @@ +beAnInstanceOf('spec\Laracasts\Validation\AnotherExampleValidator'); + $this->beConstructedWith($validatorFactory); + } + + function it_allows_setting_of_a_context() + { + $this->setContext('store'); + + $this->getContext()->shouldReturn('store'); + } + + function it_returns_correct_validation_rules() + { + $this->setContext('store'); + + $this->getValidationRules()->shouldReturn(array('username' => 'required')); + } + +} + +class AnotherExampleValidator extends \Laracasts\Validation\FormValidator { + protected $rules = [ + 'store' => ['username' => 'required'] + ]; +} From 957aa910e6a900910e5da378925f83d87af7a36d Mon Sep 17 00:00:00 2001 From: Jacob Bennett Date: Wed, 24 Dec 2014 14:53:39 -0600 Subject: [PATCH 4/5] docs: Updated Readme --- readme.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/readme.md b/readme.md index d9db891..a236bc5 100644 --- a/readme.md +++ b/readme.md @@ -103,3 +103,30 @@ Now, just inject this object into your controller or application service, and ca ```php $this->registrationForm->validate(Input::all()); ``` + +## Optional + +You can also create a set of nested rules like this: + +```php + /** + * Validation rules for registering + * + * @var array + */ + protected $rules = [ + 'store' => [ + 'username' => 'required', + 'email' => 'required|unique:users', + ], + 'update' => [ + 'username' => 'required', + ] + ]; +``` + +To call the set of rules you want to use, simply set a context on the validator before calling `validate()` + +```php + $this->registrationForm->setContext('store')->validate(Input::all()); +``` \ No newline at end of file From 49b92a9d9ca5c6e94767b53d48477b699a1d727d Mon Sep 17 00:00:00 2001 From: Jacob Bennett Date: Wed, 24 Dec 2014 14:57:29 -0600 Subject: [PATCH 5/5] fix: reset min-stability in composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 4bc85df..37c8911 100644 --- a/composer.json +++ b/composer.json @@ -20,5 +20,5 @@ "Laracasts\\Validation": "src/" } }, - "minimum-stability": "dev" + "minimum-stability": "stable" }