Use the required
attribute - Quoting the official documentation
required - Boolean A Boolean value that specifies whether this
inputField 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.
Note that if this input field displays a custom object name its value
can be set to nil and will not be required unless you set this
attribute to true. The same does not apply to standard object names,
which are always required regardless of this attribute.
This is by design. The required attribute simply makes the field required for the Ajax request.
In order to have the red mark the element needs to be the child of a pageblockscetion or sectionitem IIRC.
For example, this does display the red mark without any issue:
<apex:page standardcontroller="Account">
<apex:form >
<apex:pageBlock>
<apex:pageblockSection>
<apex:pageblockSectionItem>
<apex:inputField value="{!Account.Name}" required="true"/>
</apex:pageblockSectionItem>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
For other elements you may have try out the info here:
http://itsmersaini.blogspot.com/2014/11/adding-required-red-mark-on-vf-page.html
Enclose your text box in div with class requiredInput and put another
div with class requiredBlock where you want to show Red mark.
Page: testRequiredField
<apex:page controller="AccountCustomController" >
<apex:form >
<apex:pageBlock title="Quick Edit: {!account.Name}">
<apex:pageBlockSection title="Account Details" columns="1">
<div class="requiredInput">
<div class="requiredBlock"></div>
<input type="text" value="Fill your name here" />
</div>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Basically you will have to wrap each inputField in the two divs
I have done it with jQuery / css before like this:
On document Ready:
$('.req_blk').each(function(){
$(a).parent("td").prepend("<div class='requiredBlock'></div>");
$(a).parent("td").contents().wrapAll("<div class='requiredInput'/>");
});
then put the class req_blk on each input field that I wanted to mark required. There may be better ways though......
Using a PageBlockTable
<apex:page standardcontroller="Account" recordSetVar="accts">
<apex:form >
<apex:pageBlock>
<apex:pageBlockTable value="{!accts}" var="a">
<apex:column headerValue="Name" >
<div class="requiredInput">
<div class="requiredBlock"></div>
<apex:inputField value="{!a.Name}" required="true" />
</div>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Best Answer
Salesforce does not add the
required
HTML attribute to your fields when settingrequired="true"
on a component.Setting the component's
required
attribute indicates that Salesforce should validate that the field is required on the server-side. This differs from the HTML version of therequired
attribute which does the validation on the client-side.What you are seeing is the expected behaviour.
If you add an
apex:pageMessages
component to your page and try and call anaction
or anything that will submit your form then you should get an error message if yourapex:inputSecret
is blank.