1

Hi I have following code for visual force and apex controller to render the checkbox. I have marked the checkbox as required="true". When I try to submit the page it give me cryptic error message.

Error Message:

j_id0:SPE: Validation Error: Value is required.

How do I modify this error message?

<apex:selectCheckboxes value="{!RegPeriod}" required="true" id="RegPerdId">
                    <apex:selectOptions value="{!RegistrationPeriod}"/> 
</apex:selectCheckboxes>

Apex Controller:

public List<String> regPeriod = new List<String>();
    public List<String> getRegPeriod(){
        return regPeriod;
    } 
    public void setRegPeriod(List<String> RegPrd){
        regPeriod.addAll(RegPrd);
    }

public List<SelectOption> RegistrationPeriod {
        get {
            List<SelectOption> listOfRegPeriod = new List<SelectOption>();
            for(S_Period__c certiPeriod : listPeriod) {
                listOfRegPeriod.add(new SelectOption( certiPeriod.Id, certiPeriod.Name));
            }
        return listOfRegPeriod;
        }
        set;
    }
  • 1
    Have you tried providing a label? – sfdcfox Jan 19 '15 at 21:39
  • I find it helpful to handle my own required field checking, so that I can respond with a complete error message, without the "j_id0:SPE" that gets added by the built-in validation ... – user735 Jan 19 '15 at 21:58
2

Specify a label attribute for your input elements. Example provided below.

Controller

public class reqchk {
    public reqchk() {
        values = new String[0];
    }
    public String[] values { get; set; }
    public SelectOption[] getOptions() {
        SelectOption[] results = new SelectOption[0];
        for(String item: 'Value A;Value B;Value C;Value D'.split(';')) {
            results.add(new SelectOption(item, item));
        }
        return results;
    }
}

Page

<apex:page controller="reqchk">
    <apex:form>
        <apex:pageMessages />
        <apex:selectCheckboxes label="Value Selections" required="true" value="{!values}">
            <apex:selectOptions value="{!options}" />
        </apex:selectCheckboxes>
        <apex:commandButton value="Submit" />
    </apex:form>
</apex:page>

Note that you won't see the label unless you include the element in a page block, however... but the error message will be correct.

  • Thanks for the reply. :) After updating it with label the error message is: Value Selections: Validation Error: Value is required. Cant we modify whole message? – JavaAster Jan 19 '15 at 21:50
  • 1
    For anything more advanced, you'll have to roll your own with ApexPages.addMessage, and do the validation in controller code. You'll lose the ability to "highlight" the field with a required mark automatically, so you'll also need to add more code to make it look required. Also, consider that the default message is automatically translated (except the label itself) for all languages. And if your label uses $Label, it too can be multilingual. – sfdcfox Jan 19 '15 at 22:03
  • For the time being this seems fine. – JavaAster Jan 19 '15 at 22:05

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.