Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to programmatically inspect if a field in a form has an error

jgiraldo1 May 29, 2020

Say I have a custom field customField1 in the current transition screen and I have behaviours that may set that customField to error

def customField1 = getFieldByName("customField1")
<Some condition>

customField1.setError("This field has an incorrect value")
or 
<Some other condition>
customField1.clearError()

is there a way to inspect the field to see if it currently has an error?

may be something like

customField1.hasError() that returns true if the field in the form is supposed to be showing an error like after the <Some condition> example above or false if the field is the form is not supposed to be showing an error like after the <Some other condition> example above.

Hope this makes sense. Thanks




1 answer

0 votes
Steven F Behnke
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 29, 2020

FormField has a #isValid() method. Could try that out.

jgiraldo1 May 31, 2020

Tried that after you suggested it but didnt work. isValid() method seems to work if instead of setError(Error String) I use setValid(false).

When would one use setError() and when setValid()? Wondering if there is any "official documentation" or best practices document that explains when to use each. The setValid() doesnt support the setting of text (presumably HelpText) to tell the user why the field contains an invalid value so you have to combine this with setHelpText() if you want / need that. The Error String in setError is actually used to setHelpText(), and presumably it sets some unreadable status of the field, but it doesnt seem to be the isValid status. 

The other thing that I noticed that that those inspection methods (isValid(), isReadOnly(), isRequired(), etc) ALWAYS return false unless you have explicitly set or changed the values  on the same script.  So either I don't understand how these are meant to be used or there is bug.

jgiraldo1 May 31, 2020

Also, it seems that even if you set a field as invalid with setValid(false), the user can still complete the transitions where are with setError(), the user cant complete the transition unless the error has been cleared (clearError()). So at least for my use case, setError() gives me more of what I need, except that I cant inspect a field to determine if it has a setError. 

jgiraldo1 June 16, 2020

Looks like setError changes isValid to false, but clearError does NOT change isValid to true. 

Also,

If you setValid to false, you can still complete the transition with the invalid entry.

Its just very strange the way this works

jgiraldo1 June 16, 2020

So you can end up with fields for which you get isValid = true, but cant still complete the transition because you didnt clearError().

You can also end up with fields for which you get isValid = false, but you can complete the transition.

If we are meant to only use setError and clearError, then shouldnt there be a method to check if a field is in this Error state ?

jgiraldo1 June 16, 2020

worse you can replace the error message with setHelpText('All is fine') and you end up with a field that returns isValid = True, a Help Text that says "All is fine", and still not be able to complete the transition

Suggest an answer

Log in or Sign up to answer