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

PHP Array format compatibility #221

Open
billbarsch opened this issue Mar 9, 2015 · 6 comments
Open

PHP Array format compatibility #221

billbarsch opened this issue Mar 9, 2015 · 6 comments

Comments

@billbarsch
Copy link

Hello,
When we submit default alpaca forms PHP receive data like that:

["client"]=> string(6) "asdlfk"
["data"]=> string(7) "as;dlfk"
["obs"]=> string(7) "sa;lfdk"
["items_0_quantidade"]=> string(0) ""
["items_0_produto"]=> string(0) ""
["items_0_descricao_descricao"]=> string(0) ""
["items_0_descricao_tamanhos_0_quantidade"]=> string(0) ""
["items_0_descricao_tamanhos_0_tamanho"]=> string(0) ""
["items_0_valorUnitario"]=> string(0) ""
["items_0_valorTotal"]=> string(0) ""

It's good but not so useful. Maybe we have to write a parser to read this like multidimentional php arrays.
But just "one line" change at this alpaca function:

       calculateName: function()
        {
            if (!this.name || (this.name && this.nameCalculated))
            {
                // has path?
                if (this.parent && this.parent.name && this.path)
                {
                    var lastSegment = this.path.substring(this.path.lastIndexOf('/') + 1);
                    if (lastSegment.indexOf("[") !== -1 && lastSegment.indexOf("]") !== -1)
                    {
                        lastSegment = lastSegment.substring(lastSegment.indexOf("[") + 1, lastSegment.indexOf("]"));
                    }
                    if (lastSegment)
                    {
                        this.name = this.parent.name + "_" + lastSegment;
                        this.nameCalculated = true;
                    }
                }
                else
                {
                    if (this.path)
                    {
                        this.name = this.path.replace(/\//g,"").replace(/\[/g,"_").replace(/\]/g,"");
                        this.nameCalculated = true;
                    }
                }
            }
        },

If we change this line
from:

                        this.name = this.parent.name + "_" + lastSegment;

to:

                        this.name = this.parent.name + "[" + lastSegment + "]";

The php receives data in a perfect multidimentional array, like that:

["cliente"]=> string(2) "fd"
  ["data"]=> string(3) "lkj"
  ["observacoes"]=> string(5) ",mn,m"
  ["itensPedido"]=> array(2) {
    [0]=>array(5) {
      ["quantidade"]=>  string(0) ""
      ["produto"]=> string(0) ""
      ["descricao"]=>  array(1) {
        ["descricao"]=>     string(0) ""
      }
      ["valorUnitario"]=>   string(0) ""
      ["valorTotal"]=>   string(0) ""
    }
    [1]=>array(5) {
      ["quantidade"]=>  string(0) ""
      ["produto"]=>   string(0) ""
      ["descricao"]=> array(2) {
        ["descricao"]=>   string(0) ""
        ["tamanhos"]=>   array(1) {
          [0]=> array(2) {
            ["quantidade"]=>  string(0) ""
            ["tamanho"]=>   string(0) ""
          }
        }
      }
      ["valorUnitario"]=> string(0) ""
      ["valorTotal"]=>  string(0) ""
    }
  }

With data like that, we can easily apply the php function: json_encode($_POST);
And the json result is:

"cliente": "cliente",
"data": "data",
"observacoes": "obs",
"itensPedido": [
    {
        "quantidade": "",
        "produto": "",
        "descricao": {
            "descricao": "",
            "tamanhos": [
                {
                    "quantidade": "",
                    "tamanho": ""
                },
                {
                    "quantidade": "",
                    "tamanho": ""
                }
            ]
        },
        "valorUnitario": "",
        "valorTotal": ""
    },
    {
        "quantidade": "",
        "produto": "",
        "descricao": {
            "descricao": ""
        },
        "valorUnitario": "",
        "valorTotal": ""
    }
]

Is really extremely useful, and completly compatible with the alpaca way to think
Just one line! And the result is wonderful and amazing! (for php community)

obs: I know we can just use the javascript functions getData and send the perfect json to php using ajax and after parser, have the same result. But I don't know how ajax is working about uploading files. I think now it's a limitation until the new javascript files API came by default on all major browsers. But what I really know is: in that format we can work posting everything directly over html: <form action="" method="post"... and... SUBMIT!

billbarsch added a commit to billbarsch/alpaca that referenced this issue Mar 16, 2015
This change make alpaca sends fields names as php can parce it like multidimentional arrays
As detailed explained here: 
gitana#221
@jamesmcb
Copy link

This is exactly what I needed, thanks for doing this investigation and proposing a workaround.

@gautiermichelin
Copy link

huge +1, or is this no more actual ?

@billbarsch
Copy link
Author

billbarsch commented Mar 17, 2017 via email

@seanvree
Copy link

How do we change this in the main alpaca.min.js script?

@billbarsch
Copy link
Author

You may edit the alpaca.js file, not the .min. file.

@NayanaMadhuwantha
Copy link

NayanaMadhuwantha commented Feb 10, 2023

Thank you @billbarsch you saved me!

The code little bit changed now

 calculateName: function()
                {
                    if (!this.name || (this.name && this.nameCalculated))
                    {
                        // has path?
                        if (this.parent && this.parent.name && this.path)
                        {
                            if (this.propertyId)
                            {
                                this.name = this.parent.name + "_" + this.propertyId;
                                this.nameCalculated = true;
                            }
                            else
                            {
                                var lastSegment = this.path.substring(this.path.lastIndexOf('/') + 1);
                                if (lastSegment.indexOf("[") !== -1 && lastSegment.indexOf("]") !== -1)
                                {
                                    lastSegment = lastSegment.substring(lastSegment.indexOf("[") + 1, lastSegment.indexOf("]"));
                                }

                                if (lastSegment)
                                {
                                    this.name = this.parent.name + "_" + lastSegment;
                                    this.nameCalculated = true;
                                }
                            }
                        }
                        else
                        {
                            // generate name from the path
                            if (this.path)
                            {
                                this.name = this.path.replace(/\//g,"").replace(/\[/g,"_").replace(/\]/g,"");
                                this.nameCalculated = true;
                            }
                        }
                    }
                },

In 2023 we have to edit two lines

this.name = this.parent.name + "_" + this.propertyId;
this.name = this.parent.name + "_" + lastSegment;

to

this.name = this.parent.name + "[" + this.propertyId + "]";
this.name = this.parent.name + "[" + lastSegment + "]";

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

No branches or pull requests

5 participants