6

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.

  • can you please explain us details? – Keyul Shah Dec 30 '13 at 15:00
  • Magento shows a small disc in the product tabs, so there is some magento build in function, but I have no idea how it works. – Fabian Blechschmidt Aug 18 '14 at 15:57
11

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);       
      }
   }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.