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

Dynamic where rules #16

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
20 changes: 18 additions & 2 deletions spec/Laracasts/Validation/FormValidatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ function it_throws_an_exception_for_invalid_form_data(FactoryInterface $validato
$this->shouldThrow('Laracasts\Validation\FormValidationException')->duringValidate($fakeFormData);
}

function it_injects_mappings_into_rules(FactoryInterface $validatorFactory, ValidatorInterface $validator)
{
$fakeFormData = ['username' => 'joe', 'email' => '[email protected]'];
$mappings = ['user_id' => 1];
$convertedRuleSet = ['username' => 'required', 'email' => 'unique:users,email,1,id'];

$validatorFactory->make($fakeFormData, $convertedRuleSet, [])->willReturn($validator);
$validator->fails()->willReturn(false);

$this->validate($fakeFormData, $mappings)->shouldReturn(true);

$this->getValidationRules()->shouldBe($convertedRuleSet);
}

function it_accepts_a_command_object_in_lieu_of_an_input_array(FactoryInterface $validatorFactory, ValidatorInterface $validator)
{
$formDataAsCommand = new CommandStub;
Expand All @@ -44,12 +58,14 @@ function it_accepts_a_command_object_in_lieu_of_an_input_array(FactoryInterface
$validatorFactory->make($formDataCastToArray, $this->getValidationRules(), [])->willReturn($validator);

$this->validate($formDataAsCommand)->shouldReturn(true);

}
}

class ExampleValidator extends \Laracasts\Validation\FormValidator {
protected $rules = ['username' => 'required'];
protected $rules = [
'username' => 'required',
'email' => 'unique:users,email,{user_id},id'
];
}

class CommandStub {
Expand Down
40 changes: 39 additions & 1 deletion src/Laracasts/Validation/FormValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ function __construct(ValidatorFactory $validator)
* Validate the form data
*
* @param mixed $formData
* @param array $mappings
* @return mixed
* @throws FormValidationException
*/
public function validate($formData)
public function validate($formData, $mappings = [])
{
if ( ! empty($mappings))
{
$this->replaceRulePlaceholdersWith($mappings);
}

$formData = $this->normalizeFormData($formData);

$this->validation = $this->validator->make(
Expand Down Expand Up @@ -98,4 +104,36 @@ protected function normalizeFormData($formData)
return $formData;
}

/**
* Go trough all the mappings
* and inject the mappings into the rules.
*
* @param array $mappings
* @return void
*/
protected function replaceRulePlaceholdersWith(array $mappings)
{
$rules = $this->getValidationRules();

foreach ($mappings as $search => $value)
{
$this->injectMappingIntoRules($rules, $search, $value);
}
}

/**
* Inject a mapping into all defined rules.
*
* @param array $rules
* @param string $search
* @param mixed $value
* @return void
*/
protected function injectMappingIntoRules(array $rules, $search, $value)
{
foreach ($rules as $field => $rule)
{
$this->rules[$field] = str_replace('{'.$search.'}', $value, $rule);
}
}
}