Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Adding form with multiple attribute?

Hey,

So I've been building some terrifyingly huge user profiles mod, and there's this whole user likes system which basically enables users to add/search for their favourite foods/artists/authors all that sorta social networky stuff. Managed to get my head around the Form class, however I can't seem to find a proper way of adding multi-selects; Am I just being blind here, or are they not actually supported in the form class yet?

Also, If they aren't in there yet, will there be any negative repercussions for manually adding a multi-select?

Tagged:

Comments

  • hgtonighthgtonight MVP
    edited August 2014

    You should be able to use Gdn_Form::DropDown() with array('multiple' => 'multiple') as the attributes.

    echo $Sender->Form->DropDown('FieldName', $Data, array('multiple' => 'multiple'));
    

    I haven't tested it, so let me know how it works out.

    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.

  • SudoCatSudoCat New
    edited August 2014

    I tried that, unfortunately when you are creating multiselects you also need to append [] to the end of the name attribute, and also add support for selecting multiple values, which this is not doing.

    EDIT: That paragraph wasn't very clear, what I mean is add support for multiple carrying a selected attribute. The [] is also required in order to post the data as an array (much like with checkboxes or radio buttons)

    Here's the code being created:
    <select id="Form_Miscellaneous" name="Miscellaneous" class="LikeTags Miscellaneous" multiple="multiple"> <option value="1">Test Like</option> <option value="2">Another Like</option> <option value="6">what is this</option> <option value="7">Hello World</option> <option value="8">A new tag thing</option> </select>

    Whereas it should be:
    <select id="Form_Miscellaneous" name="Miscellaneous[]" class="LikeTags Miscellaneous" multiple="multiple"> <option value="1" selected="selected">Test Like</option> <option value="2" selected="selected">Another Like</option> <option value="6" selected="selected">what is this</option> <option value="7" selected="selected">Hello World</option> <option value="8" selected="selected">A new tag thing</option> </select>

    However right now I'm still not certain whether the lack of selected status is my own fault - I suspect it is.

  • echo $Sender->Form->DropDown('FieldName[]', $Data, array('multiple' => 'multiple'));
    

    Should do it. The selected attribute is based on the dataset passed to the 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.

  • @hgtonight said:
    echo $Sender->Form->DropDown('FieldName[]', $Data, array('multiple' => 'multiple'));

    Should do it. The selected attribute is based on the dataset passed to the form.

    Awesome thanks, well I've got it working using a manual select for the time being as I'm not sure how well my data will get passed along and by this point in the day just the thought of trying to figure out how the dataset stuffs works exactly is making my head hurt! haha

  • What form is the dataset in?

    If it isn't a Gdn_Dataset object, you can specify what the text field and value field should be in the attributes array.

    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.

  • edited August 2014

    @hgtonight said:
    echo $Sender->Form->DropDown('FieldName[]', $Data, array('multiple' => 'multiple'));

    Should do it. The selected attribute is based on the dataset passed to the form.

    That is correct. However, you cannot specify named indexes. This prevents the possibility of taking advantage of grouped fields, which are supported by PHP. For example, the following won't work:

    echo $Sender->Form->DropDown('FieldName["SomeGroup"]', $Data, array('multiple' => 'multiple'));
    

    When I developed the Awards plugin, I needed to group fields for Award Rules, therefore I implemented such feature in my AFC plugin. This allows to specify fields like the following (pseudo code):

    Settings[Rules][Rule1][Setting1]
    Settings[Rules][Rule1][Setting2]
    Settings[Rules][Rule1][Setting3]
    
    Settings[Rules][Rule2][Setting1]
    Settings[Rules][Rule2][Setting2]
    Settings[Rules][Rule3][Setting3]
    

    And receive them automatically grouped, as follows:

    Settings['Rules'] = [
      'Rule1' = [
        'Setting1' = 'Value',
        'Setting2' = 'Value',
        'Setting3' = 'Value'
      ],
      'Rule2' = [
        'Setting1' = 'Value',
        'Setting2' = 'Value',
        'Setting3' = 'Value'
      ]
    ]
    

    I also remember that the multiple attribute didn't seem to work too well for me with standard Gdn_Form, therefore I implemented it in the AFC plugin as well. It's not a very popular plugin, but I don't remember a single plugin, theme or extension I developed that doesn't rely on it. Well, I developed it for a reason... :D

  • peregrineperegrine MVP
    edited August 2014

    Well, I developed it for a reason.

    self-reliance. :)

    good explanation.

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

  • @hgtonight said:
    What form is the dataset in?

    If it isn't a Gdn_Dataset object, you can specify what the text field and value field should be in the attributes array.

    Right I have a vaguely better idea of what's going on here now, I just did the previous one using some handwritten code as it just needs to be done, but figured I'd try and do it properly this time as it's only a standard select this time around.
    Now I've got it passing the dataset correctly, however my _Result from the query is an array not an object - Any ideas what I should do about this?

  • peregrineperegrine MVP
    edited September 2014

    how are you retrieving the dataset result ->Result(); or ->FirstRow(DATASET_TYPE_ARRAY); or something else, if you want to retrieve a different type of format.

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

  • Okay not really sure what I managed to do, poked and tweaked it around this morning and it's now working as expected - not really sure exactly what I changed though. I think I must've been accidentally passing it a result instead of dataset. Thanks all for your help!

Sign In or Register to comment.