Opened 9 years ago

Closed 9 years ago

#12437 closed defect (wontfix)

dojo.objectToQuery and arrays

Reported by: Richard Nespithal Owned by: anonymous
Priority: high Milestone: tbd
Component: General Version: 1.5
Keywords: Cc:
Blocked By: Blocking:

Description

There's a problem in dojo.objectToQuery - this will return a string with same variable names without an index:

Example:

var food = {}

food['vegetables'] = 'spinach';
food['fruits'] = new Array('banana','apple','orange');

console.log(dojo.objectToQuery(food));

The result:

vegetables=spinach&fruits=banana&fruits=apple&fruits=orange

Correct would be fruits[index] (for using these vars in PHP, for example):

vegetables=spinach&fruits[0]=banana&fruits[1]=apple&fruits[2]=orange

Change History (5)

comment:1 Changed 9 years ago by Eugene Lazutkin

I am not sure, if this is a defect, or even an enhancement --- I use the fact that several values are returned using the same name in my projects without any problems. It is easy to do in any language, including PHP. In any case I do not support claim that using 0-based indices with square brackets are "correct" vs. the current behavior.

It may make some sense to implement it as an additional module outside of the base, in DojoX perhaps to gauge if there is any interest in this functionality.

comment:2 Changed 9 years ago by bill

Yeah, this ticket seems like "invalid" to me too.

Note that regular form submission works according to the current behavior of dojo.objectToQuery(), what richardvfx calls incorrect.

I tried the simple HTML of

<form>
	<input name=name1 value=val1>
	<input name=name1 value=val2>
	<button type=submit>submit</button>
</form>

On submit it sends:

name1=val1&name1=val2

comment:3 Changed 9 years ago by Richard Nespithal

of course you'll get this string but please try to get the value of the first

name1 == in PHP.

$_GETname? and you'll get val2 instead of both vars. So how you'll get the first value?

comment:4 in reply to:  2 Changed 9 years ago by Eugene Lazutkin

Replying to bill:

Yeah, this ticket seems like "invalid" to me too.

Yes, like in your example duplicating names is the default behavior of all browsers. To be completely fair, I saw some people using the notation mentioned by Richard. In this case they formed names like that:

<form>
  <input name="name[0]" value=val1>
  <input name="name[1]" value=val2>
  <button type=submit>submit</button>
</form>

I am not sure how legal it is, but I saw it in the wild.

comment:5 in reply to:  3 Changed 9 years ago by Eugene Lazutkin

Resolution: wontfix
Status: newclosed

Replying to richardvfx:

$_GET["name"] and you'll get val2 instead of both vars. So how you'll get the first value?

By using PHP, of course. Example can be found here: http://www.php.net/manual/en/reserved.variables.get.php#92439 --- additionally it lists old PHP bug tickets related to this project and all closed without actions.

PHP manual explains various techniques to deal with complex input too: http://php.net/manual/en/language.variables.external.php

It looks like this modification of your example should work:

var food = {}

food['vegetables'] = 'spinach';
food['fruits[0]'] = 'banana';
food['fruits[1]'] = 'apple';
food['fruits[2]'] = 'orange';

console.log(dojo.objectToQuery(food));

Loks regular enough for me and can be easily automated (just like you suggested).

I am closing this ticket as "wontfix" --- catering to PHP (I admit it is a big market) we will break other languages, which don't have such bone-headed libraries to deal with a standard form encoding (see my answer to Bill). Additionally this is a breaking behavior, which will affect current users.

I would still consider a patch for a DojoX, which implements this functionality. Please open a new ticket for that, and don't forget to file CLA.

Note: See TracTickets for help on using tickets.