Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product

Adding array to hidden form data

hgtonighthgtonight ∞ · New Moderator
edited August 2013 in Development

I am having trouble passing data through to another form.

I have an array of UserIDs that I want to pass from one form into a different form. I had the bright idea of using Form->AddHidden() on the controller side, but I can't figure out how to pass it an array of data.

Current workflow:

  1. Inject form fields through a plugin hook Check
  2. Post form upon control change to a different controller via JS Check
  3. Process received data and add new array to form data ???
  4. Show form via standard Vanilla syntax Check
  5. Process data as usual Check but I can't access the array
  6. Profit!

Thoughts?

Search first

Check out the Documentation! We are always looking for new content and pull requests.

Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

Comments

  • sorry can you explain better what it is you are trying to do?

    Why are you passing a bunch of ids via form fields?

    grep is your friend.

  • peregrineperegrine MVP
    edited August 2013

    Did you initialize the array first? or is the data there, but you just don't know how to get to it?

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • You can use the form name="FormPrefix/FieldName[]"

    Try this

    $this->Form->AddHidden('FieldName', array(1,2,3),TRUE);
    

    Or

    $this->Form->AddHidden('FieldName', array(1,2,3));
    echo $this->Form->Hidden('FieldName[]',array('value'=>'1'));
    echo $this->Form->Hidden('FieldName[]',array('value'=>'2'));
    echo $this->Form->Hidden('FieldName[]',array('value'=>'3'));
    

    grep is your friend.

  • x00x00 MVP
    edited August 2013

    On second thoughts you may have to do it like this

    $this->Form->AddHidden('FieldName[0]', 1,TRUE);
    $this->Form->AddHidden('FieldName[1]', 2,TRUE);
    $this->Form->AddHidden('FieldName[2]', 3,TRUE);
    

    You could of course serialise.

    grep is your friend.

  • hgtonighthgtonight ∞ · New Moderator

    Thanks for the replies :D

    So far, none of this has worked.

    I am writing a plugin that injects some checkboxes into existing dashboard fields. I am then submitting that form via JS so I can post the form data to a different controller. I have the controller set up and am able to recieve/process the data just fine.

    If I just do the actions I want to, everything is hunky dory. I really need a confirm button before releasing this as a plugin. So I need to figure out how to pass the array of userids back into the different form.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • if you want to pm where to see code, I'd be glad to check it out later today.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • why don't you just serialise the array?

    grep is your friend.

  • What are you doing with these ids?

    grep is your friend.

  • hgtonighthgtonight ∞ · New Moderator

    @x00 said:
    why don't you just serialise the array?

    I should have thought of this. I seem to be having some issues thinking outside the box.

    @x00 said:
    What are you doing with these ids?

    Different things. Always editing some aspect of the list of users.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • businessdadbusinessdad Stealth contributor MVP

    @hgtonight said:
    I should have thought of this. I seem to be having some issues thinking outside the box.

    Well, serialisation is quite thinking "inside" the box. :)
    Personally, in this cases, I serialise using either serialize() or, better, json_encode(). I tend to prefer the latter because I can use field content in JavaScript as well. Besides, it's more readable and easier to debug, in my opinion.

    hgtonight
  • businessdadbusinessdad Stealth contributor MVP

    @x00 said:
    You can use the form name="FormPrefix/FieldName[]"

    I'm afraid that it won't work. Vanilla "sanitises" field names, and it escapes square brackets. When I developed my Badges plugin, I wanted to group fields in a hierarchical way and I had to develop an override mechanism to ensure that the square brackets were preserved.

  • hgtonighthgtonight ∞ · New Moderator

    Serializing worked, @x00. Thanks!

    @businessdad Thinking inside the box might be the wrong term. More like "I have a hammer and dammit if this screw won't work as a nail!" I really like the idea of json_encoded data, since I will be expanding this in the future.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

    peregrine
  • @businessdad said:
    I'm afraid that it won't work. Vanilla "sanitises" field names, and it escapes square brackets. When I developed my Badges plugin, I wanted to group fields in a hierarchical way and I had to develop an override mechanism to ensure that the square brackets were preserved.

    I use

        echo $this->Form->TextBox('MetaName[]',array('class'=>'InputBox MetaName'));
        echo $this->Form->TextBox('MetaValue[]',array('class'=>'InputBox MetaValue'));
        echo $this->Form->Dropdown('MetaAny[]',array(T('Off'),T('On')),  array('class'=>'MetaAny','readonly'=>'readonly','style'=>'visibility:hidden'));
    

    In one of my plugins then I use javascript to extend.

    It does escape when you place some value in the brackets, I experimented a bit.

    grep is your friend.

    peregrinehgtonight
  • library/core/class.form.php

       public function EscapeString($String) {
          $Array = FALSE;
          if (substr($String, -2) == '[]') {
             $String = substr($String, 0, -2);
             $Array = TRUE;
          }
          $Return = urlencode(str_replace(' ', '_', $String));
          if ($Array === TRUE) $Return .= '[]';
    
          return str_replace('.', '-dot-', $Return);
       }
    

    grep is your friend.

    hgtonight
Sign In or Register to comment.