Skip to content

Commit

Permalink
Merge pull request #6 from langleyfoxall/feature/add-optionallyChange…
Browse files Browse the repository at this point in the history
…Password-method

Add optionallyChangePassword method
  • Loading branch information
DivineOmega authored Oct 31, 2018
2 parents 44bf0e2 + ede0ced commit 2828890
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ the following scenerios.
* Register
* Change password, with old password
* Change password, without old password
* Optionally change password, with old password
* Optionally change password, without old password
* Login



See the code below for example usage syntax.

```php
Expand All @@ -53,6 +57,18 @@ $this->validate($request, [
'password' => PasswordRules::changePassword($request->email, $request->old_password),
]);

// Optionally change password, without old password
$this->validate($request, [
'old_password' => 'required',
'password' => PasswordRules::optionallyChangePassword($request->email),
]);

// Optionally change password, with old password
$this->validate($request, [
'old_password' => 'required',
'password' => PasswordRules::optionallyChangePassword($request->email, $request->old_password),
]);

// Change password, without old password
$this->validate($request, [
'old_password' => 'required',
Expand All @@ -65,3 +81,7 @@ $this->validate($request, [
'password' => PasswordRules::login(),
]);
```

The `optionallyChangePassword` method supplies validation rules that are
appropriate for forms in which the password can be optionally changed if
filled in.
17 changes: 17 additions & 0 deletions src/PasswordRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ public static function changePassword($username, $oldPassword = null)
return $rules;
}

public static function optionallyChangePassword($username, $oldPassword = null)
{
$rules = self::changePassword($username, $oldPassword);

$rules = array_merge($rules, [
'nullable',
]);

foreach ($rules as $key => $rule) {
if (is_string($rule) && $rule === 'required') {
unset($rules[$key]);
}
}

return $rules;
}

public static function login()
{
return [
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/PasswordRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ private function getPasswordRuleSets()
PasswordRules::register('username'),
PasswordRules::changePassword('username', 'oldPassword'),
PasswordRules::changePassword('username'),
PasswordRules::optionallyChangePassword('username', 'oldPassword'),
PasswordRules::optionallyChangePassword('username'),
PasswordRules::login(),
];
}
Expand Down

0 comments on commit 2828890

Please sign in to comment.