Merge branch '21-wrong-input-validators-when-creating-new-callbacks' into 'dev'
Resolve "Wrong input validators when creating new callbacks." Closes #21 See merge request pnx/httpcb!19
This commit is contained in:
commit
84491b1c5e
2 changed files with 42 additions and 14 deletions
|
|
@ -6,4 +6,21 @@ use Phalcon\Mvc\Model;
|
||||||
|
|
||||||
class Base extends Model
|
class Base extends Model
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*
|
||||||
|
* Phalcon throws an exception if no valid snapshot data is present.
|
||||||
|
*
|
||||||
|
* We override this behaviour by treating no
|
||||||
|
* snapshot data (new row) as changed.
|
||||||
|
*
|
||||||
|
* @param string|array $fieldName
|
||||||
|
* @param boolean $allFields
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasChanged($fieldName = null, $allFields = false)
|
||||||
|
{
|
||||||
|
return $this->hasSnapshotData() === false
|
||||||
|
|| parent::hasChanged($fieldName, $allFields);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,12 @@
|
||||||
|
|
||||||
namespace App\Model\Data;
|
namespace App\Model\Data;
|
||||||
|
|
||||||
use Phalcon\Mvc\Model,
|
use Phalcon\Validation,
|
||||||
Phalcon\Validation,
|
|
||||||
Phalcon\Validation\Validator\Callback as CallbackValidator,
|
Phalcon\Validation\Validator\Callback as CallbackValidator,
|
||||||
InvalidArgumentException,
|
InvalidArgumentException,
|
||||||
Httpcb\OAuth\UserData\UserDataInterface;
|
Httpcb\OAuth\UserData\UserDataInterface;
|
||||||
|
|
||||||
class User extends Model
|
class User extends Base
|
||||||
{
|
{
|
||||||
const STATUS_ACTIVE = 'Active';
|
const STATUS_ACTIVE = 'Active';
|
||||||
const STATUS_DELETED = 'Deleted';
|
const STATUS_DELETED = 'Deleted';
|
||||||
|
|
@ -49,19 +48,31 @@ class User extends Model
|
||||||
|
|
||||||
public function validation()
|
public function validation()
|
||||||
{
|
{
|
||||||
// Validation
|
$rules = [
|
||||||
$validator = new Validation();
|
'username' => new CallbackValidator([
|
||||||
|
'callback' => function () {
|
||||||
$validator->add('username', new CallbackValidator([
|
return $this->findFirstByUsername($this->getUsername()) === false;
|
||||||
'callback' => function() { return $this->findFirstByUsername($this->getUsername()) === false; },
|
},
|
||||||
'message' => 'The username already exists.'
|
'message' => 'The username already exists.'
|
||||||
]));
|
]),
|
||||||
$validator->add('email', new CallbackValidator([
|
'email' => new CallbackValidator([
|
||||||
'callback' => function() { return $this->findFirstByEmail($this->getEmail()) === false; },
|
'callback' => function() {
|
||||||
|
return $this->findFirstByEmail($this->getEmail()) === false;
|
||||||
|
},
|
||||||
'message' => 'The email address already exists.'
|
'message' => 'The email address already exists.'
|
||||||
]));
|
])
|
||||||
|
];
|
||||||
|
|
||||||
return $this->validate($validator);
|
$validation = new Validation();
|
||||||
|
foreach($rules as $field => $validator) {
|
||||||
|
|
||||||
|
// Only validate changed fields.
|
||||||
|
if ($this->hasChanged($field)) {
|
||||||
|
$validation->add($field, $validator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->validate($validation);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Reference in a new issue