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
kurtz_wolfgangkurtz_wolfgang 

pageMessages and validation error messages

hello all,

 

  I need your expert guidance in the following matter.

 

here's my two sets of codes from Apex class:

 

        if (!isValidUserName()){
           ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Username should be a valid email address.');
            ApexPages.addMessage(msg);
            return null;
        }

   private boolean isValidUserName(){
        String at = '@';
        String dot = '.';
        Integer iLenAt = username.indexOf(at);
        Integer iLenDot = username.indexOf(dot,iLenAt);
        
        if(username.indexOf(at) <= 0 || username.indexOf(at) == username.length() || username.indexOf(dot) <= 0 
        || username.indexOf(dot) == username.length() || username.substring(iLenAt+1,iLenAt+2)==dot || username.indexOf(dot,(iLenAt+2))==-1 || username.indexOf(' ') !=-1 ){
            return false;
        }
 
        return true;
    }

/*----------------------------------------*/
public boolean checkFirstName(){
         if (first_Name.length() == 0 ){
//             ApexPages.Message msgFirstName = new ApexPages.Message(ApexPages.Severity.INFO, 'First Name is required.',  'First Name is required.');
            ApexPages.Message msgFirstName = new ApexPages.Message(ApexPages.Severity.ERROR, Label.First_Name_Required);
            ApexPages.addMessage(msgFirstName);
 
            bRetVal= false;
        }
}

 

 

my visualforce page contains the following components:

 

 

	          						<tr>
	          							<td>
	          								<apex:outputLabel value="First Name: " for="firstName" styleClass="applicationInformationLabelRequired"/>
	          							</td>
	          							<td>
	          								<apex:inputText required="true" id="firstName" value="{!firstName}" size="25"/>
	          							</td>
	          						</tr>
	          						<tr>
	          							<td>
	          								<apex:outputLabel value="{!$Label.site.username}/Email: " for="username" styleClass="applicationInformationLabelRequired"/>
	          							</td>
	          							<td>
	          								<apex:inputText required="true" id="username" value="{!username}" size="25"/> 
	          							</td>
	          						</tr>

 

 

both set of codes are similar and simple however for firstname the message is printed:

:theForm:theBlock:Section:firstName: Validation Error: Value is required.

 

which is wrong.

 

where as for user name the message is :

Username should be a valid email address.

 

which is what I want.

 

Any help would be greatly appreciated.

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

It looks like this error message is being added in as a side effect of the required="true" attribute on the input field.  When you submit the form, that validation will kick in before your controller gets a chance to check the field.

 

As you are validating this server side, do you need the required attribute on the inputfield?

All Answers

bob_buzzardbob_buzzard

It looks like this error message is being added in as a side effect of the required="true" attribute on the input field.  When you submit the form, that validation will kick in before your controller gets a chance to check the field.

 

As you are validating this server side, do you need the required attribute on the inputfield?

This was selected as the best answer
sravusravu

Have you tried making your isValidUsername as public method instead of private.

kurtz_wolfgangkurtz_wolfgang

Thanks Bob. Your suggestion worked.

 

Thanks sRavu, however the isValidUserName is working fine, it's the firstName that's causing the issue.