app/forms/UserSettings.php: Make the form work correctly with null or "empty" user object.
This commit is contained in:
parent
599eb17f69
commit
c6704f5f4a
1 changed files with 58 additions and 28 deletions
|
|
@ -50,19 +50,24 @@ class UserSettings extends FormBase
|
||||||
|
|
||||||
public function initialize()
|
public function initialize()
|
||||||
{
|
{
|
||||||
|
$entity = $this->getEntity();
|
||||||
|
|
||||||
$this->setValidation(new \Phalcon\Validation());
|
$this->setValidation(new \Phalcon\Validation());
|
||||||
|
|
||||||
// Id
|
// Id
|
||||||
$id = new Text('id', array(
|
if ($entity && $entity->getId()) {
|
||||||
'class' => 'form-control',
|
$id = new Text('id', array(
|
||||||
'readonly' => '',
|
'class' => 'form-control',
|
||||||
));
|
'readonly' => '',
|
||||||
$id->addValidator(new IdenticalValidator([
|
));
|
||||||
'accepted' => $this->getEntity()->getId(),
|
|
||||||
]));
|
|
||||||
|
|
||||||
$id->setLabel('ID');
|
$id->addValidator(new IdenticalValidator([
|
||||||
$this->add($id);
|
'accepted' => $entity->getId(),
|
||||||
|
]));
|
||||||
|
|
||||||
|
$id->setLabel('ID');
|
||||||
|
$this->add($id);
|
||||||
|
}
|
||||||
|
|
||||||
// Username
|
// Username
|
||||||
$username = new Text('username', array(
|
$username = new Text('username', array(
|
||||||
|
|
@ -72,16 +77,20 @@ class UserSettings extends FormBase
|
||||||
|
|
||||||
$username->setLabel('Username');
|
$username->setLabel('Username');
|
||||||
|
|
||||||
$username->addValidator(new AlnumValidator());
|
$validator_options = array(
|
||||||
|
|
||||||
$validator = new UniquenessValidator(array(
|
|
||||||
'model' => new UserModel(),
|
'model' => new UserModel(),
|
||||||
'message' => 'The username already exists.',
|
'message' => 'The :field already exists.',
|
||||||
'attribute' => 'username',
|
'attribute' => 'username',
|
||||||
'except' => [ $this->getEntity()->getUsername() ]
|
);
|
||||||
));
|
|
||||||
|
|
||||||
$username->addValidator($validator);
|
if ($entity && strlen($entity->getUsername())) {
|
||||||
|
$validator_options['except'] = [ $entity->getUsername() ];
|
||||||
|
}
|
||||||
|
|
||||||
|
$username->addValidators([
|
||||||
|
new AlnumValidator(),
|
||||||
|
new UniquenessValidator($validator_options)
|
||||||
|
]);
|
||||||
|
|
||||||
$this->add($username);
|
$this->add($username);
|
||||||
|
|
||||||
|
|
@ -100,18 +109,39 @@ class UserSettings extends FormBase
|
||||||
$this->add($name);
|
$this->add($name);
|
||||||
|
|
||||||
// Email
|
// Email
|
||||||
$email = new Text('email', array(
|
if ($this->_admin === false && $entity) {
|
||||||
'class' => 'form-control',
|
$email = new Text('email', array(
|
||||||
'placeholder' => 'Email',
|
'class' => 'form-control',
|
||||||
'readonly' => '',
|
'placeholder' => 'Email',
|
||||||
));
|
'readonly' => '',
|
||||||
|
));
|
||||||
|
|
||||||
$email->addValidator(new IdenticalValidator([
|
$email->addValidator(new IdenticalValidator([
|
||||||
'accepted' => $this->getEntity()->getEmail(),
|
'accepted' => $entity->getEmail(),
|
||||||
]));
|
]));
|
||||||
|
} else {
|
||||||
|
$email = new Text('email', array(
|
||||||
|
'class' => 'form-control',
|
||||||
|
'placeholder' => 'Email',
|
||||||
|
));
|
||||||
|
|
||||||
|
$validator_options = [
|
||||||
|
'model' => new UserModel(),
|
||||||
|
'message' => 'The :field already exists.',
|
||||||
|
'attribute' => 'email',
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($entity && strlen($entity->getEmail())) {
|
||||||
|
$validator_options['except'] = [ $entity->getEmail() ];
|
||||||
|
}
|
||||||
|
|
||||||
|
$email->addValidators([
|
||||||
|
new EmailValidator(),
|
||||||
|
new UniquenessValidator($validator_options)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
$email->setLabel('Email');
|
$email->setLabel('Email');
|
||||||
|
|
||||||
$this->add($email);
|
$this->add($email);
|
||||||
|
|
||||||
// Passwords
|
// Passwords
|
||||||
|
|
@ -127,10 +157,10 @@ class UserSettings extends FormBase
|
||||||
*/
|
*/
|
||||||
protected function _passwords()
|
protected function _passwords()
|
||||||
{
|
{
|
||||||
$current_pw = $this->getEntity()->getPassword();
|
$entity = $this->getEntity();
|
||||||
|
|
||||||
// Current
|
// Current
|
||||||
if ($this->_admin === false && strlen($current_pw) > 0) {
|
if ($this->_admin === false && $entity && strlen($entity->getPassword()) > 0) {
|
||||||
$current = new Password('passwordCurrent', array(
|
$current = new Password('passwordCurrent', array(
|
||||||
'class' => 'form-control',
|
'class' => 'form-control',
|
||||||
));
|
));
|
||||||
|
|
@ -155,7 +185,7 @@ class UserSettings extends FormBase
|
||||||
// Validation
|
// Validation
|
||||||
$validation = $this->getValidation();
|
$validation = $this->getValidation();
|
||||||
|
|
||||||
if ($this->_admin === false && strlen($current_pw) > 0) {
|
if ($this->_admin === false && $entity && strlen($entity->getPassword()) > 0) {
|
||||||
$validation->add('passwordCurrent', new CallbackValidator([
|
$validation->add('passwordCurrent', new CallbackValidator([
|
||||||
'callback' => function($data) {
|
'callback' => function($data) {
|
||||||
$new_pw = $data['passwordNew'];
|
$new_pw = $data['passwordNew'];
|
||||||
|
|
|
||||||
Reference in a new issue