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
erikdozsaerikdozsa 

Bypass required field on Visualforce page

Dear all,

I would like to ask for your suggestions regrarding the following situation:
I have to create a VF page to record expense line items. Since we don't want to add the rocords one by one the idea is to create some empty rows that the user can fill. We also need some required fields. I would like to have the fields required only if the user started to populate the row but for a completely empty row I would avoid the the error messages and would let the save complete:

Before Save

And this is what happens after save:
On Save

How can I avoid this error popping up for a completely empty row? Any idea is more than welcome.
 
<apex:column id="col1">
		<apex:facet name="header" >{!$ObjectType.Expenses_Line_Item__c.fields.SP_Expense_Category__c.label}</apex:facet>
		<apex:inputField value="{!item.SP_Expense_Category__c}" required="true" />	 
	</apex:column> 
	<apex:column id="col2">
		<apex:facet name="header" >{!$ObjectType.Expenses_Line_Item__c.fields.SP_Expense_Date__c.label}</apex:facet> 
		<apex:inputField value="{!item.SP_Expense_Date__c}" required="true"/>
	</apex:column>
	<apex:column id="col3">
		<apex:facet name="header" >{!$ObjectType.Expenses_Line_Item__c.fields.Value__c.label}</apex:facet>
		<apex:inputField value="{!item.Value__c}" required="true" />	 
	</apex:column>

Thank you in advance!

Erik








 
Best Answer chosen by erikdozsa
James LoghryJames Loghry
You simply can't do much complex stuff with the required attribute in VF like you're attempting to.

In addition to Scott's suggestion, the next best way would be to implement custom requiredness checking in an Apex controller or extension.  In that case, for each row, you could check the three values, and  if one or more fields are populated, but another one is not, then add an error to that field. (More info: https://www.salesforce.com/us/developer/docs/dbcom_apex290/Content/apex_System_SObject_field_addError.htm)

All Answers

Scott McFarlaneScott McFarlane
If I were you I would enforce those rules by validation, instead of making the fields mandatory. Pseudocode:

IF(OR(expensedate1<>"",expenseitem1<>""),TRUE,FALSE)
James LoghryJames Loghry
You simply can't do much complex stuff with the required attribute in VF like you're attempting to.

In addition to Scott's suggestion, the next best way would be to implement custom requiredness checking in an Apex controller or extension.  In that case, for each row, you could check the three values, and  if one or more fields are populated, but another one is not, then add an error to that field. (More info: https://www.salesforce.com/us/developer/docs/dbcom_apex290/Content/apex_System_SObject_field_addError.htm)
This was selected as the best answer
erikdozsaerikdozsa

Thank you guys. I went for the "addError" version.
In the meantime I have found another nice feature to mark fields like they are required: http://salesforce.stackexchange.com/questions/5462/required-field-mark-red-vertical-bar-not-coming
So I mark them and also validate the missing values during save.

Cheers,