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