Extend the datawriter _preSave function instead of the controller

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:
 
Thanks for the heads up, I'll make a note of this for any old and future addons.
 
Back
Top Bottom