1

I want to make an inputText field's attribute required="true" on the VF page and display the error message : Error: You must enter a value (Note: I do not want to use an InputField here)

When I click on the submit button, this doesn't show me the inline error message.

Please see the below code, and can some one explain what am I doing wrong here?

<apex:page sidebar="false" showHeader="false" id="page" standardController="Case" extensions="MyControllerExt">
<div>
    <apex:form styleClass="form-horizontal" id="form">
        <div class="requiredInput">
            <div class="requiredBlock"></div>
            <apex:inputText value="{!myCustomVar}" id="LastName" label="LastName" required="true" />
        </div>
        <!-- Submit From -->
        <apex:commandButton id="btn2" styleclass="btn btn-lg btn detail" rerender="form" value="Submit" action="{!submitAction}" />
    </apex:form>
</div>

And this is what the document say about the required true attribute;

required -> Boolean -> A Boolean value that specifies whether this field is a required field. If set to true, the user must specify a value for this field. If not selected, this value defaults to false.

  • Submit a bug to salesforce. I'm not sure if this ever worked in the past, but I was surprised that it doesn't work now. The first hint that something went wrong is that no "required" bit is rendered on the input itself. The validation does block the form submission, and also will sow an erro in apex:pageMessages, but should be rendering on the input itself as well. – sfdcfox Apr 17 '18 at 1:18
  • 1
    You seem to be missing <apex:messages /> in your vf page. @sfdcfox you're right, that it does not render any styling signifying that it's required or not, but observed that to be able to view the message on VF, the message tag is required. – Jayant Das Apr 17 '18 at 1:27
  • 2
    @JayantDas Yes, compare that to using apex:inputField; the error appears directly below the field, the input box gains a red outline. apex:inputText, by contrast, only adds a message with a block. This is a different behavior between each element, and inputText is also not consistent with the salesforce UI. – sfdcfox Apr 17 '18 at 1:32
3

You seem to be missing <apex:messages/> in your vf page. By definition, it says:

All messages that were generated for all components on the current page. If an <apex:message> or <apex:messages> component is not included in a page, most warning and error messages are only shown in the debug log.

I verified this, and once you add the tag, it works fine. However you will still need to address the styling as @sfdcfox mentioned, it does not really render as expected when required=true.

  • + 1 but it still doesn't work "fine", it's broken. Try comparing it to apex:inputField and you'll see the difference. – sfdcfox Apr 17 '18 at 1:45
  • To get the inputField's behaviour I think I need to find a custom styling mechanism. with <apex:message/>, I could see the inline message as "page:detail:form:myCustomVar: Validation Error: Value is required" I want to change this to "Error: You must enter a value" – Hasantha Apr 17 '18 at 3:50
  • @Hasantha, if I am not mistaken, your original question was that the message was not displayed, hope that's resolved. In regards to what you are looking for around custom message, in order to achieve that, you may then not like to use "required" attribute. Instead perform a custom validation in the controller method and add message to be displayed using ApexPages.addmessage(..). – Jayant Das Apr 17 '18 at 4:06
  • @Jayant Das : You are right about my question, and this is not going to full fill my requirement, I'm going to use Apex page messages and do the work-around for the validations. – Hasantha Apr 19 '18 at 3:24

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.