在编辑自定义管理员表格中的元素时,如何检查表格是否已编辑为表单的最后保存数据?有什么办法可以使用任何内置的Magento函数检查此内容,还是应该为更改中的每个字段编写验证?我的表格中有多个标签,以便进一步注意。

提前致谢。

有帮助吗?

解决方案

当您有一种模型时 Mage_Core_Model_Abstract, ,然后您可以轻松地获取先前的数据(原始数据),然后使用 public function getOrigData($key=null) 方法。但是,还有另外两种方法可能对您的情况有所帮助。

有一种方法可以知道值是否已更改 public function hasDataChanges(). 。因此,使用此方法可以检查您的数据是否已在全球上更改(这意味着如果设置了任何更改),如果将其更改为更改,则使用其他方法循环遍历所有密钥 public function dataHasChangedFor($field).

将这些逻辑添加 protected function _beforeSave().

这是您的代码外观。

protected function _beforeSave() {
   parent::_beforeSave();
   if ($this->hasDataChanges()) {
      foreach ($this->_data as $key => $value) {
          if (!$this->dataHasChangedFor($key)) continue;

          // Now you know what field value has changed and call your own function to handle the changed parameter, or check conditions. 
          // This could be a switch/case handler that will call other function just to keep this function clean.
          $this->_dataChangeHandler($key, $value);       
      }
   }
}
许可以下: CC-BY-SA归因
scroll top