Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3259346

Browse filesBrowse files
mrvipchiencuong.tt
andauthored
[12.x] Fix required and sometimes validation of Password rule (#58034)
* Fix required and sometimes validation options to Password rule * Update password validation tests for required and sometimes rules --------- Co-authored-by: cuong.tt <cuong.tt@gmail.com>
1 parent a30a89f commit 3259346
Copy full SHA for 3259346

File tree

Expand file treeCollapse file tree

2 files changed

+100
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+100
-8
lines changed
Open diff view settings
Collapse file

‎src/Illuminate/Validation/Rules/Password.php‎

Copy file name to clipboardExpand all lines: src/Illuminate/Validation/Rules/Password.php
+28-4Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ class Password implements Rule, DataAwareRule, ValidatorAwareRule
4444
*/
4545
protected $max;
4646

47+
/**
48+
* If the password is required.
49+
*
50+
* @var bool
51+
*/
52+
protected $required = false;
53+
54+
/**
55+
* If the password should only be validated when present.
56+
*
57+
* @var bool
58+
*/
59+
protected $sometimes = false;
60+
4761
/**
4862
* If the password requires at least one uppercase and one lowercase letter.
4963
*
@@ -155,21 +169,29 @@ public static function default()
155169
/**
156170
* Get the default configuration of the password rule and mark the field as required.
157171
*
158-
* @return array
172+
* @return static
159173
*/
160174
public static function required()
161175
{
162-
return ['required', static::default()];
176+
$password = static::default();
177+
178+
$password->required = true;
179+
180+
return $password;
163181
}
164182

165183
/**
166184
* Get the default configuration of the password rule and mark the field as sometimes being required.
167185
*
168-
* @return array
186+
* @return static
169187
*/
170188
public static function sometimes()
171189
{
172-
return ['sometimes', static::default()];
190+
$password = static::default();
191+
192+
$password->sometimes = true;
193+
194+
return $password;
173195
}
174196

175197
/**
@@ -312,6 +334,8 @@ public function passes($attribute, $value)
312334
$validator = Validator::make(
313335
$this->data,
314336
[$attribute => [
337+
...($this->required ? ['required'] : []),
338+
...($this->sometimes ? ['sometimes'] : []),
315339
'string',
316340
'min:'.$this->min,
317341
...($this->max ? ['max:'.$this->max] : []),
Collapse file

‎tests/Validation/ValidationPasswordRuleTest.php‎

Copy file name to clipboardExpand all lines: tests/Validation/ValidationPasswordRuleTest.php
+72-4Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,14 @@ public function testItCanSetDefaultUsing()
237237

238238
$this->passes(Password::default(), ['abcd', '454qb^', '接2133手田']);
239239
$this->assertSame($password, Password::default());
240-
$this->assertSame(['required', $password], Password::required());
241-
$this->assertSame(['sometimes', $password], Password::sometimes());
240+
$this->assertInstanceOf(Password::class, Password::required());
241+
$this->assertInstanceOf(Password::class, Password::sometimes());
242242

243243
Password::defaults($password2);
244244
$this->passes(Password::default(), ['Nn', 'Mn', 'âA']);
245245
$this->assertSame($password2, Password::default());
246-
$this->assertSame(['required', $password2], Password::required());
247-
$this->assertSame(['sometimes', $password2], Password::sometimes());
246+
$this->assertInstanceOf(Password::class, Password::required());
247+
$this->assertInstanceOf(Password::class, Password::sometimes());
248248
}
249249

250250
public function testItCannotSetDefaultUsingGivenString()
@@ -375,6 +375,74 @@ public function testCanRetrieveAllRulesApplied()
375375
]);
376376
}
377377

378+
public function testRequired()
379+
{
380+
$this->fails(Password::required(), [null], [
381+
'validation.required',
382+
]);
383+
384+
$this->passes(Password::required(), ['12345678', 'password123']);
385+
386+
$this->fails([Password::required()], ['short'], [
387+
'validation.min.string',
388+
]);
389+
390+
$this->passes(Password::required()->mixedCase()->numbers(), ['Password1']);
391+
392+
// Ensure it still correct when using array
393+
$this->passes([Password::required()], ['12345678', 'password123']);
394+
395+
$this->fails([Password::required()], ['short'], [
396+
'validation.min.string',
397+
]);
398+
399+
$this->passes(['string', Password::required()], ['12345678', 'password123']);
400+
401+
$this->passes([Password::required()->mixedCase()->numbers()], ['Password1']);
402+
403+
// Test with custom defaults
404+
Password::defaults(Password::min(6)->letters());
405+
406+
$this->fails(Password::required(), [null], [
407+
'validation.required',
408+
]);
409+
410+
$this->passes(Password::required(), ['Password123', 'password123']);
411+
$this->passes([Password::required()], ['Password123', 'password123']);
412+
}
413+
414+
public function testSometimes()
415+
{
416+
$this->fails(Password::sometimes(), ['short'], [
417+
'validation.min.string',
418+
]);
419+
420+
$this->passes(Password::sometimes(), ['12345678', 'password123']);
421+
422+
$this->fails([Password::sometimes()], ['12345'], [
423+
'validation.min.string',
424+
]);
425+
426+
$this->passes(Password::sometimes()->mixedCase()->numbers(), ['Password1']);
427+
428+
// Ensure it still correct when using array
429+
$this->passes([Password::sometimes()], ['12345678', 'password123']);
430+
431+
$this->fails([Password::sometimes()], ['12345'], [
432+
'validation.min.string',
433+
]);
434+
435+
$this->passes(['string', Password::sometimes()], ['12345678', 'password123']);
436+
437+
$this->passes([Password::sometimes()->mixedCase()->numbers()], ['Password1']);
438+
439+
// Test with custom defaults
440+
Password::defaults(Password::min(6)->letters());
441+
442+
$this->passes(Password::sometimes(), ['Password123', 'password123']);
443+
$this->passes([Password::sometimes()], ['Password123', 'password123']);
444+
}
445+
378446
protected function passes($rule, $values)
379447
{
380448
$this->assertValidationRules($rule, $values, true, []);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.