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.

All other Modules /

Discuss all other Modules here.

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

Adding attributes to a form

Go to End

4 Posts   1859 Views


Community Member, 10 Posts

23 November 2009 at 10:50pm

Hi! I'm a newbie in silverstripe and I would just like to ask if how can i add an attribute in a form? For example i want to add onsubmit=''return false;" and class="myClass". Here is the function that I'm using:

public function NewsletterForm() {
$fields = new FieldSet(
new EmailField('Email','')

$actions = new FieldSet(
new FormAction('NewsletterSubmit', 'Ok!')

$validator = new RequiredFields('Email','onsubmit');
return new Form($this, 'NewsletterSubmit', $fields, $actions,$validator);

* Inserts a submitted email to database
* @param <type> $form
public function NewsletterSubmit($form) {
$newsletter = new Newsletter();
return Director::redirectBack();


Any help will be much appreciated. Thanks!


Community Member, 10 Posts

24 November 2009 at 3:39pm

Edited: 24/11/2009 3:44pm

hello?is there anyone who knows the answer to my simple question?

it's like having this in form tag:

<form name="frmName" method="post" action="home/newsletterSubmit" class="myClass" onsubmit="return false;">

i just want to add class and onsubmit attributes in this form.


Forum Moderator, 4102 Posts

24 November 2009 at 4:19pm

You should never mix javascript and HTML. Assign your event handlers unobtrusively in a javascript file.

$('#my-form').submit(function() {
// do stuff
return false;


Forum Moderator, 4102 Posts

24 November 2009 at 4:34pm

If you need to customize your form, there's nothing forcing you to use the default form markup. That's just for quick rendering and prototyping, IMO.

<% control MyForm %>
<form $FormAttributes>
<div class="customStuff">
<% control Fields %>
<% end_control %>

<% control Actions %>
<% end_control %>