public function editAction() { $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('admin/states', array('action' => 'add')); } $state = $this->getStateTable()->getState($id); $state->setAdapter($this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter')); $form = new StateForm($this->getServiceLocator()->get('Admin\\Model\\CountriesTable')); $form->bind($state); $form->get('submit')->setAttribute('value', 'Edit'); $request = $this->getRequest(); if ($request->isPost()) { // Adding already exist validation on runtime excluding the current record $state->getInputFilter()->get('state_code')->getValidatorChain()->attach(new \Zend\Validator\Db\NoRecordExists(array('table' => 'state', 'field' => 'state_code', 'adapter' => $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'), 'exclude' => array('field' => 'id', 'value' => $id)))); $state->getInputFilter()->get('state_name')->getValidatorChain()->attach(new \Zend\Validator\Db\NoRecordExists(array('table' => 'state', 'field' => 'state_name', 'adapter' => $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'), 'exclude' => array('field' => 'id', 'value' => $id)))); $form->setInputFilter($state->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $this->getStateTable()->saveState($form->getData()); $this->flashMessenger()->addSuccessMessage('State updated successfully..!!'); // Redirect to list of pages return $this->redirect()->toRoute('admin/states'); } else { $this->errors = $form->getMessages(); } } return array('id' => $id, 'form' => $form, 'errors' => $this->errors); }