Skip to content

Commit 9fe4162

Browse files
committed
Change message separator from '.' to ':'
1 parent f3add9f commit 9fe4162

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ $validation_a->validate();
219219
#### Custom Message for Specific Attribute Rule
220220

221221
Sometimes you may want to set custom message for specific rule attribute.
222-
To do this you can use `.` as message separator or using chaining method.
222+
To do this you can use `:` as message separator or using chaining methods.
223223

224224
Examples:
225225

@@ -231,7 +231,7 @@ $validation_a = $validator->make($dataset_a, [
231231
]);
232232

233233
$validation_a->setMessages([
234-
'age.min' => '18+ only',
234+
'age:min' => '18+ only',
235235
]);
236236

237237
$validation_a->validate();

src/Validation.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Validation
1717

1818
protected $aliases = [];
1919

20+
protected $messageSeparator = ':';
21+
2022
public function __construct(Validator $validator, array $inputs, array $rules, array $messages = array())
2123
{
2224
$this->validator = $validator;
@@ -254,22 +256,22 @@ protected function resolveMessage(Attribute $attribute, $value, Rule $validator)
254256
$alias = $attribute->getAlias() ?: $this->resolveAttributeName($attribute);
255257
$message = $validator->getMessage(); // default rule message
256258
$message_keys = [
257-
$attributeKey.'.'.$ruleKey,
259+
$attributeKey.$this->messageSeparator.$ruleKey,
258260
$attributeKey,
259261
$ruleKey
260262
];
261263

262264
if ($primaryAttribute) {
263265
// insert primaryAttribute keys
264266
// $message_keys = [
265-
// $attributeKey.'.'.$ruleKey,
267+
// $attributeKey.$this->messageSeparator.$ruleKey,
266268
// >> here [1] <<
267269
// $attributeKey,
268270
// >> and here [3] <<
269271
// $ruleKey
270272
// ];
271273
$primaryAttributeKey = $primaryAttribute->getKey();
272-
array_splice($message_keys, 1, 0, $primaryAttributeKey.'.'.$ruleKey);
274+
array_splice($message_keys, 1, 0, $primaryAttributeKey.$this->messageSeparator.$ruleKey);
273275
array_splice($message_keys, 3, 0, $primaryAttributeKey);
274276
}
275277

tests/ValidatorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class ValidatorTest extends PHPUnit_Framework_TestCase
99
{
1010

11+
// TODO: blablabla
1112
protected $validator;
1213

1314
protected function setUp()
@@ -508,6 +509,28 @@ public function testSetCustomMessagesInValidation()
508509
$this->assertEquals($errors->get('comments.0.text', 'required'), 'baz');
509510
}
510511

512+
public function testSpecificRuleMessage()
513+
{
514+
$validation = $this->validator->make([
515+
'something' => 'value',
516+
], [
517+
'something' => 'email|max:3|numeric',
518+
]);
519+
520+
$validation->setMessages([
521+
'something:email' => 'foo',
522+
'something:numeric' => 'bar',
523+
'something:max' => 'baz',
524+
]);
525+
526+
$validation->validate();
527+
528+
$errors = $validation->errors();
529+
$this->assertEquals($errors->get('something', 'email'), 'foo');
530+
$this->assertEquals($errors->get('something', 'numeric'), 'bar');
531+
$this->assertEquals($errors->get('something', 'max'), 'baz');
532+
}
533+
511534
public function testSetAttributeAliases()
512535
{
513536
$validation = $this->validator->make([

0 commit comments

Comments
 (0)