1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Extend the datawriter _preSave function instead of the controller

Discussion in 'XenForo Development Discussions' started by cclaerhout, Feb 22, 2013.

  1. cclaerhout

    cclaerhout Well-Known Member

    I'm not sure it can help, but here's a quick message for coders who have directly extended the datawriter (ie: _preSave) instead of the controller.

    Don't do only this:
    PHP:
        protected function _preSave()
        {
            
    $_input = new XenForo_Input($_REQUEST);
            
    $myNewField $_input->filterSingle('my_new_field'XenForo_Input::UINT);
            
    $this->set('my_new_field'$myNewField);
          
            return 
    parent::_preSave();
        }
    Do this:
    PHP:
        protected function _preSave()
        {
            
    $_input = new XenForo_Input($_REQUEST);
            
    $myNewField $_input->filterSingle('my_new_field'XenForo_Input::UINT);
        
            if(
    $_input->inRequest('my_new_field'))
            {
                
    $this->set('my_new_field'$myNewField);
            }
          
            return 
    parent::_preSave();
        }
    It will avoid your extented field to save a blank value when the controller of this datawritter is called from another location (see here or there) :coffee:
     
  2. MOZ

    MOZ Well-Known Member

    Thanks for the heads up, I'll make a note of this for any old and future addons.
     

Share This Page