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.

Form Questions

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


Reply

3 Posts   1343 Views

Avatar
te_chris

1 October 2010 at 6:47pm (Last edited: 1 October 2010 6:48pm), Community Member, 24 Posts

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?

Avatar
Martijn

1 October 2010 at 9:35pm (Last edited: 1 October 2010 9:40pm), Community Member, 271 Posts

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){
      ${$req}->addExtraClass('required');
}

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

Edit:

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

http://silverstripe.org/form-questions/show/251573?start=8#post293059

Avatar
te_chris

3 October 2010 at 1:58pm Community Member, 24 Posts

perfect, thanks for that!