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

Avatar
te_chris

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?

Avatar
Martijn

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){
      ${$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

Community Member, 24 Posts

3 October 2010 at 1:58pm

perfect, thanks for that!