Регистрация  |  Вход

Вопрос от новичка

Не могу вернуться на предыдущую страницу при помощи кнопки cancel (выпадает ошибка что не заполнено Last Name и Email), хотя при помощи кнопки save (при заполненных полях) удаётся. Как исправить эту ошибку? Вот код:
VF Page -
<apex:page Controller="NewContactController">
<apex:form >
<apex:pageMessages />
<apex:pageBlock title="New Contact">
<apex:pageBlockSection >
<apex:inputText label="First Name" value="{!firstName}"/>
<apex:inputText label="Last Name" value="{!lastName}" required="true"/>
<apex:inputText label="Email" value="{!email}" required="true"/>
<apex:SelectList label="Contact Level" size="1" value="{!contactLevel}">
<apex:selectOptions value="{!contactLevels}" />
</apex:SelectList>
<apex:SelectList label="Account" size="1" value="{!accountId}">
<apex:selectOptions value="{!accounts}" />
</apex:SelectList>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Controller -

public class NewContactController {
public String firstName {get; set;}
public String lastName {get; set;}
public String email {get; set;}
public String contactLevel {get; set;}
public String accountId {get; set;}
public Contact newContact {get; set;}
public List<SelectOption> contactLevels{get; set;}
public List<SelectOption> accounts{get; set;}
public List<Account> accountList {get; set;}

public NewContactController() {
contactLevels = new List<SelectOption>();
contactLevels.add(new SelectOption('Primary','Primary'));
contactLevels.add(new SelectOption('Secondary','Secondary'));
contactLevels.add(new SelectOption('Tertiary','Tertiary'));

accountList = [SELECT Id, Name FROM Account];
accounts = new List<SelectOption>();
for(Account item: accountList) {
accounts.add(new SelectOption(item.Id, item.Name));
}

}


public Pagereference save() {
try{
newContact = new Contact(FirstName = firstName, LastName = lastName, Email = email,
Contact_Level__c = contactLevel, AccountId = accountId );
insert newContact;
return redirect();
}catch(DMLException e){
return null;
}
}

public Pagereference cancel() {
return redirect();

}

public Pagereference redirect(){
PageReference pr = new PageReference('/apex/ContactManager');
pr.setRedirect(true);
return pr;
}


}

upd: ошибку исправил.
Не могу вернуться на предыдущую страницу при помощи кнопки cancel (выпадает ошибка что не заполнено Last Name и Email), хотя при помощи кнопки save (при заполненных полях) удаётся. Как исправить эту ошибку? Вот код: 
VF Page -
<apex:page Controller="NewContactController">
    <apex:form >
       <apex:pageMessages /> 
    	<apex:pageBlock title="New Contact">
            <apex:pageBlockSection >               
                <apex:inputText label="First Name" value="{!firstName}"/>                
                <apex:inputText label="Last Name" value="{!lastName}" required="true"/>               
                <apex:inputText label="Email" value="{!email}" required="true"/>                
                <apex:SelectList label="Contact Level"  size="1" value="{!contactLevel}">
                	<apex:selectOptions value="{!contactLevels}" />
                </apex:SelectList>                
                <apex:SelectList label="Account"  size="1" value="{!accountId}">
                	<apex:selectOptions value="{!accounts}" />
                </apex:SelectList>
            </apex:pageBlockSection> 
            <apex:pageBlockButtons location="bottom">
     			<apex:commandButton action="{!save}" value="Save"/>                 
        		<apex:commandButton action="{!cancel}" value="Cancel"/>                               
     		</apex:pageBlockButtons>
    	</apex:pageBlock>
    </apex:form>
</apex:page>

Controller - 

public class NewContactController {
    public String firstName {get; set;}
    public String lastName {get; set;}
    public String email {get; set;}
    public String contactLevel {get; set;}
    public String accountId {get; set;}
    public Contact newContact {get; set;}
    public List<SelectOption> contactLevels{get; set;}
    public List<SelectOption> accounts{get; set;}
    public List<Account> accountList {get; set;}
        
   public NewContactController() {
        contactLevels = new List<SelectOption>();
        contactLevels.add(new SelectOption('Primary','Primary'));
        contactLevels.add(new SelectOption('Secondary','Secondary'));
        contactLevels.add(new SelectOption('Tertiary','Tertiary'));
        
        accountList = [SELECT Id, Name FROM Account];
        accounts = new List<SelectOption>();
        for(Account item: accountList) {
            accounts.add(new SelectOption(item.Id, item.Name));
        }
            
    } 
    
    
    public Pagereference save() {
        try{
            newContact = new Contact(FirstName = firstName, LastName = lastName, Email = email,
                                    Contact_Level__c = contactLevel, AccountId = accountId );
        	insert newContact; 
        return redirect();
        }catch(DMLException e){
           return null;
        } 
    }
    
    public Pagereference cancel() {
        return redirect();
        
    }

    public Pagereference redirect(){
        PageReference pr = new PageReference('/apex/ContactManager');
        pr.setRedirect(true);
        return pr;
    }
    

}  

upd: ошибку исправил.