dongzhuanlei0768 2014-10-28 21:26
浏览 37
已采纳

为什么我的Zend表单没有更新为新值?

I can't figure out why my Zend form is not updating to new values. I have followed the Album example very closely but cannot figure out where I went wrong. Currently the Form fails the IsValid IF statement. If I place the save line above this check it seems to re enter the old information regardless of what new information is in the form box. Any thoughts?

EDIT ACTION BELOW:

     public function editLicenceAction()
     {
     $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter'); 
     $pro_id='22';

     $licence_id = (int) $this->params()->fromRoute('licence_id', 0);
     if (!$licence_id) {
         return $this->redirect()->toRoute('proLicence', array(
             'action' => 'index'
         ));
     }
     try {
         $proLicence = $this->getProLicenceTable()->getProLicence($licence_id);
     }
     catch (\Exception $ex) {
         return $this->redirect()->toRoute('proLicence', array(
             'action' => 'index'
         ));
     }
     $form  = new ProLicenceForm($dbAdapter);
     $form->bind($proLicence);
     $form->get('submit')->setAttribute('value', 'Edit');


     $request = $this->getRequest();
     if ($request->isPost()) {

         //$form->setInputFilter($proLicence->getInputFilter());  
         $form->setData($request->getPost()); 
         echo 'I got Request';

         if ($form->isValid()) {

             echo 'I got Save';
             $this->getProLicenceTable()->saveProLicence($proLicence);

             return $this->redirect()->toRoute('proLicence');
         }
     }

     return array(
         'licence_id' => $licence_id,
         'form' => $form,
     );
 } 

VIEW BELOW:

 $title = 'Edit Licence';
 $this->headTitle($title);
 ?>
 <h1><?php echo $this->escapeHtml($title); ?></h1>

 <?php
 $form = $this->form;
 $form->setAttribute('action', $this->url(
     'proLicence',
     array(
         'action' => 'editLicence',
         'licence_id' => $this->licence_id,
     )
 ));
 $form->prepare();

 echo $this->form()->openTag($form);
 echo $this->formHidden($form->get('licence_id'));
 echo $this->formRow($form->get('licence_name_id'));
 echo $this->formRow($form->get('licence_number'));
 echo $this->formSubmit($form->get('submit'));
 echo $this->form()->closeTag();

Pro Licence Table Class

<?php
 namespace Pro\Model\ProLicence;

 use Zend\Db\TableGateway\TableGateway;
 use Zend\Db\Sql\Sql;

 class ProLicenceTable
 {
     protected $tableGateway;

     public function __construct(TableGateway $tableGateway)
     {
         $this->tableGateway = $tableGateway;
     }

     public function fetchAll()
     {
         $resultSet = $this->tableGateway->select();
         return $resultSet;
     }

     public function getProLicence($licence_id)
     {
         $licence_id  = (int) $licence_id;
         $rowset = $this->tableGateway->select(array('licence_id' => $licence_id));
         $row = $rowset->current();
         if (!$row) {
             throw new \Exception("Could not find row $licence_id");
         }
         return $row;
     }

     public function saveProLicence(ProLicence $proLicence)
     {
         $data = array(
             'pro_id' =>$proLicence->pro_id,
             //'licence_id' => $proLicence->licence_id,
             'licence_name_id'  => $proLicence->licence_name_id,
             'licence_number' => $proLicence->licence_number,
             'licence_approved' => $proLicence->licence_approved,
         );

         $licence_id = (int) $proLicence->licence_id;
         if ($licence_id == 0) {
             $this->tableGateway->insert($data);
         } else {
             if ($this->getProLicence($licence_id)) {
                 $this->tableGateway->update($data, array('licence_id' => $licence_id));
             } else {
                 throw new \Exception('Licence id does not exist');
             }
         }
     }

     public function deleteProLicence($licence_id)
     {
         $this->tableGateway->delete(array('licence_id' => (int) $licence_id));
     }
 }

Pro Licence Class

 <?php
namespace Pro\Model\ProLicence;

 use Zend\InputFilter\InputFilter;
 use Zend\InputFilter\InputFilterAwareInterface;
 use Zend\InputFilter\InputFilterInterface;

 class ProLicence implements InputFilterAwareInterface
 {
     public $pro_id;
     public $licence_id;
     public $licence_name_id;
     public $licence_number;
     public $licence_approved;
     protected $inputFilter;

     public function exchangeArray($data)
     {
         $this->pro_id     = (!empty($data['pro_id'])) ? $data['pro_id'] : null;
         $this->licence_id     = (!empty($data['licence_id'])) ? $data['licence_id'] : null;
         $this->licence_name_id = (!empty($data['licence_name_id'])) ? $data['licence_name_id'] : null;
         $this->licence_number  = (!empty($data['licence_number'])) ? $data['licence_number'] : null;
         $this->licence_approved  = (!empty($data['licence_approved'])) ? $data['licence_approved'] : null;

     }

         // Add content to these methods:
     public function getArrayCopy()
     {
         return get_object_vars($this);

     }

     public function setInputFilter(InputFilterInterface $inputFilter)
     {
         throw new \Exception("Not used");
     }

     public function getInputFilter()
     {
         if (!$this->inputFilter) {
             $inputFilter = new InputFilter();

             $inputFilter->add(array(
                 'name'     => 'pro_id',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'Int'),
                 ),
             ));
             $inputFilter->add(array(
                 'name'     => 'licence_id',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'Int'),
                 ),
             ));
             $inputFilter->add(array(
                 'name'     => 'licence_name_id',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));
             $inputFilter->add(array(
                 'name'     => 'licence_number',
                 'required' => false,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));
             $inputFilter->add(array(
                 'name'     => 'licence_approved',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));

             $this->inputFilter = $inputFilter;
         }

         return $this->inputFilter;
     }

 }

FORM Class:

<?php
namespace Pro\Form;

use Zend\Form\Form;
use Zend\Db\Adapter\AdapterInterface;

 class ProLicenceForm extends Form
 {
     public function __construct(AdapterInterface $dbAdapter)
     {
         $this->adapter =$dbAdapter;
         // we want to ignore the name passed
         parent::__construct();

         $this->add(array(
             'name' => 'pro_id',
             'type' => 'Hidden',
         ));
         $this->add(array(
             'name' => 'licence_id',
             'type' => 'Hidden',
             'options' => array(
             ),
             'attributes' => array( 
                  //'required' => 'required'
                  )
         )); 
         $this->add(array(
             'name' => 'licence_approved',
             'type' => 'Hidden',
             'options' => array(
             ),
             'attributes' => array( 
                //  'required' => 'required'
                  )
         )); 
         $this->add(array(
             'name' => 'licence_name_id',
            'type' => 'Zend\Form\Element\Select',
             'options' => array(
                 'label' => 'Licence',
                 'value_options' => $this->getLicenceOptions(),
                 'empty_option' => '--- please choose ---'

             ),
             'attributes' => array( 
                 'placeholder' => 'Choose all that apply'             
                 )
         )); 
         $this->add(array(
             'name' => 'licence_number',
            'type' => 'Text',
             'options' => array(
                 'label' => 'Licence Number'
                 )

         )); 
         $this->add(array(
             'name' => 'submit',
             'type' => 'Submit',
             'options' => array(
             'label'=> 'Primary Button',
                 ),
             'attributes' => array(
                 'value' => 'Go',
                 'id' => 'submitbutton',
             ),
         ));

     }

      /*
      * SQL statements used to bring in optiosn
      */


     public function getLicenceOptions()
    {
        $dbAdapter = $this->adapter;
        $sql       = 'SELECT licence_name_id,licence_name  FROM licence_name_table ORDER BY licence_name';
        $statement = $dbAdapter->query($sql);
        $result    = $statement->execute();

        $selectData = array();

        foreach ($result as $res) {
            $selectData[$res['licence_name_id']] = $res['licence_name'];
        }
        return $selectData;
    }      

 }

Many Thanks, M

Update: The below works (copied from the album example). This is what I am using to trouble shoot the problem.

public function editAction()
 {
 $id = (int) $this->params()->fromRoute('id', 0);
 if (!$id) {
     return $this->redirect()->toRoute('album', array(
         'action' => 'add'
     ));
 }

 // Get the Album with the specified id.  An exception is thrown
 // if it cannot be found, in which case go to the index page.
 try {
     $album = $this->getAlbumTable()->getAlbum($id);
 }
 catch (\Exception $ex) {
     return $this->redirect()->toRoute('album', array(
         'action' => 'test'
     ));
 }

 $form  = new AlbumForm();
 $form->bind($album);
 $form->get('submit')->setAttribute('value', 'Edit');

 $request = $this->getRequest();
 if ($request->isPost()) {
     $form->setInputFilter($album->getInputFilter());
     $form->setData($request->getPost());

     if ($form->isValid()) {
         $this->getAlbumTable()->saveAlbum($album);
         Debug::dump($form);
         //var_dump($album);
         // Redirect to list of albums
         //return $this->redirect()->toRoute('album');
     }
 }

 return array(
     'id' => $id,
     'form' => $form,
 );

}

Update 2: comparing the debug it seems like the below portion is not updating on the licence example but is on the working album example:

["object":protected] => object(Pro\Model\ProLicence\ProLicence)#398 (6) {
    ["pro_id"] => string(2) "22"
    ["licence_id"] => string(1) "1"
    ["licence_name_id"] => string(1) "5"
    ["licence_number"] => string(9) "345345345"
    ["licence_approved"] => string(1) "T"
  • 写回答

2条回答 默认 最新

  • douliang2087 2014-10-31 22:04
    关注

    I forgot to add the hidden forms to my edit field. Once they were added I was able to edit no problem.

    echo $this->formHidden($form->get('membership_id'));
     echo $this->formHidden($form->get('pro_id'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配