+ Start a Discussion
enrydevenrydev 

Visual force error message in a new page

I'm creating a custom VF page for the registration of new users.

Page: simple form to get the user's email.

Controller:

 

public with sharing class SiteRegisterController {

    public SiteRegisterController () {
    }


public String username {get; set;}
public String email {get; set;}
public String password {get; set;}

    public PageReference registerUser() {
       user u = [select id,username from User where username = :email];
           if (u!=null) {
                  //ADD HERE CODE FOR ERROR MESSAGE IN THE PAGE FORGOTPASSWORD
                  PageReference page = System.Page.ForgotPassword;
                page.setRedirect(true);
                return page;
            }
            else {
                PageReference page = new PageReference('http://registration.com/');
                page.setRedirect(true);
                return page;
            }

        return null;
    }
}

 If the user is already registered i would like also add a message "You are already registered, Did you forget your password?" on the new page System.Page.ForgotPassword;. I know how to add an eror message in the current page

 

 

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.');
ApexPages.addMessage(myMsg); 

 

but is it also possible add it in a new page(forgotpassword) from the controller of the current page? Of course in the page forgot password there is a tag 

<apex:pageMessages id="error"/>

 

 

 

Thanks in advantage for any advice.

BR.

 

 

 

 

 

vbsvbs
entydev - Why not pass pass a query parameter for e.g. hasError=true before calling the Page ForgotPassword. The first thing that you do in the constructor for the controller on this page is ApexPAges.addMessage if hasError = true. This should be an easy change to implement
Gdev20000Gdev20000
Thanks a lot for your support,great idea! How can i send the parameter has error ? Now i haven t an URL,just the page 's name.
vbsvbs

There are different ways to do this. Take your pick from below:

PageReference page = Page.ForgotPassword;
page.getParameters().put('hasError', true);

 

PageReference page = new PageReference('/apex/ForgotPassword?hasError=true');

Both of these should work. Just try and let us know if this works. Please mark this as a solution so that it helps others. 

 

EnryEnry

Thanks a lot, this works:

PageReference page = System.Page.ForgotPassword;
page.getParameters().put('hasError','true');

 page 2:

 string errormessage=Apexpages.currentpage().getparameters().get('hasError');
    
       if(errormessage=='true'){
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'error message');
            ApexPages.addMessage(msg);
         
          }