function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TehNrdTehNrd 

How to handle the error when inputText is set to incorrect data type?

In the process of building out a Visualforce page I have run into an error I am not sure how to handle. I have an inputText field that maps to an Integer variable in the controller. It works great if the user enters in a Integer. Yet if they enter letters or decimal the logic fails in the controller and the debug log shows the following ...

j_id0:j_id1:j_id2: An error occurred when processing your submitted information.

...and the page appears to do nothing, no displayed errors or anything:

It would be great if the VF page automatically displayed an error message similar to what is does for inputFields. Something like "Invalid Integer" or what ever data type it  is supposed to be. I've attached code below that compares the behaviors.

What should I do to handle this? Its looking like I will have to use a string variable and then convert that to an integer but then I will need to catch any errors if the conversion fails which intern requires more unit tests. More work than I'd like to do if possible.

Ideas, thoughts?

Code:
//When you enter an invalid probability it lets you know. If you enter an invalid integer it gives no warning.

<apex:page controller="input">
        <apex:outputPanel id="panel">
                <apex:form>
                        <apex:inputText value="{!numberEntry}" /><br/>
                        <apex:inputField value="{!opp.Probability}" /><br/>
                        <apex:commandButton action="{!submit}" value="Submit" rerender="panel"/>
                </apex:form>
        </apex:outputPanel>
</apex:page>

public class input {

        Integer numberEntry;

        public Integer getNumberEntry() {
                return numberEntry;
        }

        public void setNumberEntry(Integer numberEntry){
                this.numberEntry = numberEntry;
        }

        Opportunity opp = new Opportunity();
        
        public Opportunity getopp() {
                return opp;
        }

        public PageReference submit() {
                
                system.debug(numberEntry);
                system.debug(opp.probability);
                return null;
        }
}

 


Message Edited by TehNrd on 05-01-2008 10:42 AM
Ron HessRon Hess
Why not use an input field bound to a number in an sobject, then the input field will check the data typed into it.

all you need in your controller is an empty sobject that just holds a number field.

something like this
  <apex:inputField value="{!opp.someNumberField}" /><br/>

if you don't have such a field, you could create one for this reason, or just use another sobject that has a number as a temporary number holder.

sort of like this:
<apex:inputField value="{!account.NumberOfEmployees}" /><br/>
then in the controller :

 Account tmp = new Account();
 getAccount() {return tmp; }
 
then when you need the number, just get it out of tmp.NumberOfEmployees

never insert the temporary account and no harm done, in this case Account tmp is just a holder for the number you want the user to enter.
What you get is all the features of inputField without adding a number to your opportunity object.



TehNrdTehNrd
I didn't even think of that. I've done that for lookup fields to capture IDs but never even thought of that for other types of variables.

Once again, thanks for the help Ron. I owe you a beer or something.
jwetzlerjwetzler
A few things I'd like to add:

The thing about inputText is that it's designed to be a very simple input box with no validation attached.  We give you all of the validation rules with inputField (which is why Ron's suggestion is perfect for your case) and give you all of the salesforce styling (error messaging included), but we need to have inputText to be a vanilla solution for developers who don't want salesforce behavior.  Sure, we could take every inputText component you have and do some validations on it (requiredness, etc.) and then if there's an error, surround it with a big, bold, red box, but what about the developers who have their own UI design for error messaging?  inputText is the only component they can use then, and that's why we keep this validation limited to inputField.  You can supply your own validation if you'd like (more work than using the inputField proxy, though), which would give you complete controll over the placement of your error messaging.

I bet if you had had the "messages" component on your page you would have seen some sort of error.  It's good to have that on your page while you're developing, and especially good to add when your page looks like it's refreshing and not doing what you ask it to.

When you aren't using the messages component, it's difficult for us to know what to do (and as a team we've had several discussions about this).  It's not very appropriate for us to just inject the error messaging we think you want directly on your page somewhere.
Steve ChadbournSteve Chadbourn

How do validation rules work with inputField? Are you talking about Summer 08?

I use inputField wherever possible and still have to do all my validation manually.

TehNrdTehNrd
Steve, validations rules are enforced/executed during DML operations. The type of validation I am referring to is that if I there is an Integer variable in the controller but someone enters text into the InputText field on the visualforce page it will error out and there is no real way to catch this.

Using fields on SObjects allows all field validation to be done in real time without lots of extra code.  I will post some code that shows what I did.

Code:
PAGE:
<apex:outputPanel id="splitPercent1">
        <apex:inputField value="{!targetOppPercent1.Split_Percent__c}">
              <apex:actionSupport event="onblur" rerender="splitPercent1"/>
        </apex:inputField>
</apex:outputPanel>

CONTROLLER:
Opportunity targetOppPercent1 = new Opportunity();

//where Split_Percent__c is a custom number field on the Opportunity object with no decimals and only 3 characters long.


What is cool about the code above is that as soon as the user navigates away from the inputField the panel will refresh and display any errors. Example, user enters text, the "Error: Invalid number" message will appear and if they enter 7273, "Error: Number is too large" both shown with standard red highlighting. The only validation I have left to do in this example is make sure the input is greater than 0 and less than or equal to 100 and this can easily be done in the controller.

FYI, this is example is from a converted s-control that relied on javascript alert() to display most input errors which I despise for a few reasons. 1, its a popup and 2, it add unnecessary clicks.

Jill, thanks for the detail behind the thought process for this behavior. The more familiar I become with VF the more sense it makes.



Message Edited by TehNrd on 05-04-2008 08:54 PM
TehNrdTehNrd
So I've hit one little snag when using inputFields on sObjects. When I used the inputText component it had the attribute size which allowed me to control the width but on inputField there is no such attribute. I assume I need to override the width with some custom styling but this is where I run in to trouble. Here is what I have so far but it does not work.

<style>
    .split {width: 15px}
</style>

<apex:inputField value="{!sourceOppPercent.Split_Percent__c}" style="split"/>


EDIT:
This worked: <apex:inputField value="{!sourceOppPercent.Split_Percent__c}" style="width: 25px"> but I would still like to define this style in one place and use it for many fields.


Message Edited by TehNrd on 05-05-2008 12:37 PM
jwetzlerjwetzler
Well if you're going to be referring to a class you need to be using the styleClass attribute instead of style.

Disclaimer: we have some inconsistencies between different types of fields where style and styleClass values actually work (meaning if you view the source they are actually being appended as an attribute to the various input tags that get generated).

We're working on fixing this for all of our various types of fields but unfortunately not all of them work right now.

Jill
kdmrkdmr

Hi, Have you found a solution to your question as to how to make a inputtext accept only numeric values? If you have please do share the same. I am also facing the same issue. For the time being I am using the following code for a solution

 

boolean check = pattern.matches('[a-zA-Z]+', inputtextvalue);

if(check == true) {

your error message

}

 

Thanks

KD