Forum: Ruby on Rails "form_for" names fields using brackets = javascript problems

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Andreas Hallberg (Guest)
on 2007-07-04 01:18
Hi all,

The "form_for" helper and its friends "text_field_for" etc generate form
fields with names like "forum[title]”, i.e. using square brackets to
denote attributes on model objects.

Now, for the convenience of the user I want to do some simple
client-side validation to check that some fields have been filled in. So
I am trying to write javascript like

"if (myForm.forum[title].value == '') { return false; }",

 but ofcourse the  "title" in the brackets is interpreted as the name of
a child object of a "forum" object in the DOM for the page. Doh!

Is there a way to solve this?

I realize this is more a javascript question than RoR question, but I
have looked fruitlessly for an answer and I figure people trying
client-side validation in RoR must have stumbled on this problem.

Please note that I am NOT using client-side validation to replace
server-side, it is just used as an added benefit for javascript enabled
clients.

Thanks,

/Andreas
Philip H. (Guest)
on 2007-07-04 04:48
(Received via mailing list)
> The "form_for" helper and its friends "text_field_for" etc generate form
> fields with names like "forum[title]", i.e. using square brackets to
> denote attributes on model objects.
>
> Now, for the convenience of the user I want to do some simple
> client-side validation to check that some fields have been filled in. So
> I am trying to write javascript like
>
> "if (myForm.forum[title].value == '') { return false; }",

myForm.elements['forum[title]'].value

-philip
Andreas Hallberg (Guest)
on 2007-07-04 09:58
Philip H. wrote:
>> The "form_for" helper and its friends "text_field_for" etc generate form
>> fields with names like "forum[title]", i.e. using square brackets to
>> denote attributes on model objects.
>>
>> Now, for the convenience of the user I want to do some simple
>> client-side validation to check that some fields have been filled in. So
>> I am trying to write javascript like
>>
>> "if (myForm.forum[title].value == '') { return false; }",
>
> myForm.elements['forum[title]'].value
>
> -philip

Thanks philip! I'll try it out later today, seems like it will get the
job done!

/Andreas
This topic is locked and can not be replied to.