2

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?

Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118

2 Answers2

4

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);
}
Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118
  • I think either `$postData` should be `$original`, or `$original[$key]` should be `$postData[$key]` – jost21 Sep 30 '18 at 11:16
4

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.

Bakual
  • 2,731
  • 1
  • 13
  • 16
  • Okay, but I am also doing redirection based on if the POST data is valid or not. I am guessing redirecting from the model is not a good MVC practice? – Dzhuneyt May 02 '13 at 10:22
  • I think if the form fails to save, the controller will redirect back to the form with an error message by default. That's why you can specify an item and a list view in the controller. – Bakual May 02 '13 at 10:42
  • "That's why you can specify an item and a list view in the controller." Can you ellaborate on this part? – Dzhuneyt May 02 '13 at 10:48
  • Added some info to the answer – Bakual May 02 '13 at 11:02
  • This caught me out as well, but instead of automatically pluralising with an s, it was guessing my top level view to be "awardsentries" instead of "awardsentrys". Which is technically correct, but no where is this mentioned in class naming convention anywhere I could find. Took a lot of guesswork to bring me here. – Martyn Shutt Apr 25 '15 at 13:25