Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Check if field validation passed (isValid?)

Avatar

Former Community Member

Greetings,

I'm looking for a way to determine if a field is valid.  Is their some sort of boolean or function to determine whether or not a specific field (in its current state) passes the validation?

For example, lets say I have an Order, with a Line Item, and an Item Number.  If I want to find out whether the Item Number validation has passed (anywhere in my code), I want to be able to do something like:

if(Order.LineItem.ItemNumber.isValid == "True")

{

//do something

}

How can this be accomplished?

Thanks!!

GS

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

A couple of late comments here:

If you want to see if a date field matches a pattern you can:

a) Add a display pattern for the field

b) The field will be valid if:

field.rawValue != field.formattedValue

The reason this works is because if a field cannot be formatted with a picture clause, the formatted value will be the same as the raw value.

There's more help coming:

In acrobat 9.1 (XFA 3.0) there are new properties to determine if a field is valid.

You can call subform.getInvalidObjects() for a list of invalid fields.

See: http://blogs.adobe.com/formfeed/2009/03/xfa_30_list_of_invalid_fields.html

In addition, there is a new field property: field.errorText.

If this returns an empty string, the field is valid.

good luck.

  John

View solution in original post

14 Replies

Avatar

Level 4

i think what you have is close to what you want

i think the issue you might be having would arise from using the isValid = "true" statement - there is a presumption that something validation is true for something to be within the command line isValid - so with isValid = true your kind of saying is apple is apple

the syntax should work as

if (x.isValid)

   ABC.presence = "hidden";

else

   xyz.presence = "visible";

as an example

hope that helps

cheers

Avatar

Level 4

wow my english was awful there - i meant to say that something isValid only if it passed validation - so there would be a logic loop with isValid = "true"....

must be beeroclock already

Avatar

Former Community Member

Thanks for the assist.

But, is there an isValid?

That's what i'm curious about, because I don't see an isValid associated with the fields and haven't found anything similar.  I can do the check you suggested for, if(TheField.isValid) - but nothing happens because it doesn't look like there is an isValid

Am I missing something maybe?

Thanks!

GS

Avatar

Level 4

The missing thing is:  = "true"

Then it works

Avatar

Former Community Member

Ah okay, so its not "built-in" based on whether the validation actually passed or failed.  I have to set this manually for each field?

That's what I was hoping to avoid if there was already something "built-in" to accomplish this.

GS

Avatar

Level 4

What kind of field is it? What do you want to accomplish?

Like I said, I've got some validations for standard fields here...

(checking date and timefields f.ex. because there were people who said yesterday was a date and error messages have no meaning ;D)

Humm... that's strange... today I thought it already DID work...

Probably I have written the wrong code

Avatar

Former Community Member

Thanks for the follow-up!

It's primarily going to be used for Date fields - to make sure users don't type invalid information into the date (which the Adobe form allows, tsk tsk).  So for example, the user might enter 1/1/2000p (accidentally hitting a "p").  The "validation" for the date fails, and the user is informed - but they're still allowed to proceed.

But, we want to make sure that for any field that has validation associated - this is enforced.  So I want to iterate through "all fields" on the form when they click "Submit", and make sure validation has passed.  If it hasn't passed for even one field, they can't "Submit".

We have a *ton* of forms, many with repeating sections and whatnot, so it's not realistic for us to go through every single field and add something to the "validation event" if something already exists at the field-level.  I already have implemented a method that recursively loops through all fields on the form, i just need to figure out how to determine if that field passed the validation or not.

Hope this helps clarify

Avatar

Level 4

glad it's working - i was going from an example in J P terry's book - where

he just used the (if x.isValid) as the line and indicated that the is true

was a redundant loop

perhaps i misunderstood the master -

Avatar

Level 4

had another look at jp's code and your right he had set the isValid = true

earlier in the (kinda long) script - sorry about that

On Fri, Sep 11, 2009 at 9:03 AM, Legal Logic Limited <

Avatar

Level 4

I had the same problem, only that people here typed "yesterday" as a date...

Though I searched for that isValid thingy I couldn't find a way to get a grip of the pattern validation with script, neither the syslanguage. Both are in the pattern section and it seemed as if they can't get out that easily...

I'm not sure, if you got the solution already... ^^

Here was the script that I used, though it only accepts one kind of date (to say the truth, I was just too lazy to type all kinds in there... )

if(this.formattedValue!= null &&  this.formattedValue!= ""){
     var my_array=this.formattedValue.split("\.");
     var yy = Number(my_array[2]);
     var m = Number(my_array[1]);
     var d = Number(my_array[0]);
     var r = new RegExp(/^(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[0-2])\.(19|20|21|22)\d{2}$/);
     var result = r.test(this.formattedValue);
     if (!result || (d==31 && (m==2 || m==4 || m==6 || m==9 || m== 11)) || (m==2 && d==30) || (m==2 && d==29 && !((yy%4==0 && yy%100!=0) || yy%400==0)) ){
          xfa.host.messageBox("Please use the calender or the following format: DD.MM.YYYY.","Adobe Acrobat",0,0);
          this.rawValue="";
          xfa.host.setFocus(this.somExpression);
     }
}

It could be improved, since like stated it only allows dates like "01.01.2009" ... but this one did the job I wanted

(If they type it into the datefield 3 times wrong, they'll use the calender-function ;D )

Hope that helped you at least a little bit, though it is validating the field on the specific event.

Lisa

PS.: watch your timefields, they could have a similar problem. If you have an international form, even your decimalfields may be overgiven wrong since in some countries it's typed 19,22 $ and in others 19.22 $.

Avatar

Former Community Member

"had another look at jp's code and your right he had set the isValid = true

earlier in the (kinda long) script - sorry about that" -

No problem, thanks for the clarification!  I was hoping that their was a way to check field validation from script without being in the individual fields event, but alas I'm going to have to take another route it seems.

Thanks to everyone for all the good feedback and suggestions -

GS

Avatar

Correct answer by
Former Community Member

A couple of late comments here:

If you want to see if a date field matches a pattern you can:

a) Add a display pattern for the field

b) The field will be valid if:

field.rawValue != field.formattedValue

The reason this works is because if a field cannot be formatted with a picture clause, the formatted value will be the same as the raw value.

There's more help coming:

In acrobat 9.1 (XFA 3.0) there are new properties to determine if a field is valid.

You can call subform.getInvalidObjects() for a list of invalid fields.

See: http://blogs.adobe.com/formfeed/2009/03/xfa_30_list_of_invalid_fields.html

In addition, there is a new field property: field.errorText.

If this returns an empty string, the field is valid.

good luck.

  John

Avatar

Former Community Member

You sir, are the man!!

That was exactly what I needed, and we're looking forward to the new errorText and getInvalidObjects().

Much appreciated,

GS

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----