Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not able to understand some javascript written in validator-rules.xml

 
Jignesh Gohel
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i am a beginner in struts.
I was going through a validator-rules.xml in a book & i found the following sampel file whcih defines javascript inside a javascript tag.I know it has been consolidated to commons-validator project since struts1.2 but i would like to understand the thing i have marked in BOLD and ITALICS in the following code.I am not able to understand the thing ??

 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Form may contain text,textarea,listbox,radio buttons,etc..But in our validation.xml we simply specify the form name,property name and validation types to be done.

While doing validation validator first want to check the type of field(say whether property belongs to text or radio button or textarea) and then type of validation is done.

In our case,Bolded area code first checks for field type,then validation type(mandatory field),checked on the basis of type of field.
 
Jignesh Gohel
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that i understood but i want to know what is this array about & what is oRequired .And what the form[oRequired[x][0]],form[oRequired[x][1]] contains??

And this for loop argument (x in oRequired) how it works???

var fields = new Array();
oRequired = new required();
for (x in oRequired)
{
var field = form[oRequired[x][0]];
....
}
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look under the heading "FOR/IN" in this link for an explanation of how the for/in loop works.

As for the statement:

oRequired = new required();

In JavaScript, you can create a new object simply by defining a function and using the keyword new with that function. So, elsewhere in the code or in one of the includes, you will find a function required(). That function most likely defines properties, something like this:

function required() {
this.property1 = 'abc';
this.property2 = 'xyz';
this.property3 = new Array();
this.property3[0] = 'foo';
this.property3[1] = 'bar;';
}

The for/in loop simply iterates through the properties of the "required" object.

The code you are analyzing is expecting the properties of the "required" object to be arrays.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic