php - Validation for Add screen not working but it working in edit screen with same code -
i learning zend framework , have created add, update , delete functionality country name , continent name , working perfectly. have set validation by
$name->setrequired('true');
and
$continent->setrequired('true');
in form.php.
validation working in edit form return error 'an error occurred' , 'application error' in add form. below controller code:
for add:
/*add record database*/ public function addaction() { $form =new application_form_add(); $form->submit->setlabel('add country'); $this->view->form = $form; if($this->getrequest()->ispost()) { $formdata = $this->getrequest()->getpost(); if($form->isvalid($formdata)) { $file = new application_model_country(); $name = $form->getvalue('name'); $continent = $form->getvalue('continent'); $file->addcountry($name, $continent); $this->_helper->redirector('index'); } else { $this->populate($formdata); } } }
for edit:
/*edit record database*/ public function editaction() { $form = new application_form_edit(); $form->submit->setlabel('edit country'); $this->view->form = $form; if($this->getrequest()->ispost()) { $formdata = $this->getrequest()->getpost(); if($form->isvalid($formdata)) { $id = $form->getvalue('country_id'); $name = $form->getvalue('name'); $continent = $form->getvalue('continent'); $file = new application_model_country(); $file->updatecountry($id,$name,$continent); $this->_helper->redirector('index'); } else { $form->populate($formdata); } } else { $id = $this->getrequest()->getparam('country_id'); if($id >0) { $formdata = $this->getrequest()->getpost(); $file = new application_model_country(); $files = $file->fetchrow('country_id='.$id); $form->populate($files->toarray()); } } }
both code same, why validation not working in add form?
you need change following code in addaction
logic:
instead of:
$this->populate($formdata);
use
$form->populate($formdata);
the reason $this
means action object in context , have correctly used $form
object in editaction working properly, kind of silly typing mistake.
ps: should use proper case in method names ispost
, isvalidate
etc. otherwise may errors in linux environment.
Comments
Post a Comment