How can I limit the number of selections in multiple select? Say, a user should not be able to select more than 5 values in a multiple selection box. I am following the multiple select example here: http://www.gilluminate.com/2007/02/15/best-way-to-do-multiple-select-combo-boxes-in-rails/ . Thanks. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I don''t think you can with Rails (well, you could do it with validation on the model) but it''s easy enough with jQuery. Assuming you have a text box like this: <select name="Test" id="Test" multiple size="1"> <option value="option1">option1</option> <option value="option2">option2</option> <option value="option3">option3</option> <option value="option4">option4</option> <option value="option5">option5</option> <option value="option6">option6</option> <option value="option7">option7</option> </select> You can do this: <script type=''text/javascript''> if ($(''#Test'').val().length > 5) (''#submit'').hide(); } </script> Or take whatever action you want, but basically $(sel).val() returns an array for multiple select boxes. Cheers, Andy -- Andy Jeffries http://andyjeffries.co.uk/ #rubyonrails #mysql #jquery Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS Company number: 5452840 On 23 February 2010 22:30, Vincent P <easebus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How can I limit the number of selections in multiple select? Say, a > user should not be able to select more than 5 values in a multiple > selection box. I am following the multiple select example here: > > http://www.gilluminate.com/2007/02/15/best-way-to-do-multiple-select-combo-boxes-in-rails/ > . Thanks. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 24 February 2010 11:12, Andy Jeffries <andyjeffries-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> (well, you could do it with validation on the model)Isn''t that where it would be best?> You can do this: > <script type=''text/javascript''> > if ($(''#Test'').val().length > 5) > (''#submit'').hide(); > } > </script>but of course, we all know that relying solely on JS for validation is a sure method for letting duff data get into your DB (because not every browser has JS enabled or supported) By all means, use the JS to enhance the front end; but if you have to ensure there''s no more than a certain amount of associations, you *have* to check that in the model. You could also check the params array coming into the controller, and see what the size is there (unfortunately, I don''t have time right this second to try it out, but if you haven''t sorted it yourself later, re-post for help and I''ll have a play) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> > > (well, you could do it with validation on the model) > > Isn''t that where it would be best? >I agree.> > You can do this: > > <script type=''text/javascript''> > > if ($(''#Test'').val().length > 5) > > (''#submit'').hide(); > > } > > </script> > > but of course, we all know that relying solely on JS for validation is > a sure method for letting duff data get into your DB (because not > every browser has JS enabled or supported) > > By all means, use the JS to enhance the front end; but if you have to > ensure there''s no more than a certain amount of associations, you > *have* to check that in the model. > You could also check the params array coming into the controller, and > see what the size is there (unfortunately, I don''t have time right > this second to try it out, but if you haven''t sorted it yourself > later, re-post for help and I''ll have a play) >Completely agree with this. Cheers, Andy -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you all so much. Validation at the model level is an excellent idea. I should have thought about that. I am still getting into the habit of thinking the rails way. On Wed, Feb 24, 2010 at 4:28 AM, Andy Jeffries <andyjeffries-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:>> > (well, you could do it with validation on the model) >> >> Isn''t that where it would be best? > > I agree. > >> >> > You can do this: >> > <script type=''text/javascript''> >> > if ($(''#Test'').val().length > 5) >> > (''#submit'').hide(); >> > } >> > </script> >> >> but of course, we all know that relying solely on JS for validation is >> a sure method for letting duff data get into your DB (because not >> every browser has JS enabled or supported) >> >> By all means, use the JS to enhance the front end; but if you have to >> ensure there''s no more than a certain amount of associations, you >> *have* to check that in the model. >> You could also check the params array coming into the controller, and >> see what the size is there (unfortunately, I don''t have time right >> this second to try it out, but if you haven''t sorted it yourself >> later, re-post for help and I''ll have a play) > > Completely agree with this. > Cheers, > > Andy > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.