1

I have created a dummy object 'Console' with 2 required fields (type and year) and a VisualForce page in which I set them using a select list. This is the current scenario:

  1. Set type to 'PlayStation' leave year as 'None' then click save. I get an expected validation error (year is required).
  2. Set type to 'None' and set year to '2001' then click save. I get an expected validation error (type is required). However, the selectlist for 'Type' is set to the previous value 'PlayStation' which is inconsistent with the error message.

How can I retain the value of 'None' which the user selected for consistency?

VisualForce:

<apex:page standardController="Console__c" extensions="ConsoleNewController" sideBar="true">
    <apex:pageMessages id="pageMessages"/>

    <apex:form>
        <apex:pageBlock mode="maindetail">
            <apex:pageBlockSection columns="1">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="{!$ObjectType.Console__c.fields.Type__c.Label}" for="consoleTypeSelectList"/>
                    <apex:selectList value="{!Console__c.Type__c}" size="1" required="true" id="consoleTypeSelectList">
                        <apex:selectOptions value="{!consoleTypes}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="{!$ObjectType.Console__c.fields.Year__c.Label}" for="consoleYearSelectList"/>
                    <apex:selectList value="{!Console__c.Year__c}" size="1" required="true" id="consoleYearSelectList">
                        <apex:selectOptions value="{!consoleYears}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:commandButton value="Save" action="{!save}" status="status" id="saveButton"/>
        <apex:commandButton value="Cancel" action="!cancel" id="cancelButton"/>
    </apex:form>
</apex:page>

Apex Controller:

public with sharing class ConsoleNewController
{
    public ApexPages.StandardController standardController;
    private Console__c console;
    private List<SelectOption> consoleTypes { get; private set; }
    private List<SelectOption> consoleYears { get; private set; }

    public ConsoleNewController(ApexPages.StandardController controller)
    {
        this.standardController = controller;
        this.console = (Console__c) controller.getRecord();
        this.consoleTypes = getConsoleTypes();
        this.consoleYears = getConsoleYears();
    }

    public List<SelectOption> getConsoleTypes()
    {
        List<String> types = new List<String> {'PlayStation', 'Xbox', 'Nintendo', 'Atari', 'Sega'};
        List<SelectOption> typelist = new List<SelectOption>();
        typelist.add(new SelectOption('', 'None'));

        for (String type : types)
        {
            typelist.add(new SelectOption(type, type));
        }

        return typelist;
    }

    public List<SelectOption> getConsoleYears()
    {
        List<SelectOption> yearList = new List<SelectOption>();
        yearList.add(new SelectOption('', 'None'));

        for (Integer i = 2001; i < 2018; i++)
        {
            String value = String.valueOf(i);
            yearList.add(new SelectOption(value, value));
        }
        return yearList;
    }
}
4
+50

Currently according to your code, during validation error, page is not properly rendering.

To retain the values during validation error, you need to add rerender ="pageMessages" on Save button. That's it!

<apex:commandButton value="Save" 
            action="{!save}" 
            status="status" 
            id="saveButton" 
            rerender ="pageMessages"/>

Result:

  1. Initially, Type was 'PlayStation' and Year was 'None', Validation error properly throwing.

  2. After that, change Type to 'None' and Year to '2003'. Validation error and Type is retaining to 'None'. It will not show 'PlayStation' which you were facing the issues.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.