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

[4.2] add setter and getter for Validatior #5199

Merged
merged 6 commits into from
Sep 10, 2014
Merged
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
55 changes: 55 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2119,6 +2119,19 @@ public function setAttributeNames(array $attributes)

return $this;
}

/**
* Set the custom values on the validator.
*
* @param array $values
* @return \Illuminate\Validation\Validator
*/
public function setValueNames(array $values)
{
$this->customValues = $values;

return $this;
}

/**
* Get the files under validation.
Expand Down Expand Up @@ -2212,6 +2225,48 @@ public function setCustomMessages(array $messages)
{
$this->customMessages = array_merge($this->customMessages, $messages);
}

/**
* Get the custom attributes for the validator
*
* @return array
*/
public function getCustomAttributes()
{
return $this->customAttributes;
}

/**
* Add the custom attributes for the validator
*
* @param array $customAttributes
* @return void
*/
public function addCustomAttributes(array $customAttributes)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a setAttributeNames method that does almost the same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@piotr-cz One is 'set', 'add' is another.

{
$this->customAttributes = array_merge($this->customAttributes, $customAttributes);
}

/**
* Get the custom values for the validator
*
* @return array
*/
public function getCustomValues()
{
return $this->customValues;
}

/**
* Add the custom values for the validator
*
* @param array $customValues
* @return void
*/
public function addCustomValues(array $customValues)
{
$this->customValues = array_merge($this->customValues, $customValues);
}

/**
* Get the fallback messages for the validator.
Expand Down
44 changes: 43 additions & 1 deletion tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ public function testAttributeNamesAreReplaced()
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('Name is required!', $v->messages()->first('name'));

//set customAttributes by setter
$trans = $this->getRealTranslator();
$trans->addResource('array', array('validation.required' => ':attribute is required!'), 'en', 'messages');
$customAttributes = array('name' => 'Name');
$v = new Validator($trans, array('name' => ''), array('name' => 'Required'));
$v->addCustomAttributes($customAttributes);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('Name is required!', $v->messages()->first('name'));


$trans = $this->getRealTranslator();
$trans->addResource('array', array('validation.required' => ':attribute is required!'), 'en', 'messages');
Expand Down Expand Up @@ -147,7 +158,38 @@ public function testDisplayableValuesAreReplaced()
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('type must be included in Short, Long.', $v->messages()->first('type'));


// test addCustomValues
$trans = $this->getRealTranslator();
$trans->addResource('array', array('validation.in' => ':attribute must be included in :values.'), 'en', 'messages');
$customValues = array(
'type' =>
array(
'5' => 'Short',
'300' => 'Long',
)
);
$v = new Validator($trans, array('type' => '4'), array('type' => 'in:5,300'));
$v->addCustomValues($customValues);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('type must be included in Short, Long.', $v->messages()->first('type'));

// set custom values by setter
$trans = $this->getRealTranslator();
$trans->addResource('array', array('validation.in' => ':attribute must be included in :values.'), 'en', 'messages');
$customValues = array(
'type' =>
array(
'5' => 'Short',
'300' => 'Long',
)
);
$v = new Validator($trans, array('type' => '4'), array('type' => 'in:5,300'));
$v->setValueNames($customValues);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('type must be included in Short, Long.', $v->messages()->first('type'));
}


Expand Down