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:

Form Questions /

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

using jquery for form validation, how do i change the class attr of an input?

Go to End

3 Posts   2053 Views


Community Member, 24 Posts

1 October 2010 at 6:47pm

Edited: 01/10/2010 6:48pm

Hey guys,

So prototype was being an exceptional PITA wtih all my other jquery ajax and UI stuff so in order to simplify my site i've got rid of all the prototype validation. The problem is that now the easiest way to use jQuery's validation plugin is just to simply add, to the class attribute of each input, "required". I've seen the ssbits tutorial, but it's pretty hacky when one can do it this way. Is there any way to simply add an extra class definition to my form code, without creating a whole separate template for it, and end all the time i'm wasting trying to get this working, or do I have to keep dealing with writing a whole new bunch of JS rules per form?


Community Member, 271 Posts

1 October 2010 at 9:35pm

Edited: 01/10/2010 9:40pm

I solved it this way:

//add fields
$details = new CompositeField(
		$Name = new TextField("Name", _t("Member.NAME","Naam")), 
		$Email = new TextField("Email", _t("Member.EMAIL","Email")),
		$Tel = new TextField("Tel", _t("Member.PHONE","Telefoon") )

//required array
$defaultRequired = array('Name', 'Email', 'Tel');

// add required class to the required fields		
foreach($defaultRequired as $req){

// add them to required fields for serverside validation		
$v = new RequiredFields($defaultRequired);


Here is a slightly different way, which uses the fieldByName on a FieldSet.


Community Member, 24 Posts

3 October 2010 at 1:58pm

perfect, thanks for that!