Using usual forms request form for fields #784

Merged
merged 1 commit into from Jun 4, 2015

Projects

None yet

3 participants

@jaisonerick
Contributor

Added a new config option called sendFieldsAsFormValue that when it's true, it changes how the form data is send. The config option was created to ensure backward compatibility.

This change helps whoever uses PHP as a Backend, since it's the way PHP understand form fields.

When there's a field that is an array or object, it changes the key, appending each property of the object wrapped in brackets and each key of the array, also wrapped in brackets and making this key equal to the corresponding value at the object/array.

This is a proposal, it's not finished or fully tested.

Examples: Array

fields: {
  tags: [ 'tagA', 'tagB', 'tagC' ]
}

The request would be (I'm using query string syntax, but its the
key/value form of multipart/form-data):

tags[]=tagA&tags[]=tagB&tags[]=tagC

Examples: Object

The same works for objects:

fields: {
  user: {
    name: 'John Doe'
    email: 'john@doe.org'
  }
}

The request would be (also like query string):

user[name]=John Doe&user[email]=john@doe.org

Examples: Objects inside Array

If the request is an array of multiple objects:

``` javascript`
fields: {
users: [
{ email: 'user1@example.org' },
{ email: 'user2@example.org' }
]
}


The request:

users[0][email]=user1@example.org&users[1][email]=user2@example.org

@tsanko
tsanko commented Jun 2, 2015

+1

@danialfarid
Owner

Would tags[0]=tagA&tags[1]=tagB&tags[2]=tagC work for

fields: {
  tags: [ 'tagA', 'tagB', 'tagC' ]
}

or does it need to be like
tags[]=tagA&tags[]=tagB&tags[]=tagC
?

@jaisonerick
Contributor

I wrote it wrong. It's the way you said tags[0]=tagA&tags[1]=tagB&tags[2]=tagC.

Usually, when you send an array to PHP, you send as tags[], but if any of the values is another object, you must use the index number as key. Because of that, to keep it simple, the pattern above is always used (using the index number as key).

@danialfarid
Owner

@jaisonerick Could you please update the wiki sample here: https://github.com/danialfarid/ng-file-upload/wiki/PHP-Example or create a new one with the sample of fields being used and retrieved on the server? #707

@danialfarid danialfarid merged commit 30216cb into danialfarid:master Jun 4, 2015
@danialfarid danialfarid pushed a commit that referenced this pull request Jun 4, 2015
Danial Farid Fixed #783 #764 #777 #766 #763 #761 #721 #687 #650 #710 #784 #768 #789 39fa784
@jaisonerick
Contributor

@danialfarid Wiki done. Thanks for the merge.

@danialfarid
Owner

From version 5.0.0 you can set sendFieldsAs: "form" to make this work,

@danialfarid
Owner

Thanks for the PR and updating the wiki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment