function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JeriMorrisJeriMorris 

inputText with required="true" doesn't generate a required field

I have a page that defines an inputText component with required="true" but when the page renders, the input box doesn't have the red bar indicating that it's required.

My page looks like this:

<apex:page standardController="Contact">
<apex:form >
  <apex:outputLabel value="Birthdate" />
  <apex:inputText value="{!Contact.birthdate}" required="true" />
</apex:form>
</apex:page>

 When the page is displayed, the vertical red "required" bar doesn't precede the input box.

If I add a Save button to the page and click it, the field does act as if a value is required. (Contact.Birthdate is required in my org). The problem is that the UI doesn't display the "required" visual cue.

This is pretty simple stuff. What am I doing wrong?

Jeri


Best Answer chosen by Admin (Salesforce Developers) 
Rajendra ORajendra O
thanks jwetzler for pointing out mistake yes i visited above suggested link and find out more appropriate solution. now my page looks like:-
Code:
<apex:page standardController="Contact">
<apex:form>
<apex:messages/>
<apex:pageBlock title="PageBlock"  mode="edit">
<apex:pageBlockSection title="Page block section">
  <apex:outputLabel value="Birth date" for="mId"/>
  <apex:outputPanel styleClass="requiredInput" layout="block">
   <apex:outputPanel styleClass="requiredBlock" layout="block"/>
  <apex:inputText value="{!contact.Birthdate}" required="true" id="mId"/>
 </apex:outputPanel>
</apex:pageBlockSection>
<apex:pageblockButtons>
<apex:commandButton action="{!save}" value="save"></apex:commandButton>
</apex:pageblockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

 and yes i can use my custom styles or simply copy existing salesforce styles so even if salesforce make changes in underlying look and feel section i will be having working but old looking salesforce page. 
thanks

All Answers

VisualForceVisualForce
Try inputfield instesd of inputtext...
JeriMorrisJeriMorris
Thanks for your reply!

I need inputText instead of inputField because I don't want the date-picker functionality that inputField would give me. In fact, the page really looks more like this:

<apex:page standardController="Contact">
<apex:form >
<apex:outputLabel value="Birthdate" />
<apex:inputText value="{!aValue}" required="true" />
</apex:form>
</apex:page>
and the controller implements getAValue() and setAValue() methods.

My problem is that the inputText component's required attribute doesn't render as required.
Rajendra ORajendra O
In your case setting required attribute to true makes this field required in functional aspect but it doesn't inherit required styles so you need to beautify this element your self. So if you want this field to look like salesforce required element style then you can do this by following manner.

Code:
<apex:page standardController="Contact">
<apex:form >
<apex:messages></apex:messages>

<apex:pageBlock title="PageBlock"  mode="edit">
<apex:pageBlockSection title="Page block section">

  <apex:outputLabel value="Birth date" for="mId"/>
    <apex:define name="requiredInputDiv"><div class="requiredInput">
    <apex:define name="requiredInputDiv"><div class="requiredBlock"/></apex:define>
    <apex:inputText value="{!contact.Birthdate}" required="true" id="mId">
    </apex:inputText>
    </div>
</apex:define>

</apex:pageBlockSection>
<apex:pageblockButtons>

<apex:commandButton action="{!save}" value="save"></apex:commandButton>
</apex:pageblockButtons>
</apex:pageBlock>
  
</apex:form>
</apex:page>

 Hope this will solve your problem and if you got any better solution then don't forget to post.
thanks

JeriMorrisJeriMorris
Thank you! Adding the elements you suggested does display the "required" visual cue.

Any word from Salesforce as to whether this is a bug in the inputText component? If so, if the bug is fixed, I'll wind up with two red vertical bars, right?

Jeri
jwetzlerjwetzler
It is not a bug.  You are not using a salesforce styled component so you are not going to get salesforce styles.  There are many many posts on the forums about this.

Such as this one

jwetzlerjwetzler
Also I am not sure why you are using the apex: define component -- you are using it in an unsupported way (it is for use with templating) and it seems to be a bit of a fluke that it works for you.  I would suggest dropping to a div and getting rid of the define component as I can't guarantee that your page is always going to work.

And as always you use salesforce stylesheets at your own risk.  We can and do change them often.  If you want a similar red bar next to your required fields it might be better to define your own css classes.
Rajendra ORajendra O
thanks jwetzler for pointing out mistake yes i visited above suggested link and find out more appropriate solution. now my page looks like:-
Code:
<apex:page standardController="Contact">
<apex:form>
<apex:messages/>
<apex:pageBlock title="PageBlock"  mode="edit">
<apex:pageBlockSection title="Page block section">
  <apex:outputLabel value="Birth date" for="mId"/>
  <apex:outputPanel styleClass="requiredInput" layout="block">
   <apex:outputPanel styleClass="requiredBlock" layout="block"/>
  <apex:inputText value="{!contact.Birthdate}" required="true" id="mId"/>
 </apex:outputPanel>
</apex:pageBlockSection>
<apex:pageblockButtons>
<apex:commandButton action="{!save}" value="save"></apex:commandButton>
</apex:pageblockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

 and yes i can use my custom styles or simply copy existing salesforce styles so even if salesforce make changes in underlying look and feel section i will be having working but old looking salesforce page. 
thanks
This was selected as the best answer
d.sureshkumar41.33978150573839d.sureshkumar41.33978150573839

  Hi i get the exact red bar and if the user does not enter any data i want to display error like inputfield Plz help me