Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Form Dropdown only sending ID and not the value

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1785 Views
  • socks
    Avatar
    Community Member
    190 Posts

    Form Dropdown only sending ID and not the value Link to this post

    The dropdown in my form is only submitting the ID (ie 4) instead of the value (ie Atlanta, Georgia 323 S. 2nd St.). In the $source, I tried switching out "ID" with "LocationDropDown" but that resulted in no data in the dropdown on the site.

    Do I need something else to process the form instead of $data['StoreLocale']?

    StoreLocation.php

    class StoreLocation extends DataObject {
       
    public static $db = array(
          'Address' => 'Varchar(80)',
          'City' => 'Varchar',
          'State' => 'Enum("-- Select a state --,Alabama,...,Wyoming")',
       );
       
       // Populate Dropdown on Contact Form
       function LocationDropDown() {
           return $this->City . ', '. $this->State . ' - '. $this->Address;
       }

    }

    ContactPage.php

    class ContactPage_Controller extends Page_Controller {

       // CONTACT FORM

       function ContactForm() {
          // Create fields
          $fields = new FieldSet(
             ...
             new DropdownField(
              $name = 'StoreLocale',
              $title = 'If your comments pertain to a specific store, please pick a location',
              $source = Dataobject::get("StoreLocation")->map("ID", "LocationDropDown", $emptyString = "-- Select Location --", $sortByTitle = false),
              $value = 0
             ),
    ...
          );
       
          // Create actions
          $actions = new FieldSet(
           new FormAction('doContactForm', 'Submit Form')
          );

          $validator = new RequiredFields("Email","Comments");
          return new Form($this, 'ContactForm', $fields, $actions, $validator);
       
       }
       
       // PROCESS THE FORM
       function doContactForm($data, $form) {
          
          //Set data
    ...
          $body = '<h1>Feedback / Comments</h1><p><b>From:</b> '.$data['Email'].'</p>'.'<p><b>Location:</b> '.$data['StoreLocale'].'</p>'.$data['Comments'];
          $email = new Email($from, $to, $subject, $body);
          $email->send();
       }
    }

    Thanks

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Form Dropdown only sending ID and not the value Link to this post

    This is how DropdownFields work. If you want the returned data to be your 'LocationDropDown' field then simply map it to that instead of ID.

    Dataobject::get("StoreLocation")->map("LocationDropDown", "LocationDropDown");

    Usually an ID is much more useful as you can do a DataObject::get_by_id() in the process function and get the entire object rather than just a string but in your case it would work either way since all you want is the 1 address.

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Form Dropdown only sending ID and not the value Link to this post

    Will's code won't work due to the way the map() function makes it indexes.

    $values = Dataobject::get("StoreLocation")->column('LocationDropDown');
    ...
    new DropdownField(
    'StoreLocale',
    'If your comments pertain to a specific store, please pick a location'
    array_combine($values, $values),
    null,
    null,
    '-- Select Location --'
    )
    ...

  • socks
    Avatar
    Community Member
    190 Posts

    Re: Form Dropdown only sending ID and not the value Link to this post

    @Willr
    If I was just grabbing the City or State or Address (ie "Address", "Address") that would have worked. But since I had the LocationDropDown() to concatenate the city, state - address, replacing the "ID" broke the form.

    @simon_w
    That solution worked for me

    Thank you both!

    1785 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.