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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

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   1880 Views

Avatar
px

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();
$newsletter->Email=$_POST['Email'];
$newsletter->write();
return Director::redirectBack();

//Director::redirect(Director::currentURLSegment()."?msg=Thank+you+for+signing+up!");
}

Any help will be much appreciated. Thanks!

Avatar
px

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.

Avatar
UncleCheese

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;
});

Avatar
UncleCheese

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 %>
$FieldHolder 
<% end_control %>
</div>

<% control Actions %>
$FormHolder
<% end_control %>
</form>