Skip to content

Commit f3add9f

Browse files
committed
Fixing resolve message and attribute alias for array attribute
1 parent f2ebdc0 commit f3add9f

File tree

3 files changed

+186
-97
lines changed

3 files changed

+186
-97
lines changed

src/Attribute.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class Attribute
1515

1616
protected $required = false;
1717

18+
protected $primaryAttribute = null;
19+
20+
protected $otherAttributes = [];
21+
1822
public function __construct(Validation $validation, $key, $alias = null, array $rules = array())
1923
{
2024
$this->validation = $validation;
@@ -25,6 +29,34 @@ public function __construct(Validation $validation, $key, $alias = null, array $
2529
}
2630
}
2731

32+
public function setPrimaryAttribute(Attribute $primaryAttribute)
33+
{
34+
$this->primaryAttribute = $primaryAttribute;
35+
}
36+
37+
public function getPrimaryAttribute()
38+
{
39+
return $this->primaryAttribute;
40+
}
41+
42+
public function setOtherAttributes(array $otherAttributes)
43+
{
44+
$this->otherAttributes = [];
45+
foreach($otherAttributes as $otherAttribute) {
46+
$this->addOtherAttribute($otherAttribute);
47+
}
48+
}
49+
50+
public function addOtherAttribute(Attribute $otherAttribute)
51+
{
52+
$this->otherAttributes[] = $otherAttribute;
53+
}
54+
55+
public function getOtherAttributes()
56+
{
57+
return $this->otherAttributes;
58+
}
59+
2860
public function addRule(Rule $rule)
2961
{
3062
$rule->setAttribute($this);

src/Validation.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,12 @@ public function errors()
5454
return $this->errors;
5555
}
5656

57-
protected function validateAttribute(Attribute $attribute, Attribute $primaryAttribute = null, array $otherAttributes = [])
57+
protected function validateAttribute(Attribute $attribute)
5858
{
5959
if ($this->isArrayAttribute($attribute)) {
6060
$attributes = $this->parseArrayAttribute($attribute);
6161
foreach($attributes as $i => $attr) {
62-
$otherAttributes = $attributes;
63-
unset($otherAttributes[$i]);
64-
$this->validateAttribute($attr, $attribute, $otherAttributes);
62+
$this->validateAttribute($attr);
6563
}
6664
return;
6765
}
@@ -79,9 +77,7 @@ protected function validateAttribute(Attribute $attribute, Attribute $primaryAtt
7977
$valid = $ruleValidator->check($value);
8078

8179
if (!$valid) {
82-
$rulename = $ruleValidator->getKey();
83-
$message = $this->resolveMessage($attribute, $value, $ruleValidator);
84-
$this->errors->add($attributeKey, $rulename, $message);
80+
$this->addError($attribute, $value, $ruleValidator);
8581

8682
if ($ruleValidator->isImplicit()) {
8783
break;
@@ -109,10 +105,19 @@ protected function parseArrayAttribute(Attribute $attribute)
109105

110106
foreach ($data as $key => $value) {
111107
if ((bool) preg_match('/^'.$pattern.'\z/', $key)) {
112-
$attributes[] = new Attribute($this, $key, null, $attribute->getRules());
108+
$attr = new Attribute($this, $key, null, $attribute->getRules());
109+
$attr->setPrimaryAttribute($attribute);
110+
$attributes[] = $attr;
113111
}
114112
}
115113

114+
// set other attributes to each attributes
115+
foreach ($attributes as $i => $attr) {
116+
$otherAttributes = $attributes;
117+
unset($otherAttributes[$i]);
118+
$attr->setOtherAttributes($otherAttributes);
119+
}
120+
116121
return $attributes;
117122
}
118123

@@ -207,6 +212,14 @@ protected function extractDataFromPath($attributeKey)
207212
return $results;
208213
}
209214

215+
protected function addError(Attribute $attribute, $value, Rule $ruleValidator)
216+
{
217+
$ruleName = $ruleValidator->getKey();
218+
$message = $this->resolveMessage($attribute, $value, $ruleValidator);
219+
220+
$this->errors->add($attribute->getKey(), $ruleName, $message);
221+
}
222+
210223
protected function isEmptyValue($value)
211224
{
212225
$requiredValidator = new Required;
@@ -220,24 +233,46 @@ protected function ruleIsOptional(Attribute $attribute, Rule $rule)
220233
false === $rule instanceof Required;
221234
}
222235

223-
protected function resolveAttributeName($attributeKey)
236+
protected function resolveAttributeName(Attribute $attribute)
224237
{
225-
return isset($this->aliases[$attributeKey]) ? $this->aliases[$attributeKey] : ucfirst(str_replace('_', ' ', $attributeKey));
238+
$primaryAttribute = $attribute->getPrimaryAttribute();
239+
if (isset($this->aliases[$attribute->getKey()])) {
240+
return $this->aliases[$attribute->getKey()];
241+
} elseif($primaryAttribute AND isset($this->aliases[$primaryAttribute->getKey()])) {
242+
return $this->aliases[$primaryAttribute->getKey()];
243+
} else {
244+
return ucfirst(str_replace('_', ' ', $attribute->getKey()));
245+
}
226246
}
227247

228248
protected function resolveMessage(Attribute $attribute, $value, Rule $validator)
229249
{
250+
$primaryAttribute = $attribute->getPrimaryAttribute();
230251
$params = $validator->getParameters();
231252
$attributeKey = $attribute->getKey();
232253
$ruleKey = $validator->getKey();
233-
$alias = $attribute->getAlias() ?: $this->resolveAttributeName($attributeKey);
254+
$alias = $attribute->getAlias() ?: $this->resolveAttributeName($attribute);
234255
$message = $validator->getMessage(); // default rule message
235256
$message_keys = [
236257
$attributeKey.'.'.$ruleKey,
237-
$attributeKey.'.*',
258+
$attributeKey,
238259
$ruleKey
239260
];
240261

262+
if ($primaryAttribute) {
263+
// insert primaryAttribute keys
264+
// $message_keys = [
265+
// $attributeKey.'.'.$ruleKey,
266+
// >> here [1] <<
267+
// $attributeKey,
268+
// >> and here [3] <<
269+
// $ruleKey
270+
// ];
271+
$primaryAttributeKey = $primaryAttribute->getKey();
272+
array_splice($message_keys, 1, 0, $primaryAttributeKey.'.'.$ruleKey);
273+
array_splice($message_keys, 3, 0, $primaryAttributeKey);
274+
}
275+
241276
foreach($message_keys as $key) {
242277
if (isset($this->messages[$key])) {
243278
$message = $this->messages[$key];

tests/ValidatorTest.php

Lines changed: 107 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -174,91 +174,6 @@ public function testRulePresent()
174174
$this->assertTrue($v2->passes());
175175
}
176176

177-
public function testSetCustomMessagesInValidator()
178-
{
179-
$this->validator->setMessages([
180-
'required' => 'foo',
181-
'email' => 'bar'
182-
]);
183-
184-
$this->validator->setMessage('numeric', 'baz');
185-
186-
$validation = $this->validator->validate([
187-
'foo' => null,
188-
'email' => 'invalid email',
189-
'something' => 'not numeric'
190-
], [
191-
'foo' => 'required',
192-
'email' => 'email',
193-
'something' => 'numeric'
194-
]);
195-
196-
$errors = $validation->errors();
197-
$this->assertEquals($errors->get('foo', 'required'), 'foo');
198-
$this->assertEquals($errors->get('email', 'email'), 'bar');
199-
$this->assertEquals($errors->get('something', 'numeric'), 'baz');
200-
}
201-
202-
public function testSetCustomMessagesInValidation()
203-
{
204-
$validation = $this->validator->make([
205-
'foo' => null,
206-
'email' => 'invalid email',
207-
'something' => 'not numeric'
208-
], [
209-
'foo' => 'required',
210-
'email' => 'email',
211-
'something' => 'numeric'
212-
]);
213-
214-
$validation->setMessages([
215-
'required' => 'foo',
216-
'email' => 'bar'
217-
]);
218-
219-
$validation->setMessage('numeric', 'baz');
220-
221-
$validation->validate();
222-
223-
$errors = $validation->errors();
224-
$this->assertEquals($errors->get('foo', 'required'), 'foo');
225-
$this->assertEquals($errors->get('email', 'email'), 'bar');
226-
$this->assertEquals($errors->get('something', 'numeric'), 'baz');
227-
}
228-
229-
public function testSetAttributeAliases()
230-
{
231-
$validation = $this->validator->make([
232-
'foo' => null,
233-
'email' => 'invalid email',
234-
'something' => 'not numeric'
235-
], [
236-
'foo' => 'required',
237-
'email' => 'email',
238-
'something' => 'numeric'
239-
]);
240-
241-
$validation->setMessages([
242-
'required' => ':attribute foo',
243-
'email' => ':attribute bar',
244-
'numeric' => ':attribute baz'
245-
]);
246-
247-
$validation->setAliases([
248-
'foo' => 'Foo',
249-
'email' => 'Bar',
250-
]);
251-
252-
$validation->setAlias('something', 'Baz');
253-
254-
$validation->validate();
255-
256-
$errors = $validation->errors();
257-
$this->assertEquals($errors->get('foo', 'required'), 'Foo foo');
258-
$this->assertEquals($errors->get('email', 'email'), 'Bar bar');
259-
$this->assertEquals($errors->get('something', 'numeric'), 'Baz baz');
260-
}
261-
262177
/**
263178
* @expectedException \Rakit\Validation\RuleNotFoundException
264179
*/
@@ -526,4 +441,111 @@ public function testArrayValidation()
526441
$this->assertNotNull($errors->get('cart_items.3.qty', 'numeric'));
527442
$this->assertNotNull($errors->get('cart_items.4.id_product', 'numeric'));
528443
}
444+
445+
public function testSetCustomMessagesInValidator()
446+
{
447+
$this->validator->setMessages([
448+
'required' => 'foo',
449+
'email' => 'bar',
450+
'comments.*.text' => 'baz'
451+
]);
452+
453+
$this->validator->setMessage('numeric', 'baz');
454+
455+
$validation = $this->validator->validate([
456+
'foo' => null,
457+
'email' => 'invalid email',
458+
'something' => 'not numeric',
459+
'comments' => [
460+
['id' => 4, 'text' => ''],
461+
['id' => 5, 'text' => 'foo'],
462+
]
463+
], [
464+
'foo' => 'required',
465+
'email' => 'email',
466+
'something' => 'numeric',
467+
'comments.*.text' => 'required'
468+
]);
469+
470+
$errors = $validation->errors();
471+
$this->assertEquals($errors->get('foo', 'required'), 'foo');
472+
$this->assertEquals($errors->get('email', 'email'), 'bar');
473+
$this->assertEquals($errors->get('something', 'numeric'), 'baz');
474+
$this->assertEquals($errors->get('comments.0.text', 'required'), 'baz');
475+
}
476+
477+
public function testSetCustomMessagesInValidation()
478+
{
479+
$validation = $this->validator->make([
480+
'foo' => null,
481+
'email' => 'invalid email',
482+
'something' => 'not numeric',
483+
'comments' => [
484+
['id' => 4, 'text' => ''],
485+
['id' => 5, 'text' => 'foo'],
486+
]
487+
], [
488+
'foo' => 'required',
489+
'email' => 'email',
490+
'something' => 'numeric',
491+
'comments.*.text' => 'required'
492+
]);
493+
494+
$validation->setMessages([
495+
'required' => 'foo',
496+
'email' => 'bar',
497+
'comments.*.text' => 'baz'
498+
]);
499+
500+
$validation->setMessage('numeric', 'baz');
501+
502+
$validation->validate();
503+
504+
$errors = $validation->errors();
505+
$this->assertEquals($errors->get('foo', 'required'), 'foo');
506+
$this->assertEquals($errors->get('email', 'email'), 'bar');
507+
$this->assertEquals($errors->get('something', 'numeric'), 'baz');
508+
$this->assertEquals($errors->get('comments.0.text', 'required'), 'baz');
509+
}
510+
511+
public function testSetAttributeAliases()
512+
{
513+
$validation = $this->validator->make([
514+
'foo' => null,
515+
'email' => 'invalid email',
516+
'something' => 'not numeric',
517+
'comments' => [
518+
['id' => 4, 'text' => ''],
519+
['id' => 5, 'text' => 'foo'],
520+
]
521+
], [
522+
'foo' => 'required',
523+
'email' => 'email',
524+
'something' => 'numeric',
525+
'comments.*.text' => 'required'
526+
]);
527+
528+
$validation->setMessages([
529+
'required' => ':attribute foo',
530+
'email' => ':attribute bar',
531+
'numeric' => ':attribute baz',
532+
'comments.*.text' => ':attribute qux'
533+
]);
534+
535+
$validation->setAliases([
536+
'foo' => 'Foo',
537+
'email' => 'Bar'
538+
]);
539+
540+
$validation->setAlias('something', 'Baz');
541+
$validation->setAlias('comments.*.text', 'Qux');
542+
543+
$validation->validate();
544+
545+
$errors = $validation->errors();
546+
$this->assertEquals($errors->get('foo', 'required'), 'Foo foo');
547+
$this->assertEquals($errors->get('email', 'email'), 'Bar bar');
548+
$this->assertEquals($errors->get('something', 'numeric'), 'Baz baz');
549+
$this->assertEquals($errors->get('comments.0.text', 'required'), 'Qux qux');
550+
}
529551
}

0 commit comments

Comments
 (0)