2

I am trying to achieve email-checking in a form. When the user types in an email address they can click the "Check email" button - which calls the ValidateEmail method in the custom controller (it does successfully) but my system.debug line in the method ALWAYS shows emailid to be null :( When I hardcode an emailid value the SOQL query works fine, and redirects the user to the contact's record page.

Could someone please provide insight to why the apex:inputText element is not binding to the public emailid property I have defined in the custom controller?

Visualforce page code:

<apex:page controller="EnquiryForm2Controller" lightningStylesheets="true">
    <meta name="viewport" content="width=device-width, initial-scale=1"/>    
    <apex:form id="theForm">
            <apex:pageBlockSection title="Contact Details" >
                <apex:inputField value="{!Applicant.FirstName}" required="true"/>
                <apex:inputField value="{!Applicant.LastName}" required="true"/>
                <apex:inputText value="{!emailid}" required="true" />                  
                <apex:commandButton action="{!ValidateEmail}" value="Check email" reRender="theForm" immediate="true"> 
                </apex:commandButton>                    
            </apex:pageBlockSection>
    </apex:form>
</apex:page>

Custom Controller code:


public with sharing class EnquiryForm2Controller {
    public Contact Applicant { get;set; }
    public String emailid {get;set;}
    

    
    
    public EnquiryForm2Controller() {
        Applicant = new Contact();       
    }
    
    public PageReference ValidateEmail(){
        System.debug('Just entered ValidateEmail');
        System.debug('emailid: ' + emailid);
        List<Contact> contactList = [SELECT Email FROM Contact WHERE Email = :emailid];

            if(contactList.size() == 1){
                System.debug('There is a match'); 
                System.debug(contactList[0].Id);
                
        String host = System.URL.getSalesforceBaseURL().toExternalForm();
        String newurl = host + '/lightning/r/Contact/' + contactList[0].Id + '/view';
        PageReference newPage = new PageReference(newurl);
        newPage.setRedirect(true);
        return newpage;
                //redirect to contact page
            }
      return null
}

2 Answers 2

1

Put your button and input field in an apex:actionRegion, and it'll allow you to make the partial update as you like:

<apex:actionRegion renderRegionOnly="false">
  <apex:inputText value="{!emailid}" required="true" />                  
  <apex:commandButton action="{!ValidateEmail}" value="Check email" reRender="theForm" />
</apex:actionRegion>

Action regions restrict the validation to just the contained fields, which is perfect for what you're trying to accomplish.

4
  • Thanks you are a legend! Putting just the command button in action region actually made my input null but I just tried putting the inputtext field in the action region too now and it works perfectly! Marking as correct answer. Nov 11, 2021 at 11:39
  • @JamesSlater Yes, that's what I meant to write. I've updated the answer to reflect that for future visitors. Glad I was able to help you out. Have a great day!
    – sfdcfox
    Nov 11, 2021 at 12:50
  • Hi @sfdcfox if you have a spare minute would you mind taking a look at my newly posted question? You seem to know your stuff! Nov 12, 2021 at 16:27
  • @JamesSlater Sure thing, always glad to take a moment when I can.
    – sfdcfox
    Nov 12, 2021 at 16:32
2

Whoops, realised the immediate="true" attribute on the command button element was discarding the input. I just wanted it to disregard the form validation.

Will leave this question up in case anyone else makes this silly mistake.

If someone could instead answer how I can disregard the form validation, whilst keeping the input - then I would be very grateful.

1
  • 1
    +1 for knowing that someone else might think/do the same thing and can learn from your faux pas, and thus being willing to air your dirty laundry, so to speak.
    – Moonpie
    Nov 10, 2021 at 16:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .