app/models/Data/User.php: in validation() only validate changed fields.
This commit is contained in:
parent
bd51dd8700
commit
28e2201a6b
1 changed files with 23 additions and 11 deletions
|
|
@ -48,19 +48,31 @@ class User extends Base
|
||||||
|
|
||||||
public function validation()
|
public function validation()
|
||||||
{
|
{
|
||||||
// Validation
|
$rules = [
|
||||||
$validator = new Validation();
|
'username' => new CallbackValidator([
|
||||||
|
'callback' => function () {
|
||||||
|
return $this->findFirstByUsername($this->getUsername()) === false;
|
||||||
|
},
|
||||||
|
'message' => 'The username already exists.'
|
||||||
|
]),
|
||||||
|
'email' => new CallbackValidator([
|
||||||
|
'callback' => function() {
|
||||||
|
return $this->findFirstByEmail($this->getEmail()) === false;
|
||||||
|
},
|
||||||
|
'message' => 'The email address already exists.'
|
||||||
|
])
|
||||||
|
];
|
||||||
|
|
||||||
$validator->add('username', new CallbackValidator([
|
$validation = new Validation();
|
||||||
'callback' => function() { return $this->findFirstByUsername($this->getUsername()) === false; },
|
foreach($rules as $field => $validator) {
|
||||||
'message' => 'The username already exists.'
|
|
||||||
]));
|
|
||||||
$validator->add('email', new CallbackValidator([
|
|
||||||
'callback' => function() { return $this->findFirstByEmail($this->getEmail()) === false; },
|
|
||||||
'message' => 'The email address already exists.'
|
|
||||||
]));
|
|
||||||
|
|
||||||
return $this->validate($validator);
|
// Only validate changed fields.
|
||||||
|
if ($this->hasChanged($field)) {
|
||||||
|
$validation->add($field, $validator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->validate($validation);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Reference in a new issue