Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » using jquery for form validation, how do i change the class attr of an input?

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

Page: 1
Go to End
Author Topic: 1322 Views
  • te_chris
    Avatar
    Community Member
    24 Posts

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

    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?

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: using jquery for form validation, how do i change the class attr of an input? Link to this post

    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

  • te_chris
    Avatar
    Community Member
    24 Posts
    1322 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.