Skip to content

Commit 7a4d718

Browse files
authored
Merge pull request #12 from rakit/bugfix-set-messages
Bugfix setMessages
2 parents e9d602f + 3977d6a commit 7a4d718

File tree

4 files changed

+118
-24
lines changed

4 files changed

+118
-24
lines changed

src/Rules/Callable.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rakit\Validation\Rules;
4+
5+
use Rakit\Validation\Rule;
6+
use InvalidArgumentException;
7+
8+
class Callable extends Rule
9+
{
10+
11+
public function setParameters(array $params)
12+
{
13+
$this->params['callable'] = array_shift($params);
14+
$this->params['parameters'] = $params;
15+
return $this;
16+
}
17+
18+
public function parameters(array $params)
19+
{
20+
$this->params['parameters'] = $params;
21+
}
22+
23+
public function check($value)
24+
{
25+
$this->requireParameters(['callable']);
26+
27+
$callable = $this->parameter('callable');
28+
if (! is_callable($callable)) {
29+
throw new InvalidArgumentException("Parameter 1 in callable rule must be callable. ", 1);
30+
}
31+
32+
$params
33+
34+
if (! is_string($value) && ! is_numeric($value)) {
35+
return false;
36+
}
37+
38+
return preg_match('/^[\pL\pM\pN_-]+$/u', $value) > 0;
39+
}
40+
41+
}

src/Validation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function setMessage($key, $message)
187187

188188
public function setMessages(array $messages)
189189
{
190-
array_merge($this->messages, $messages);
190+
$this->messages = array_merge($this->messages, $messages);
191191
}
192192

193193
public function setAlias($attributeKey, $alias)

src/Validator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public function setMessage($key, $message)
2323
return $this->messages[$key] = $message;
2424
}
2525

26-
public function setMessages()
26+
public function setMessages($messages)
2727
{
28-
array_merge($this->messages, $messages);
28+
$this->messages = array_merge($this->messages, $messages);
2929
}
3030

3131
public function setValidator($key, Rule $rule)
@@ -46,7 +46,7 @@ public function validate(array $inputs, array $rules, array $messages = array())
4646
return $validation;
4747
}
4848

49-
public function make(array $inputs, array $rules, array $messages)
49+
public function make(array $inputs, array $rules, array $messages = array())
5050
{
5151
$messages = array_merge($this->messages, $messages);
5252
return new Validation($this, $inputs, $rules, $messages);

tests/ValidatorTest.php

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,36 +124,89 @@ public function testRulePresent()
124124
$this->assertTrue($v2->passes());
125125
}
126126

127-
public function testValidationMessages()
127+
public function testSetCustomMessagesInValidator()
128128
{
129-
$validation = $this->validator->make([
130-
'email' => '',
131-
'number' => 5
129+
$this->validator->setMessages([
130+
'required' => 'foo',
131+
'email' => 'bar'
132+
]);
133+
134+
$this->validator->setMessage('numeric', 'baz');
135+
136+
$validation = $this->validator->validate([
137+
'foo' => null,
138+
'email' => 'invalid email',
139+
'something' => 'not numeric'
132140
], [
133-
'email' => 'required|email',
134-
'number' => 'min:6|max:4|between:1,4'
141+
'foo' => 'required',
142+
'email' => 'email',
143+
'something' => 'numeric'
144+
]);
145+
146+
$errors = $validation->errors();
147+
$this->assertEquals($errors->get('foo', 'required'), 'foo');
148+
$this->assertEquals($errors->get('email', 'email'), 'bar');
149+
$this->assertEquals($errors->get('something', 'numeric'), 'baz');
150+
}
151+
152+
public function testSetCustomMessagesInValidation()
153+
{
154+
$validation = $this->validator->make([
155+
'foo' => null,
156+
'email' => 'invalid email',
157+
'something' => 'not numeric'
135158
], [
136-
'email.required' => 'Kolom email tidak boleh kosong',
137-
'required' => ':attribute harus diisi',
138-
'number.max' => 'number > :max',
139-
'number.min' => 'number < :min',
140-
'number.between' => ':min - :max'
159+
'foo' => 'required',
160+
'email' => 'email',
161+
'something' => 'numeric'
141162
]);
142163

143-
$validation->setAlias('email', 'e-mail');
164+
$validation->setMessages([
165+
'required' => 'foo',
166+
'email' => 'bar'
167+
]);
168+
169+
$validation->setMessage('numeric', 'baz');
170+
144171
$validation->validate();
145172

146173
$errors = $validation->errors();
174+
$this->assertEquals($errors->get('foo', 'required'), 'foo');
175+
$this->assertEquals($errors->get('email', 'email'), 'bar');
176+
$this->assertEquals($errors->get('something', 'numeric'), 'baz');
177+
}
147178

148-
$first_error = $errors->first('email');
149-
$error_required = $errors->get('email', 'required');
150-
151-
$this->assertEquals($first_error, 'Kolom email tidak boleh kosong');
152-
$this->assertEquals($error_required, 'Kolom email tidak boleh kosong');
179+
public function testSetAttributeAliases()
180+
{
181+
$validation = $this->validator->make([
182+
'foo' => null,
183+
'email' => 'invalid email',
184+
'something' => 'not numeric'
185+
], [
186+
'foo' => 'required',
187+
'email' => 'email',
188+
'something' => 'numeric'
189+
]);
190+
191+
$validation->setMessages([
192+
'required' => ':attribute foo',
193+
'email' => ':attribute bar',
194+
'numeric' => ':attribute baz'
195+
]);
196+
197+
$validation->setAliases([
198+
'foo' => 'Foo',
199+
'email' => 'Bar',
200+
]);
201+
202+
$validation->setAlias('something', 'Baz');
153203

154-
$this->assertEquals($errors->get('number', 'max'), 'number > 4');
155-
$this->assertEquals($errors->get('number', 'min'), 'number < 6');
156-
$this->assertEquals($errors->get('number', 'between'), '1 - 4');
204+
$validation->validate();
205+
206+
$errors = $validation->errors();
207+
$this->assertEquals($errors->get('foo', 'required'), 'Foo foo');
208+
$this->assertEquals($errors->get('email', 'email'), 'Bar bar');
209+
$this->assertEquals($errors->get('something', 'numeric'), 'Baz baz');
157210
}
158211

159212
/**

0 commit comments

Comments
 (0)