Re: Changing JRequest calls

431 views
Skip to first unread message

Dmitry Rekun

unread,
Mar 20, 2013, 9:18:31 AM3/20/13
to joomla-de...@googlegroups.com
Hi!

global $app - will not work.

Use $app = JFactory::getApplication();

Dmitry

On Wednesday, March 20, 2013 9:49:49 AM UTC+2, Otto Hoppe wrote:
Hi All

As you will all know the JRequest is decrepated in Joomla 2.5. So I am changing lines like

JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
$requestData = JRequest::getVar('jform', array(), 'post', 'array');

into

global $app;

JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
$requestData = $app->input->post->get('jform', array(), 'array');


But what is the right one for 

JRequest::setVar('jform', $requestData, 'post');

?

Because

$app->input->post->set('jform', $requestData);

is not working.

I use this kind of code in the save function of the backend controller like

    public function save()
    {
        global $app;

        JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
        $requestData = $app->input->post->get('jform', array(), 'array');

        $date = date("Y-m-d H:i:s");
        $requestData['date_modify'] = $date;
        JRequest::setVar('jform', $requestData, 'post'); // this one needs to be changed
        parent::save();
    }

When the parent save is called the $data var doesnt have that date_modify changes.
In fact the parent save uses the JRequest::getVar.









Otto Hoppe

unread,
Mar 21, 2013, 2:39:01 AM3/21/13
to joomla-de...@googlegroups.com
Thanx for the reply

i changed the code to this

        $jInput = JFactory::getApplication();
        JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
        $requestData = $jInput->input->post->get('jform', array(), 'array');

        $date = date("Y-m-d H:i:s");
        $requestData['date_modified'] = $date;

        $jInput->input->post->set('jform', $requestData);

        parent::save();

In the parent:: save after the line
$data  = JRequest::getVar('jform', array(), 'post', 'array');

$data has still the old value for the date_modified.

Otto

Dmitry Rekun

unread,
Mar 21, 2013, 3:20:55 AM3/21/13
to joomla-de...@googlegroups.com
Can you please dump $requestData?

$requestData['date_modified'] = $date;
var_dump($requestData);

Just to make sure, that everything is ok in this array. If everything is ok then try to dump it after modification.


$jInput->input->post->set('jform', $requestData);
$requestData = $jInput->input->post->get('jform', array(), 'array');
var_dump($requestData);

Dmitry

Otto Hoppe

unread,
Mar 21, 2013, 4:43:48 AM3/21/13
to joomla-de...@googlegroups.com
in my controler save function after

$requestData = $jInput->input->post->get('jform', array(), 'array');

array
  'id' => string '4' (length=1)
  'company_id' => string '1' (length=1)
  'table_name' => string 'client' (length=6)
  'parent_id' => string '5' (length=1)
  'type' => string '1001' (length=4)
  'primary_address' => string '0' (length=1)
  'address' => string 'Gravendreef' (length=11)
  'address_nr' => string '89000111' (length=8)
  'zip_code' => string '8888HH' (length=6)
  'city' => string 'Den Haag' (length=8)
  'phone_number' => string '' (length=0)
  'fax_number' => string '' (length=0)
  'email' => string '' (length=0)
  'published' => string '1' (length=1)
  'date_modified' => string '2013-03-19 20:43:42' (length=19)

after setting some vars

        $date = date("Y-m-d H:i:s");
        $requestData['date_modified'] = $date;
        $requestData['parent_id'] = $app->getUserState('address-parent_id');
        $requestData['published'] = 1;
        $requestData['table_name'] = $app->getUserState('address-location');


array
  'id' => string '4' (length=1)
  'company_id' => string '1' (length=1)
  'table_name' => string 'client' (length=6)
  'parent_id' => string '5' (length=1)
  'type' => string '1001' (length=4)
  'primary_address' => string '0' (length=1)
  'address' => string 'Gravendreef' (length=11)
  'address_nr' => string '89000111' (length=8)
  'zip_code' => string '8888HH' (length=6)
  'city' => string 'Den Haag' (length=8)
  'phone_number' => string '' (length=0)
  'fax_number' => string '' (length=0)
  'email' => string '' (length=0)
  'published' => int 1
  'date_modified' => string '2013-03-21 08:37:35' (length=19)


        $jInput->input->post->set('jform', $requestData);
//        JRequest::setVar('jform', $requestData, 'post');
        $context = 'com_mentorship.edit.address';
        $values = (array) $app->setUserState($context.'.id', $id);

        parent::save();

now in the controllerform.php (the parent save function)

array
  'id' => string '4' (length=1)
  'company_id' => string '1' (length=1)
  'table_name' => string 'client' (length=6)
  'parent_id' => string '5' (length=1)
  'type' => string '1001' (length=4)
  'primary_address' => string '0' (length=1)
  'address' => string 'Gravendreef' (length=11)
  'address_nr' => string '89000111' (length=8)
  'zip_code' => string '8888HH' (length=6)
  'city' => string 'Den Haag' (length=8)
  'phone_number' => string '' (length=0)
  'fax_number' => string '' (length=0)
  'email' => string '' (length=0)
  'published' => string '1' (length=1)
  'date_modified' => string '2013-03-19 20:43:42' (length=19)

As you can see the date_modified has the old value.

I also tried 

$jInput->input->set('jform', $requestData);

But it looks to mee the input->set or input->post->set is not altering the $_POST var.
Do the parent::save function will receive the old values.

Al Warren

unread,
Mar 24, 2013, 2:52:26 PM3/24/13
to joomla-de...@googlegroups.com
$jInput->input->post-> uses a copy of $_POST data stored in it's data property.

JRequest::getVar('jform', array(), 'post', 'array'); retrieves data directly from $_POST

To make jform work with JRequest, you have to update it in $_POST
$_POST['jform'] = $requestData;


Otto Hoppe

unread,
Mar 27, 2013, 1:00:58 AM3/27/13
to joomla-de...@googlegroups.com
Hi Al,

thanks for the reply.

Yes, i know that i could modify the $_POST directly, i was more searching for the replacement of the SetVar function now JRequest is deprecated.
It looks logical for me that if JInput replaces JRequest the set function would be able to set the $_POST too, like in JRequest.

Maybe this will be a future function, so for the time being i will use the JRequest setVar.

When i look inside the JRequest setVar function i would have to 

case 'POST':
$_POST[$name] = $value;
$_REQUEST[$name] = $value;
break;

and

// Mark this variable as 'SET'
$GLOBALS['_JREQUEST'][$name]['SET.' . $hash] = true;
$GLOBALS['_JREQUEST'][$name]['SET.REQUEST'] = true;


to be sure it will be exactly the same.

Amy Stephen

unread,
Mar 27, 2013, 2:13:43 PM3/27/13
to joomla-de...@googlegroups.com
Never use the global variables directly.

Filter input.

Use JInput.

Nice resource => http://docs.joomla.org/Retrieving_request_data_using_JInput

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

r...@osdcs.com

unread,
Mar 27, 2013, 2:30:43 PM3/27/13
to joomla-de...@googlegroups.com
That would be nice if JInput worked properly to retreive html data.
I have to use JInput array then use the first element of the array to get the info that is submitted from the view inside the model.
Logged in as SU with no Filtering. doesn't seem to work. it strips all html.
had to change back to JRequest instead of that dirty JInput workaround.
And as from what I've seen in alot of other 3PD extensions, they do the same with JRequest.
If a working example could be provided, that uses the Joomla filters too, it would be much appreciated.
 
Rob


From: joomla-de...@googlegroups.com [mailto:joomla-de...@googlegroups.com] On Behalf Of Amy Stephen
Sent: Thursday, March 28, 2013 1:14 AM
To: joomla-de...@googlegroups.com
Subject: Re: [jgen] Re: Changing JRequest calls

Otto Hoppe

unread,
Mar 27, 2013, 3:42:01 PM3/27/13
to joomla-de...@googlegroups.com
Hi Amy

Thanx for the reply

i know, 'never use the globals directly'. The article about JInput i know, that set command will not alter the $_POST vars like JRequest::setVar does.
And that is exactly my problem. As far i can see there is no function in JInput to set the $_POST vars (or other globals).

So i think i have 2 options, use the deprecated JRequest::setVar until JInput does have a set function to rewrite the $_POST or write $_POST directly myself.

Amy Stephen

unread,
Mar 27, 2013, 3:54:35 PM3/27/13
to joomla-de...@googlegroups.com
On Wed, Mar 27, 2013 at 1:30 PM, <r...@osdcs.com> wrote:
That would be nice if JInput worked properly to retreive html data.

Does this work?
$jinput = JFactory::getApplication()->input;
$return_value = $jinput->get('html_field', null, 'SAFE_HTML');
There are many filter examples on that link I shared -
http://docs.joomla.org/Retrieving_request_data_using_JInput 
 

Amy Stephen

unread,
Mar 27, 2013, 3:58:21 PM3/27/13
to joomla-de...@googlegroups.com
Otto -

Do you mind backing up and giving me a little background on what you are trying to do?

IMO, JInput is much safer than JRequest for those reasons. An extension should not (and I repeat, SHOULD NOT) update global variables.

But, I'm sure you have a reason to override Apache -- and I, for one, am very interested in hearing it!

What are you trying to do, my friend?

Amy Stephen

unread,
Mar 27, 2013, 3:59:44 PM3/27/13
to joomla-de...@googlegroups.com
On Wed, Mar 27, 2013 at 2:42 PM, Otto Hoppe <megaz...@gmail.com> wrote:
So i think i have 2 options, use the deprecated JRequest::setVar until JInput does have a set function to rewrite the $_POST or write $_POST directly myself.

To add: I don't see this option happening any time soon.

Bakual

unread,
Mar 27, 2013, 4:04:21 PM3/27/13
to joomla-de...@googlegroups.com
I don't think it will happen that JInput will write back to the $_POST variable. The idea is that it the global is read once and then JInput maintains its own set of variables.
However your initial problem can (and should) be solved in a different way anyway. Look at what com_weblinks does with the modified field as an example. You have to look at the table in /administrator/components/com_weblinks/tables/weblink.php. Search for the "store" function and you see that it updates some fields there during saving.
I think that is what you're trying to do?

Donald Gilbert

unread,
Mar 27, 2013, 5:10:51 PM3/27/13
to joomla-de...@googlegroups.com
We've modified the Input package in the new Joomla Framework and removed the ability for it to change super globals. Previously, it was using a reference to the $_REQUEST super global which would modify it when updating JInput, but that was just as a proxy to keep compatibility with JRequest. From a architectural standpoint, your application shouldn't be changing these, as your app should ever be directly accessing them. If every app / plugin / component always used JInput, then it would be moot.


Otto Hoppe

unread,
Mar 27, 2013, 5:37:49 PM3/27/13
to joomla-de...@googlegroups.com
What i try to do, it the following

in a controller in the backend i have a save function. By example this one is called in the Toolbar like 

<a class="toolbar" onclick="Joomla.submitbutton('foo.save')" href="#">

the conroller foo has the function save with code like this:

    public function save()
    {
        global $app;

        JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
        $requestData = $app->input->post->get('jform', array(), 'array');

        $date = date("Y-m-d H:i:s");
        $requestData['date_modify'] = $date;        <== this is a example to overwrite some fields taken from the jform
        JRequest::setVar('jform', $requestData, 'post');
        parent::save();
    }

The $requestData is written back to the post, because the parent::save function (joomla/application/component/controllerform.php) picks up the the $data from the JRequest::setVar too
public function save($key = null, $urlVar = null)
{
// Check for request forgeries.
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

// Initialise variables.
$app   = JFactory::getApplication();
$lang  = JFactory::getLanguage();
$model = $this->getModel();
$table = $model->getTable();
$data  = JRequest::getVar('jform', array(), 'post', 'array');     <== this data pickup i talk about
$checkin = property_exists($table, 'checked_out');
$context = "$this->option.edit.$this->context";
$task = $this->getTask();

So i thought, i to make the parent::save work, i will need to write back the $requestData to to post with JRequest::setVar.

And yes, i want to use the JInput and not $_POST.

@Bakual

i see that store function in tables/weblink.php 
And i even like this way more. Because i like this more. In fact all data has been set and i only want to set/change some data fields.
In this way i can drop the save functions in the controller and use the store function in the table.

I havent tested it yet, but i looks very clear and logical to me.

Donald Gilbert

unread,
Mar 27, 2013, 5:39:45 PM3/27/13
to joomla-de...@googlegroups.com
I agree with what you're saying. If you're developing for 2.5, then you need to use the workaround that you're specifying. (I've used it myself)


Otto Hoppe

unread,
Mar 27, 2013, 5:52:26 PM3/27/13
to joomla-de...@googlegroups.com
So after all this is what i did:

i deleted the save function from the controller

in the table added this

    public function store($updateNulls = false)
    {
        $date = date("Y-m-d H:i:s");
        $this->date_modify = $date;

        // Attempt to store the user data.
        return parent::store($updateNulls);
    }

and the update is done.
Im not sure if i can remove all the save functions i use (some are much more complex) but it is for me a good way to start removing the JRequest calls i use in controllers etc.

Thanks!

Amy Stephen

unread,
Mar 27, 2013, 6:15:05 PM3/27/13
to joomla-de...@googlegroups.com
Way to go, Otto. Thanks for sharing this. If you find you have a few minutes at some point, might be good to add this info to that wiki page for others.

Dmitry Rekun

unread,
Mar 28, 2013, 3:48:54 AM3/28/13
to joomla-de...@googlegroups.com
But back to initial question - why this is not working?

$input = JFactory::getApplication()->input;
$input->post->set('myVar', $myVar);

Then in another class:
$input = JFactory::getApplication()->input;
$myVar = $input->post->get('myVar');

That is because these will be two different POSTs? Correct?

And should we use this instead (not altering post):
$input->set('myVar', $myVar);
$input->get('myVar', $myVar);

Dmitry
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.

Rob Joyce

unread,
Mar 28, 2013, 4:00:48 AM3/28/13
to joomla-de...@googlegroups.com
Sorry no.
in the editor I enter for testing if it will strip.
<object>sddsfd </object>
 
in the model I use
$jinput = JFactory::getApplication()->input;
$return_value = $jinput->get('question_wording', null, 'SAFE_HTML');
die(print_r($return_value,1));
It outputs only sddfd
The tags have been stripped.
I can see in the Input class file filter there is a HTML option, but it doesn't return html either, it strips the html too.

Dmitry Rekun

unread,
Mar 28, 2013, 4:14:33 AM3/28/13
to joomla-de...@googlegroups.com
In JFrom this would be filter="safehtml", try SAFEHTML.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.

To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.

To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.

To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.

Amy Stephen

unread,
Mar 28, 2013, 5:11:11 AM3/28/13
to joomla-de...@googlegroups.com
Dmitry (and Otto) -

Let me see if I am understanding.

Are you basically saying you were modifying the POST (JRequest::setVar) to basically add the data that needs to be there during the save method -- that is not on the form?


In Otto's case, he needs a date_modify field updated and he used to do so using JRequest::setVar which added a date_modify parameter to the collection in order to ensure the pair would "be there" for the save method?
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Dmitry Rekun

unread,
Mar 28, 2013, 5:19:48 AM3/28/13
to joomla-de...@googlegroups.com
Amy, you are understanding this correctly. The goal is to add the data to collection (array in Otto's case), than put this collection back to input (post) and then retrieve it form there.

Dmitry
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsubscribe@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.

piotr_cz

unread,
Mar 28, 2013, 5:56:46 AM3/28/13
to Joomla! General Development

I reckon the problem is that there's no RAW option for JFilterInput-
>clean

BTW: I wonder how Editor's html code is being retrieved?


This was workaround I used once:

// Initialise variables.
$jinput = JFactory::getApplication()->input;

// Workaround stupid Joomla filtering (no XML or RAW option)
$commandline = $jinput->post->get('commandline', null, 'ARRAY');

// old way
// $commandline = JRequest::getVar('commandline', null, 'post',
'HTML', JREQUEST_ALLOWHTML);


On Mar 27, 7:30 pm, <r...@osdcs.com> wrote:
> That would be nice if JInput worked properly to retreive html data.
> I have to use JInput array then use the first element of the array to get
> the info that is submitted from the view inside the model.
> Logged in as SU with no Filtering. doesn't seem to work. it strips all html.
> had to change back to JRequest instead of that dirty JInput workaround.
> And as from what I've seen in alot of other 3PD extensions, they do the same
> with JRequest.
> If a working example could be provided, that uses the Joomla filters too, it
> would be much appreciated.
>
> Rob
>
>   _____
>
> From: joomla-de...@googlegroups.com
> [mailto:joomla-de...@googlegroups.com] On Behalf Of Amy Stephen
> Sent: Thursday, March 28, 2013 1:14 AM
> To: joomla-de...@googlegroups.com
> Subject: Re: [jgen] Re: Changing JRequest calls
>
> Never use the global variables directly.
>
> Filter input.
>
> Use JInput.
>
> Nice resource =>http://docs.joomla.org/Retrieving_request_data_using_JInput
>
> <mailto:joomla-dev-general%2Bunsu...@googlegroups.com> .
> To post to this group, send an email to joomla-de...@googlegroups.com.
> Visit this group athttp://groups.google.com/group/joomla-dev-general?hl=en-GB.
> For more options, visithttps://groups.google.com/groups/opt_out.

Amy Stephen

unread,
Mar 28, 2013, 5:57:47 AM3/28/13
to joomla-de...@googlegroups.com
If using JForm, you could define the field in your XML and give it a type of hidden. Then, it will be in the collection and can be updated.

Would that do it?

It's sort of dawning on me, is the reason for even using JInput directly is to avoid using JForm?

Because it should be handling your HTML filtering @rgjoyce.

And JForm should be making this column available in the form input, Otto, provided it's defined in the table xml.

Dmitry - does that make sense?

To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Dmitry Rekun

unread,
Mar 28, 2013, 6:09:23 AM3/28/13
to joomla-de...@googlegroups.com
Amy,

I personally do use JForm :)

But in this particular Otto's case I want to understand the principle of setting and getting values from Input. What is the correct way? Am I understand correctly, that we can not alter $input->post/get directly and should use $input->set/get to set and retrieve values?

Dmitry

Amy Stephen

unread,
Mar 28, 2013, 7:10:11 AM3/28/13
to joomla-de...@googlegroups.com
Let's see what Don has to say, but, off the cuff, one of the main changes between JRequest and JInput is that JInput is not a static class.

You can add a field:

$input = new JInput();
$input->set('foo', 'bar');


Provided you use it in scope of that instance.

echo $input->get('foo');

But if you instantiate a new instance, your new field will not be there -- you'll get a whole, clean, new instance:

$input2 = new JInput();
echo $input2->get('foo'); //returns nothing

I would say you need to bear in mind the scope of the class instance as you use it. If necessary, pass the instance into the classes which need it as a dependency.

Otherwise, you could add a field to the form itself, thus creating an entry in the POST collection.

Or, do as Otto did and push the logic as close to the database call, as possible (thus eliminating the need to pass on the instance.)

But, I don't work with it much these days and Don does. I could be entirely wrong.

Don?


Better documentation:
https://github.com/joomla/joomla-framework-input


Andrew Eddie

unread,
Mar 28, 2013, 8:32:28 AM3/28/13
to joomla-de...@googlegroups.com
On Thursday, 28 March 2013 17:48:54 UTC+10, Dmitry Rekun wrote:
But back to initial question - why this is not working?

$input = JFactory::getApplication()->input;
$input->post->set('myVar', $myVar);

Then in another class:
$input = JFactory::getApplication()->input;
$myVar = $input->post->get('myVar');

I'm thinking that should be working. You are accessing the same input variable by reference.
 
And should we use this instead (not altering post):
$input->set('myVar', $myVar);
$input->get('myVar', $myVar);

I've actually been trying to wrap my head around that question for the last few days. There's this strange "double layering" in Input and I'm not sure of why it's been designed that way. It would be much easier to just have Input be independent of the request method or, at least, work that all out at construction time and just allow us to use $input->get all the time (though, I think it makes sense to treat cookies and files differently). Thoughts?

Regards,
Andrew Eddie

Tuan Pham Ngoc

unread,
Mar 28, 2013, 8:33:39 AM3/28/13
to joomla-de...@googlegroups.com
Hi

I think I have answers for the questions in this forum thread :

1. For Otto's problem : I think we can use the solution below :

$input = JFactory::getApplication()->input;
$date = date("Y-m-d H:i:s");
$requestData['date_modified'] = $date;
$input->post->set('jform', $requestData);

Then in the parent::save method, instead of using :

$data  = JRequest::getVar('jform', array(), 'post', 'array');

You can call :

$input = JFactory::getApplication()->input;
$data = $input->post->get('jform');

The idea is input is a public property of the global  application object, so if you modify this input from parent class, you can always access to it from child class.

2. For getting HTML input using JInput class, we cannot use $input from JFactory::getApplication()->input ; We need to create new object from JInput. Something like that:

$filter = JFilterInput::getInstance(null, null, 1, 1);
$input = new JInput(null, array('filter' => $filter));
echo $input->post->get('description', '', 'HTML');

3. For the problem which rgjoyce mentioned, we will need to use a different code for filterinput object because object is in blacklist tags. Something like this should work :

$filter = JFilterInput::getInstance(null, null, 1, 1, 0);
$input = new JInput(null, array('filter' => $filter));
echo $input->post->get('question_wording', '', 'HTML');

Otto Hoppe

unread,
Mar 28, 2013, 8:36:46 AM3/28/13
to joomla-de...@googlegroups.com
Hi Amy,

hopefully not a double post.

I alway use the JForm, i love the way it works with the XML file.

About when to change things that comes back from the form:

- Sometimes i put hidden fields on the form which i want to initialize or just change
- Sometimes i dont punt fields on the form but want to modify them
and of course there are more situations i want to do that.

Because i use the parent::save function i have to deal with the $_POST (that parent save function will get the data with the JRequest get).

About the JInput / JFrom and html.

I didnt investigate it very good but i think this will happen:

If the filter is not set in the xml html will be stripped, which is good.
When the filter is set in the xml and i want to get that html from the input the JInput get function will strip it.
Why?

because that get function is

public function get($name, $default = null, $filter = 'cmd')
{
if (isset($this->data[$name]))
{
return $this->filter->clean($this->data[$name], $filter);
}

return $default;
}

So the clean will always been executed.

Besides it look a double clean (first the JForm, then the JInput) in this case (please correct me if i am wrong), it is nog possible to receive the html from the form.

I think that the clean function might need a RAW type.
And of course its to the developer who is dealing with that html to secure it well,.





On Wednesday, 20 March 2013 08:49:49 UTC+1, Otto Hoppe wrote:
Hi All

As you will all know the JRequest is decrepated in Joomla 2.5. So I am changing lines like

JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
$requestData = JRequest::getVar('jform', array(), 'post', 'array');

into

global $app;

JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
$requestData = $app->input->post->get('jform', array(), 'array');


But what is the right one for 

JRequest::setVar('jform', $requestData, 'post');

?

Because

$app->input->post->set('jform', $requestData);

is not working.

I use this kind of code in the save function of the backend controller like

    public function save()
    {
        global $app;

        JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
        $requestData = $app->input->post->get('jform', array(), 'array');

        $date = date("Y-m-d H:i:s");
        $requestData['date_modify'] = $date;
        JRequest::setVar('jform', $requestData, 'post'); // this one needs to be changed
        parent::save();
    }

Amy Stephen

unread,
Mar 28, 2013, 9:23:09 AM3/28/13
to joomla-de...@googlegroups.com
On Thu, Mar 28, 2013 at 7:32 AM, Andrew Eddie <mamb...@gmail.com> wrote:
On Thursday, 28 March 2013 17:48:54 UTC+10, Dmitry Rekun wrote:
But back to initial question - why this is not working?

$input = JFactory::getApplication()->input;
$input->post->set('myVar', $myVar);

Then in another class:
$input = JFactory::getApplication()->input;
$myVar = $input->post->get('myVar');

I'm thinking that should be working. You are accessing the same input variable by reference.

Thanks Andrew - yes, of course you are right, the Application class has input as a property and it is a static.
 
 
And should we use this instead (not altering post):
$input->set('myVar', $myVar);
$input->get('myVar', $myVar);

I've actually been trying to wrap my head around that question for the last few days. There's this strange "double layering" in Input and I'm not sure of why it's been designed that way. It would be much easier to just have Input be independent of the request method or, at least, work that all out at construction time and just allow us to use $input->get all the time (though, I think it makes sense to treat cookies and files differently). Thoughts?

Regards,
Andrew Eddie

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Amy Stephen

unread,
Mar 28, 2013, 9:28:13 AM3/28/13
to joomla-de...@googlegroups.com
Going to step away from this thread, you have Andrew's attention and Don said he was hoping to provide more feedback at lunch. Those are your experts.

Good luck!

r...@osdcs.com

unread,
Mar 28, 2013, 9:38:22 AM3/28/13
to joomla-de...@googlegroups.com
Thank you.
I confirm that this works:
$filter = JFilterInput::getInstance(null, null, 1, 1, 0);
$input = new JInput(null, array('filter' => $filter));
echo $input->post->get('question_wording', '', 'HTML');
 
Afraid though, there should be a cleaner way to do this I feel.


From: joomla-de...@googlegroups.com [mailto:joomla-de...@googlegroups.com] On Behalf Of Tuan Pham Ngoc
Sent: Thursday, March 28, 2013 7:34 PM

To: joomla-de...@googlegroups.com
Subject: Re: [jgen] Re: Changing JRequest calls

Dmitry Rekun

unread,
Mar 28, 2013, 9:51:25 AM3/28/13
to joomla-de...@googlegroups.com
On Thursday, March 28, 2013 2:32:28 PM UTC+2, Andrew Eddie wrote:
On Thursday, 28 March 2013 17:48:54 UTC+10, Dmitry Rekun wrote:
But back to initial question - why this is not working?

$input = JFactory::getApplication()->input;
$input->post->set('myVar', $myVar);

Then in another class:
$input = JFactory::getApplication()->input;
$myVar = $input->post->get('myVar');

I'm thinking that should be working. You are accessing the same input variable by reference.

I also think it should. I thought Otto tried it and it did not work for him. But now I see that Otto tried to get post using JRequest, not JInput. Sry, it is my fault, I missed that.
I like the idea to unify (cookies and files do not count), but wouldn't it be to much complicated for JInput? Current approach not too bad at all, just some misunderstanding, but it happens not too often, I think.

Dmitry

Tuan Pham Ngoc

unread,
Mar 28, 2013, 9:57:36 AM3/28/13
to joomla-de...@googlegroups.com
Oh

Just looked at https://github.com/joomla/joomla-platform/blob/staging/libraries/joomla/filter/input.php, it has a RAW filter option, nut sure when it was added to JFilterInput but it is not available in the current CMS (both Joomla 2.5 and Joomla 3.0)

If the JFIilterInput from platform is mergered into CMS, we will have a much simpler and correct way :

$input = JFactory::getApplication()->input;

echo $input->post->get('question_wording', null, 'RAW');

To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.

Bakual

unread,
Mar 28, 2013, 11:09:07 AM3/28/13
to joomla-de...@googlegroups.com
According to the history it was merged 14 hours ago by Andrew from a PR which was open for 2 months.

Swapnil Shah

unread,
Mar 28, 2013, 11:28:53 AM3/28/13
to joomla-de...@googlegroups.com
Otto, 

Have you tried using prepareTable method? (I can't remember if this method is part of JModelAdmin or jtable.) I would guess JTable. There are also other methods that get called before $table->store is called. 

That way items like modified date / create date are automatically set for your record. Then you don't have to worry about modifying the jform array. 

Regards, 


Neil
Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Otto Hoppe

unread,
Mar 28, 2013, 4:08:44 PM3/28/13
to joomla-de...@googlegroups.com
Hi Neil,

i did some test about saving:

      In the controller,works good
      public function save()
      {
        global $app;

        JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
        $requestData = $app->input->post->get('jform', array(), 'array');

        $requestData['date_modified'] = date('Y-m-d H:i:s');
        $model = $this->getModel();
        $model->save($requestData);
     }
In the model, but works only if the field isnt used on the form
otherwise overwritten with the value of the form.
protected function prepareTable(&$table)
{
$table->date_modified = date('Y-m-d H:i:s');
}
In table works good for initializing, overwriting fields
public function store($updateNulls = false)
    {
        $this->date_modify = date("Y-m-d H:i:s");

        return parent::store($updateNulls);
    }

So to come back to my initial question:
I still can use the save function in the controller, replace the JRequest with the JInput but better dont call the parent::save function but use the model save function.
In that last case, there is no need to rewrite the $_POST.

I am still wondering if that will be the right way to do it, probally i will need to do some more testing.

Otto
Reply all
Reply to author
Forward
0 new messages