php - Zend Framework2 form error messages -
here code below, create login form
class loginform extends form { public function __construct($name = null) { parent::__construct('login'); $this->setattribute('method', 'post'); $this->add(array('name'=>'uname','attributes'=>array('type'=>'text',), 'options'=>array('label'=>'username'), )); $this->add(array('name'=>'pword','attributes'=>array('type'=>'password',), 'options'=>array('label'=>'password'), )); $this->add(array('name'=>'submit','attribute'=>array('type'=>'submit', 'value' => 'go', 'class'=>'submit','id'=>'submitbutton',) )); } }
below code login page
$form = $this->form; $form->setattribute('action', $this->url('users', array('action' => 'login'))); $form->prepare(); echo $this->form()->opentag($form); echo $this->formrow($form->get('uname')); echo $this->formrow($form->get('pword')); echo $this->formelementerrors($form->get('pword')); echo $this->formsubmit($form->get('submit')); echo $this->form()->closetag();
form works fine no issues :
- it not taking value
submit
if see viewsource showsvalue=''
- i want display error message ever wish how achive that? tried
echo $this->formelementerrors($form->getmessages('uname'));
did not work suggestion or ideas fix this?
below controller code...
$form = new loginform(); $request = $this->getrequest(); if ($request->ispost()) { $post = $request->getpost(); if($post->get('uname')=='') { $umessage='please enter username'; return $this->redirect()->toroute('users',array('action'=>'login')); } $this->db =$this->getservicelocator()->get('db'); $authadapter = new authadapter($this->db); $authadapter->settablename('users') ->setidentitycolumn('uname') ->setcredentialcolumn('pword'); $authadapter->setidentity($post->get('uname')) ->setcredential(md5($post->get('pword'))); $authservice = new authenticationservice(); $authservice->setadapter($authadapter); $result = $authservice->authenticate(); if ($result->isvalid()) { return $this->redirect()->toroute('users'); } else { switch ($result->getcode()) { case result::failure_identity_not_found: echo 'user name not valid dude'; break; case result::failure_credential_invalid: echo 'password incorrect'; break; case result::success: echo 'login successfull'; break; } } } $this->layout('layout/index'); return array('form' => $form);
you syntax incorrect displaying individual error messages, try this:
change:
$this->formelementerrors($form->getmessages('uname')); // incorrect $this->formelementerrors($form->get('uname')); // correct
example:
<?php foreach($form->getelements() $element): ?> <?php if($element->getlabel()): ?> <?php echo $this->formlabel($element) ?> <?php endif ?> <?php echo $this->formelementerrors($element, array('class' => 'inline')); ?> <?php echo $this->formelement($element) ?> <?php endforeach ?>
notice passing in actual element, not via getmessages().
you have type on button definition (missing s
on end):
attribute => attributes
see below:
$this->add(array( 'name'=>'submit', 'attributes' => array( 'type'=>'submit', 'value' => 'go', 'class'=>'submit','id'=>'submitbutton', ) ));
i assuming have setup input filters too?
$inputfilter->add($factory->createinput(array( 'name' => 'username', 'required' => true, 'filters' => array( array('name' => 'striptags'), array('name' => 'stringtrim'), ), 'validators' => array( array( 'name' => 'stringlength', 'options' => array( 'encoding' => 'utf-8', 'min' => 1, 'max' => 100, ), ), ), )));
Comments
Post a Comment