You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by PSW <su...@wachtel.us> on 2006/02/21 20:01:50 UTC

Proper Javascript and Form integration

Hi there!

I got a custom Javascript function working on my page, but I'm insecure 
about its safety because I refer to Tapestry-generated form names.  I 
believe that there must be an accepted way of doing this, but I haven't 
been able to find it.  I'm hoping that other users out there might be 
able to confirm my method or correct it.

*Brief explanation of what the script does:*
I have a form that has an arbitrary number of rows, and the users count 
them differently for different reasons.  So, they requested that I add a 
form field that they can enter a starting number and have it 
automatically count the rows from that starting number.  If they enter 
three (3) in the form field, the rows will be refreshed to start 
counting at 3.  If they enter -1, same thing (skipping zeros).

*How I did it:*
In order to get the row count for the loop, I created a hidden field at 
the end of the loop with the count for the script to reference:

<input jwcid="@Any" type="hidden" name="numLines" value="ognl: 
periodDayNumber"/>

Each row (within the loop) has the following tag where the script refers 
to the id of "dayCount0", "dayCount1", etc:

<span jwcid="@Any" id='ognl: "dayCount" + periodDayNumber'>-</span>

The field that contains the starting number (or offset) is an actual 
Tapestry field.  Although I'd like feedback on my approach to this 
problem in general, my most specific question lies here.  The Tapestry code:
                <input type="text"
                       jwcid="@ValidField"
                       value="ognl: visit.schedule.startingDay"
                       validator="ognl: beans.int"
                       displayName="Enter the starting day number (eg. 
-1, 1, -3, but not 0)"
                       onChange="writeDayNumbers();"/>

This field becomes $ValidField when rendered, and I use it in the 
script.  Somehow, I think this is bad.

/The script:/
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script PUBLIC

    "-//Apache Software Foundation//Tapestry Script Specification 3.0//EN"

    "http://jakarta.apache.org/tapestry/dtd/Script_3_0.dtd">

<script>
  <body>
<![CDATA[
function writeDayNumbers() {
   var offset = document.Form0.$ValidField.value;
   var dayCount = 0;
   for (i = 0; i <= document.Form0.numLines.value; i++) {
       var dayCountField = document.getElementById("dayCount" + i);
       dayCount = parseInt(offset) + parseInt(i)
         if (dayCount == 0) { // zero is not a legit day. it goes from 
-1 to 1
                 dayCount++;
                 offset++;
         }
       dayCountField.firstChild.nodeValue=dayCount;
     }
}
]]>
  </body>
  <initialization>
      writeDayNumbers();
  </initialization>
</script>

So, bottom line.  How close am I to having a safe script?  Is the way I 
refer to the form field ok or not?

Thanks so much.  Tapestry is awesome!

Thanks,
PSW

Re: Proper Javascript and Form integration

Posted by Chris Chiappone <ch...@gmail.com>.
You should use explicit names for your components.  For instance
myValidField@ValidField and myFormName@Form.  Then you will know what
each on is.


On 2/21/06, PSW <su...@wachtel.us> wrote:
> Hi there!
>
> I got a custom Javascript function working on my page, but I'm insecure
> about its safety because I refer to Tapestry-generated form names.  I
> believe that there must be an accepted way of doing this, but I haven't
> been able to find it.  I'm hoping that other users out there might be
> able to confirm my method or correct it.
>
> *Brief explanation of what the script does:*
> I have a form that has an arbitrary number of rows, and the users count
> them differently for different reasons.  So, they requested that I add a
> form field that they can enter a starting number and have it
> automatically count the rows from that starting number.  If they enter
> three (3) in the form field, the rows will be refreshed to start
> counting at 3.  If they enter -1, same thing (skipping zeros).
>
> *How I did it:*
> In order to get the row count for the loop, I created a hidden field at
> the end of the loop with the count for the script to reference:
>
> <input jwcid="@Any" type="hidden" name="numLines" value="ognl:
> periodDayNumber"/>
>
> Each row (within the loop) has the following tag where the script refers
> to the id of "dayCount0", "dayCount1", etc:
>
> <span jwcid="@Any" id='ognl: "dayCount" + periodDayNumber'>-</span>
>
> The field that contains the starting number (or offset) is an actual
> Tapestry field.  Although I'd like feedback on my approach to this
> problem in general, my most specific question lies here.  The Tapestry code:
>                 <input type="text"
>                        jwcid="@ValidField"
>                        value="ognl: visit.schedule.startingDay"
>                        validator="ognl: beans.int"
>                        displayName="Enter the starting day number (eg.
> -1, 1, -3, but not 0)"
>                        onChange="writeDayNumbers();"/>
>
> This field becomes $ValidField when rendered, and I use it in the
> script.  Somehow, I think this is bad.
>
> /The script:/
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE script PUBLIC
>
>     "-//Apache Software Foundation//Tapestry Script Specification 3.0//EN"
>
>     "http://jakarta.apache.org/tapestry/dtd/Script_3_0.dtd">
>
> <script>
>   <body>
> <![CDATA[
> function writeDayNumbers() {
>    var offset = document.Form0.$ValidField.value;
>    var dayCount = 0;
>    for (i = 0; i <= document.Form0.numLines.value; i++) {
>        var dayCountField = document.getElementById("dayCount" + i);
>        dayCount = parseInt(offset) + parseInt(i)
>          if (dayCount == 0) { // zero is not a legit day. it goes from
> -1 to 1
>                  dayCount++;
>                  offset++;
>          }
>        dayCountField.firstChild.nodeValue=dayCount;
>      }
> }
> ]]>
>   </body>
>   <initialization>
>       writeDayNumbers();
>   </initialization>
> </script>
>
> So, bottom line.  How close am I to having a safe script?  Is the way I
> refer to the form field ok or not?
>
> Thanks so much.  Tapestry is awesome!
>
> Thanks,
> PSW
>
>


--
~chris

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org