Jump to:

22996 Posts in 11620 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Auto submit FormAction after Listbox field selection

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 2470 Views
  • karibe
    Avatar
    Community Member
    56 Posts

    Auto submit FormAction after Listbox field selection Link to this post

    Hello

    I'm trying to obtain such behavior for my Page_Controller form:

       function changeLanguageForm()
       {
          $arrOptions = array();
          $arrCommonLocales = i18n::get_common_languages(true);
          foreach( Translatable::get_allowed_locales() as $value )
          {
             $langName = i18n::get_lang_from_locale($value);
             $arrOptions[$value] = $arrCommonLocales[$langName];
          }
          $action = new FormAction('submitLanguageChange', '', null, null);
          $fields = new FieldSet(
             new ListboxField('langs', "", $arrOptions, Translatable::get_current_locale())
          );
          $validator = new RequiredFields('langs');
          return new Form($this, 'changeLanguageForm', $fields, $action, $validator);
       }

    For pure html+js this pretty simple

    <select name='myfield' onchange='this.form.submit()'>
    <option .... >
    ...
    </select>
    </form>

    I have no idea how to do that in ss way...

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Auto submit FormAction after Listbox field selection Link to this post

    The "SS way" is to always use the Form Actions to submit a form. That said there is not straightforward way to change this.

    However I would just toss out a little jQuery script on your form page to bind to the onchange event for that item.

  • karibe
    Avatar
    Community Member
    56 Posts

    Re: Auto submit FormAction after Listbox field selection Link to this post

    How to do that? I suppose I have to hide submit button and fire it from js, any examples?

  • karibe
    Avatar
    Community Member
    56 Posts

    Re: Auto submit FormAction after Listbox field selection Link to this post

    OK I did it by jQuery:

    // Standard jQuery header
    ;(function($) {
    $(document).ready(function() {
       $("#Form_changeLanguageForm").find(":submit").css({display: "none"});// nie pokazuj szukania
       $("#Form_changeLanguageForm select").change(function(){
          $("#Form_changeLanguageForm").find(":submit").click();
          return;
       });
    // Standard jQuery footer
    })
    })(jQuery);

    And in Page_Controller init() method:

          Requirements::javascript("jsparty/jquery/jquery-packed.js");
          Requirements::javascript("themes/".SSViewer::current_theme()."/js/behaviours.js");

    Works fine but in firebug console I have frustrating error:

    jQuery is not defined

    How to eliminate last error?

    2470 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.