zf2 form to create user accounts with optional password change
Posted on July 22, 2013
Here is an example to create an ZF2 form to create user accounts with the option to change the account without changing the password.
User form class
namespace ExampleForm;
use ZendFormForm;
use ZendFormElement;
class User extends Form
{
public function __construct()
{
parent::__construct('user');
$this->setAttribute('method', 'post');
$this->setInputFilter(new BisAdminFormUserFilter());
$this->add(array(
'name' => 'userName',
'attributes' => array(
'type' => 'text',
'id' => 'username',
'class' => 'txt',
'size' => '50',
),
'options' => array(
'label' => 'Username'
),
));
$this->add(array(
'name' => 'fullName',
'attributes' => array(
'type' => 'text',
'id' => 'fullname',
'class' => 'txt',
'size' => '50',
),
'options' => array(
'label' => 'Fullname'
),
));
$this->add(array(
'name' => 'password',
'attributes' => array(
'type' => 'password',
'id' => 'password1',
'class' => 'txt',
),
'options' => array(
'label' => 'Password'
),
));
$this->add(array(
'name' => 'password_verify',
'attributes' => array(
'type' => 'password',
'id' => 'password',
'class' => 'txt',
),
'options' => array(
'label' => 'Password (verify)'
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save',
'class' => 'button',
),
));
}
}
User input filter class
namespace ExampleForm;
use ZendFormForm;
use ZendInputFilterInputFilter;
class UserFilter extends InputFilter
{
public function __construct()
{
$this->add(array(
'name' => 'userName',
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
ZendValidatorNotEmpty::IS_EMPTY => 'Username cannot be empty.'
)
)
),
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 3,
'max' => 50,
'messages' => array(
ZendValidatorStringLength::TOO_SHORT => 'Username is to short, use a minimum of 3 characters.',
ZendValidatorStringLength::TOO_LONG => 'Username is to long, use a maximum of 50 characters.'
)
),
)
)
));
$this->add(array(
'name' => 'fullName',
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
ZendValidatorNotEmpty::IS_EMPTY => 'Fullname cannot be empty.'
)
)
)
)
));
$this->add(array(
'name' => 'password',
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
ZendValidatorNotEmpty::IS_EMPTY => 'The password cannot be empty.'
)
)
)
)
));
$this->add(array(
'name' => 'password_verify',
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
ZendValidatorNotEmpty::IS_EMPTY => 'The verify password cannot be empty.'
)
)
),
array(
'name' => 'Identical',
'options' => array(
'token' => 'password',
'messages' => array(
ZendValidatorIdentical::NOT_SAME => 'The passwords are not the same.',
)
)
),
)
));
}
}
Controller Code
public function usereditAction(){
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('config', array('action' => 'userlist'));
}
$objectManager = $this->getServiceLocator()->get('DoctrineORMEntityManager');
$user = $objectManager->find('ExampleEntityUser',$id);
$form = new ExampleFormUser();
$form->setHydrator(new ZendStdlibHydratorReflection());
$form->bind($user);
if ($this->getRequest()->isPost()) {
$form->setData($this->getRequest()->getPost());
if ($form->get("password")->getValue() == ""){
$form->getInputFilter()->remove('password');
$form->getInputFilter()->remove('password_verify');
}
if ($form->isValid()) {
//var_dump($form->getData());
$objectManager->persist($user);
$objectManager->flush();
$this->flashMessenger()->addMessage('Changes saved');
return $this->redirect()->toRoute('config',array('action'=>'userlist'));
} else {
$form->get("password")->setValue("");
return array(
'userForm' => $form,
'flashMessages' => $this->flashMessenger()->getMessages(),
);
}
} else {
$form->get("password")->setValue("");
return array(
'userForm' => $form,
'flashMessages' => $this->flashMessenger()->getMessages(),
);
}
}
View code
Edit Useraccount
<?php
$form = $this->userForm;
$form->prepare();
$form->setAttribute('method', 'post');
$formerrors = $this->formElementErrors()->setMessageOpenFormat('')
->setMessageSeparatorString('')
->setMessageCloseString('')
->render($form->get('userName'));
$formerrors .= $this->formElementErrors()->setMessageOpenFormat('')
->setMessageSeparatorString('')
->setMessageCloseString('')
->render($form->get('fullName'));
$formerrors .= $this->formElementErrors()->setMessageOpenFormat('')
->setMessageSeparatorString('')
->setMessageCloseString('')
->render($form->get('password'));
$formerrors .= $this->formElementErrors()->setMessageOpenFormat('')
->setMessageSeparatorString('')
->setMessageCloseString('')
->render($form->get('password2'));
$formerrors .= $this->formElementErrors()->setMessageOpenFormat('')
->setMessageSeparatorString('')
->setMessageCloseString('')
->render($form->get('active'));
?>
<?php if($formerrors != ""): ?>
<?php echo $formerrors?>
<?php endif ?>
<?php echo $this->form()->openTag($form) ?>
<?php echo $this->formLabel($form->get('userName')) ?> <?php echo $this->formInput($form->get('userName')) ?>
<?php echo $this->formLabel($form->get('fullName')) ?> <?php echo $this->formInput($form->get('fullName')) ?>
Use only to change your password, otherwise leave them empty
<?php echo $this->formLabel($form->get('password')) ?> <?php echo $this->formInput($form->get('password')) ?>
<?php echo $this->formLabel($form->get('password_verify')) ?> <?php echo $this->formInput($form->get('password_verify')) ?>
<?php echo $this->formLabel($form->get('active')) ?> <?php echo $this->formSelect($form->get('active')) ?>
<?php echo $this->formSubmit($form->get('submit')) ?>
<?php echo $this->form()->closeTag() ?>
namespace ExampleForm;
use ZendFormForm;
use ZendFormElement;
class User extends Form
{
public function __construct()
{
parent::__construct('user');
$this->setAttribute('method', 'post');
$this->setInputFilter(new BisAdminFormUserFilter());
$this->add(array(
'name' => 'userName',
'attributes' => array(
'type' => 'text',
'id' => 'username',
'class' => 'txt',
'size' => '50',
),
'options' => array(
'label' => 'Username'
),
));
$this->add(array(
'name' => 'fullName',
'attributes' => array(
'type' => 'text',
'id' => 'fullname',
'class' => 'txt',
'size' => '50',
),
'options' => array(
'label' => 'Fullname'
),
));
$this->add(array(
'name' => 'password',
'attributes' => array(
'type' => 'password',
'id' => 'password1',
'class' => 'txt',
),
'options' => array(
'label' => 'Password'
),
));
$this->add(array(
'name' => 'password_verify',
'attributes' => array(
'type' => 'password',
'id' => 'password',
'class' => 'txt',
),
'options' => array(
'label' => 'Password (verify)'
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save',
'class' => 'button',
),
));
}
}
User input filter class
namespace ExampleForm;
use ZendFormForm;
use ZendInputFilterInputFilter;
class UserFilter extends InputFilter
{
public function __construct()
{
$this->add(array(
'name' => 'userName',
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
ZendValidatorNotEmpty::IS_EMPTY => 'Username cannot be empty.'
)
)
),
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 3,
'max' => 50,
'messages' => array(
ZendValidatorStringLength::TOO_SHORT => 'Username is to short, use a minimum of 3 characters.',
ZendValidatorStringLength::TOO_LONG => 'Username is to long, use a maximum of 50 characters.'
)
),
)
)
));
$this->add(array(
'name' => 'fullName',
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
ZendValidatorNotEmpty::IS_EMPTY => 'Fullname cannot be empty.'
)
)
)
)
));
$this->add(array(
'name' => 'password',
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
ZendValidatorNotEmpty::IS_EMPTY => 'The password cannot be empty.'
)
)
)
)
));
$this->add(array(
'name' => 'password_verify',
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
ZendValidatorNotEmpty::IS_EMPTY => 'The verify password cannot be empty.'
)
)
),
array(
'name' => 'Identical',
'options' => array(
'token' => 'password',
'messages' => array(
ZendValidatorIdentical::NOT_SAME => 'The passwords are not the same.',
)
)
),
)
));
}
}
Controller Code
public function usereditAction(){
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('config', array('action' => 'userlist'));
}
$objectManager = $this->getServiceLocator()->get('DoctrineORMEntityManager');
$user = $objectManager->find('ExampleEntityUser',$id);
$form = new ExampleFormUser();
$form->setHydrator(new ZendStdlibHydratorReflection());
$form->bind($user);
if ($this->getRequest()->isPost()) {
$form->setData($this->getRequest()->getPost());
if ($form->get("password")->getValue() == ""){
$form->getInputFilter()->remove('password');
$form->getInputFilter()->remove('password_verify');
}
if ($form->isValid()) {
//var_dump($form->getData());
$objectManager->persist($user);
$objectManager->flush();
$this->flashMessenger()->addMessage('Changes saved');
return $this->redirect()->toRoute('config',array('action'=>'userlist'));
} else {
$form->get("password")->setValue("");
return array(
'userForm' => $form,
'flashMessages' => $this->flashMessenger()->getMessages(),
);
}
} else {
$form->get("password")->setValue("");
return array(
'userForm' => $form,
'flashMessages' => $this->flashMessenger()->getMessages(),
);
}
}
View code
Edit Useraccount
<?php
$form = $this->userForm;
$form->prepare();
$form->setAttribute('method', 'post');
$formerrors = $this->formElementErrors()->setMessageOpenFormat('')
->setMessageSeparatorString('')
->setMessageCloseString('')
->render($form->get('userName'));
$formerrors .= $this->formElementErrors()->setMessageOpenFormat('')
->setMessageSeparatorString('')
->setMessageCloseString('')
->render($form->get('fullName'));
$formerrors .= $this->formElementErrors()->setMessageOpenFormat('')
->setMessageSeparatorString('')
->setMessageCloseString('')
->render($form->get('password'));
$formerrors .= $this->formElementErrors()->setMessageOpenFormat('')
->setMessageSeparatorString('')
->setMessageCloseString('')
->render($form->get('password2'));
$formerrors .= $this->formElementErrors()->setMessageOpenFormat('')
->setMessageSeparatorString('')
->setMessageCloseString('')
->render($form->get('active'));
?>
<?php if($formerrors != ""): ?>
<?php echo $formerrors?>
<?php endif ?>
<?php echo $this->form()->openTag($form) ?>
<?php echo $this->formLabel($form->get('userName')) ?> <?php echo $this->formInput($form->get('userName')) ?>
<?php echo $this->formLabel($form->get('fullName')) ?> <?php echo $this->formInput($form->get('fullName')) ?>
Use only to change your password, otherwise leave them empty
<?php echo $this->formLabel($form->get('password')) ?> <?php echo $this->formInput($form->get('password')) ?>
<?php echo $this->formLabel($form->get('password_verify')) ?> <?php echo $this->formInput($form->get('password_verify')) ?>
<?php echo $this->formLabel($form->get('active')) ?> <?php echo $this->formSelect($form->get('active')) ?>
<?php echo $this->formSubmit($form->get('submit')) ?>
<?php echo $this->form()->closeTag() ?>