Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

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

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

Auto submit FormAction after Listbox field selection


Go to End
Reply


5 Posts   3867 Views

Avatar
karibe

Community Member, 56 Posts

22 November 2009 at 6:10am

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

Avatar
dalesaurus

Community Member, 283 Posts

26 November 2009 at 6:32am

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.

Avatar
karibe

Community Member, 56 Posts

26 November 2009 at 9:08pm

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

Avatar
karibe

Community Member, 56 Posts

7 December 2009 at 11:06pm

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?

Avatar
mandrew

Community Member, 37 Posts

8 January 2016 at 3:15pm

If anyone has this error "jQuery is not defined" then check the order of the included javascript files. If jQuery isn't included before other required js files then you will have problems