dsa88886666 2014-02-25 11:58
浏览 21

SocialEngine / Zend中表单的问题

I have created a module in SocialEngine(*which is built on Zend framework v1.9) that contains an admin form with a few options.

The problem I have with it is that it seems to no get the values of the fields from database after I refresh the page and it shows me the default values.

It shows the correct values immediately after I save(*but I am not sure if the page is refreshed after saving), but not after I refresh.

controller /application/modules/Mymodule/controllers/AdminSomesettingsController.php :

class Mymodule_AdminSomesettingsController extends Core_Controller_Action_Admin
{
  public function indexAction()
  {
    $this->view->form = $form = new Mymodule_Form_Admin_Someform();

    $settings = Engine_Api::_()->getApi('settings', 'core');

    if(!$form->isValid($settings->mymodule))
    { return $form->populate($settings->mymodule); }

    if( !$this->getRequest()->isPost() ) { return; }
    if( !$form->isValid($this->getRequest()->getPost()) ) { return; }

    $db = Engine_Api::_()->getDbTable('settings','core')->getAdapter();
    $db->beginTransaction();
    try {
      $values = $form->getValues();
      $settings->mymodule = $values;

      $db->commit();
    } catch( Exception $e ) {
      $db->rollback();
      throw $e;
    }

    $form->saveValues();

    $form->addNotice('Your changes have been saved.');
  }
}

form /application/modules/Mymodule/Form/Admin/Someform.php :

class Mymodule_Form_Admin_Someform extends Engine_Form
{
  public function init()
  {
    $this
      ->setTitle('My Settings')
      ->setDescription('Settings');

    $this->addElement('Radio', 'some_setting', array(
      'label' => 'Some Setting',
      'description' => '',
      'multiOptions' => array(
        0 => 'Option One',
        1 => 'Option Two',
        2 => 'Option Three',
      ),
      'value' => 1,
      'escape' => false,
    ));

    // Add submit button
    $this->addElement('Button', 'submit', array(
      'label' => 'Save Changes',
      'type' => 'submit',
      'ignore' => true
    ));
  }
  public function saveValues()
  {
  }
}

I have checked with other plugins and it seems to me that $form->populate($settings->mymodule); repopulates the form after refresh, but it does not work for me. Any idea how I could make it show the values from the database(*when these values exist) instead of the default values?

  • 写回答

2条回答 默认 最新

  • doucang2871 2014-03-03 03:41
    关注

    I myself am new to socialengine and zend.My understanding of socialengine says, make a function saveValues() inside ur form class, then call it from controller action as $form->saveValues(),passing parameter as needed.This is the convention that socialengine seems to follow, and inside the saveValues() of form class,u can save valus as needed.Ur form shud be populated only if validation fails

    (!$form->isValid($formData )) { return $form->populate($formData); }

    Instead of default adapter,U should try this-

    $db =Engine_Api::_()->getDbTable('settings','core')->getAdapter(),
    $db->beginTransaction();
    

    If u want to set the value of a particular field try - $form->populate(array('formField'=>'urValue')); in ur case maybe -

    $val=$settings->mymodule,
    $form->populate('formField'=>$val); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大