Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using usual forms request form for fields #784

Merged
merged 1 commit into from
Jun 4, 2015

Conversation

jaisonerick
Copy link
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
Copy link

tsanko commented Jun 2, 2015

+1

@danialfarid
Copy link
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
Copy link
Contributor Author

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
Copy link
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 added a commit that referenced this pull request Jun 4, 2015
Using usual forms request form for fields
@danialfarid danialfarid merged commit 30216cb into danialfarid:master Jun 4, 2015
danialfarid pushed a commit that referenced this pull request Jun 4, 2015
@jaisonerick
Copy link
Contributor Author

@danialfarid Wiki done. Thanks for the merge.

@danialfarid
Copy link
Owner

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

@danialfarid
Copy link
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants