# "form\_for" names fields using brackets = javascript problems

**URL:** <https://www.ruby-forum.com/t/form-for-names-fields-using-brackets-javascript-problems/108476>\
**Category:** Rails\
**Created:** [July 3, 2007, 11:18pm UTC](https://www.ruby-forum.com/t/form-for-names-fields-using-brackets-javascript-problems/108476 "2007-07-03T23:18:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreas\_Hallberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@Andreas\_Hallberg](https://www.ruby-forum.com/u/Andreas_Hallberg)\
**Post date:** [July 3, 2007, 11:18pm UTC](https://www.ruby-forum.com/t/form-for-names-fields-using-brackets-javascript-problems/108476/1 "2007-07-03T23:18:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andreas\_Hallberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@Andreas\_Hallberg](https://www.ruby-forum.com/u/Andreas_Hallberg)\
**Post date:** [July 4, 2007, 2:48am UTC](https://www.ruby-forum.com/t/form-for-names-fields-using-brackets-javascript-problems/108476/2 "2007-07-04T02:48:56Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Andreas\_Hallberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@Andreas\_Hallberg](https://www.ruby-forum.com/u/Andreas_Hallberg)\
**Post date:** [July 4, 2007, 7:58am UTC](https://www.ruby-forum.com/t/form-for-names-fields-using-brackets-javascript-problems/108476/3 "2007-07-04T07:58:32Z")

</div>

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
