문제

While editing the elements in the custom admin form, how can I check whether the form has been edited to the last saved data of the form? Is there any way I can check this using any inbuilt Magento functions or should I write the validation for every field in the form for changes? I have multiple tabs in my form for further notice.

Thanks in advance.

도움이 되었습니까?

해결책

When you have a model which is an type of Mage_Core_Model_Abstract, then you can easily get the previous data (original data) on save using public function getOrigData($key=null) method. But there are two other methods that might be helpful in your case.

There is a method to know if the values has changed or not public function hasDataChanges(). So using this method you can check if your data has changed globally (meaning if there is any change this flag will be set) and if it is changed then loop through all the key using another method public function dataHasChangedFor($field).

Add these logic on protected function _beforeSave().

Here's how you code might look like.

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 ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top