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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

Validate - Limit # of Checkboxes or Customize Name within Input


Go to End


2 Posts   1598 Views

Avatar
socks

Community Member, 191 Posts

22 August 2011 at 6:27pm

I'm trying jQuery Validate to limit the # of checkboxes a user can choose in my form (MultiForm module). To do that, the set of checkboxes need to have the same name=" " within the input.

ie.

<input name="tech" id="ebio" type="checkbox" value="" />
<input name="tech" id="mna"  type="checkbox" value="" /> 

If I use something like $dataFieldByName(Bla) to generate the input field in the template, it doesn't allow me to specify the name part of the input that I know of.

If I write out the input manually, the checkbox state isn't being saved when I go to another page/step of the form and come back.

Any Suggestions?

Avatar
Devlin

Community Member, 344 Posts

22 August 2011 at 8:59pm

http://api.silverstripe.org/trunk/forms/fields-basic/CheckboxSetField.html

To limit the user input with jQuery, simply count all checked children of .checkboxsetfield. Something like:

var count = $("ul.checkboxsetfield input:checked").length;

http://api.jquery.com/checked-selector/