Overwriting JControllerForm save() Method to Trim POST Data Has No Effect

心已入冬 提交于 2019-12-24 06:07:09

问题


I have a component that has a controller called MyproductControllerGeneralsetting which extends JControllerForm. Inside MyproductControllerGeneralsetting I am overwriting the save method from the parent class in order to modify $_POST data and then the overwriting method calls the parent class' save method to do the actual saving.

Here's the overwritten method in MyproductControllerGeneralsetting:

/**
 * We overwrite the saved form data and trim them to avoid spaces
 */
public function save($key = null, $urlVar = null){
    if($_POST['jform']){
        foreach($_POST['jform'] as $key=>&$value){
            $value = trim($value);
        }
    }

    // Finally, save the processed form data (calls JControllerForm-save())
    parent::save('id', $urlVar);
}

The thing is that even though I've trimmed each POST data field in this overwriting method, if I have some values submitted such as 'value ' (note the space in the end), they are not trimmed.

I've checked the save method of the JControllerForm class and it seems to be getting the data from POST here:

$data  = $this->input->post->get('jform', array(), 'array');

Maybe that's the reason? Is this getting cached data or something?


回答1:


Instead of trying to get the values from $_POST directly, try getting and setting the data in the same way the parent class does - using an internal pointer to a (shared) instance of the JInput class.

Here's a modified, working, overwritten save method:

/**
 * We overwrite the saved form data and trim them to avoid spaces
 */
public function save($key = null, $urlVar = null){
    if($_POST['jform']){

        // Get the original POST data
        $original = JRequest::getVar('jform', array(), 'post', 'array');

        // Trim each of the fields
        foreach($original as $key=>$value){
            $original[$key] = trim($value);
        }

        // Save it back to the $_POST global variable
        JRequest::setVar('jform', $postData, 'post');
    }

    // Finally, save the processed form data
    return parent::save('id', $urlVar);
}



回答2:


The controller is the wrong place for such things anyway, or is there a specific reason you want to do it in the controller? Better look at the prepareTable function in the model. There you already have the table object with the properties to save and can sanitise them prior to saving.

Additional Info: If you extend JControllerForm, you can specify

/**
 * @since   1.6
 */
protected $view_item = 'item';

/**
 * @since   1.6
 */
protected $view_list = 'items';

By default, the $view_item will equal to the context. The $view_list tries to guess a pluralized version of the $view_item. Usually by adding an s to the end.



来源:https://stackoverflow.com/questions/16280955/overwriting-jcontrollerform-save-method-to-trim-post-data-has-no-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!