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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

Custom form template. Add js events.


Go to End


4 Posts   3997 Views

Avatar
dab

Community Member, 50 Posts

5 April 2009 at 11:44am

Hi there, need advice.

How i may change layout for my custom form, for example i have function MessageForm inside my PageController class.
I need custom input tag with onfocus event inside. Like <input type="text" value="Name" onfocus="..." />

Thanks.

Avatar
Fuzz10

Community Member, 791 Posts

22 April 2009 at 12:57am

Bump..

In need of this as well..

Avatar
Hamish

Community Member, 712 Posts

23 April 2009 at 10:27am

If you want to apply JavaScript methods to your forms, you could use a selector based JavaScript library like jQuery, rather than altering the form template.

For example, if you form looks like:

<form id="SearchForm" action="" method="get">
	<input type="text" name="SearchForm_Query" value="" />
	<input type="submit" vlaue="submit" />
</form>

You can use require::javascript in your page template to add some js that might look like:

jQuery('#searchForm').submit(function(){
	if(some expression) {
		return true; // submit form;
	} else {
		return false; // don't submit form;
	}
});

If you don't want to include the library, you could do it the old fashioned way too:

var el = document.getElementById('searchForm');
// etc....

If you actually want to change the template, you'll need to override the form field FieldHolder method. But you shouldn't need to do this. You should be able to control how it looks with CSS and what it does with included scripts. Inline JS is not template friendly :)

Avatar
Fuzz10

Community Member, 791 Posts

23 April 2009 at 7:59pm

Thanks Hamish, Indeed, the only way I could figure was going the javascript route. Used prototype to do that.