I have a visualforce page for a custom object MyActivity__c. MyActivity__c has two child objects, Actions and Issues. I am using wrapper classes to add/ create new records for the 2 child objects on the page. Each action and issue added has a custom field 'Description__c' that must be filled out before saving. (see below image)
Rather than having the standard "Error: You must enter a value" message displayed by salesforce, I would like make my own custom error messages. My PageReference save looks like:
public PageReference save()
{
List<Issue__c> issues=new List<Issue__c>();
for (IssueKeyWrapper wrapper : issueWrappers)
{
issues.add(wrapper.is);
}
upsert issues;
List<Action__c> actions=new List<Action__c>();
for (ActionKeyWrapper wrapper : actionWrappers)
{
actions.add(wrapper.ac);
}
upsert actions;
delete issuesToDelete;
delete actionsToDelete;
return stdCtrl.save();
}
and my wrapper looks like:
public with sharing class IssueKeyWrapper
{
public Integer key {get; set;}
public Issue__c is {get; set;}
public IssueKeyWrapper(Integer inKey, Issue__c inCs)
{
is=inCs;
key=inKey;
}
}
My VF wrapper code looks like
<table class="list" border="1" cellpadding="0" cellspacing="0" style="display: {!IF(issueWrappers.size=0, 'none', 'table')};">
<tr class="headerRow">
<th colspan="2" style="width:50%">Issue Description</th>
<th style="width:5%"></th>
<th style="width:10%"></th>
</tr>
<apex:repeat value="{!issueWrappers}" var="issueWrap">
<tr class="dataRow">
<td colspan="2" >
<apex:inputField style="width:80%" value="{!issueWrap.is.Description__c}" styleClass="masterClass commentBox2"/>
</td>
<td >
</td>
<td >
<apex:commandButton value="Delete" action="{!deleteIssue}" rerender="tableTwo">
<apex:param name="issueToDel" value="CT{!issueWrap.key}" assignTo="{!issueToDel}"/>
</apex:commandButton>
</td>
</tr>
</apex:repeat>
</table>
<apex:commandButton value="Add Issue" action="{!addIssue}" rerender="tableTwo" immediate="true" />
In the PageReference save method I want to put some logic such as`
PageReference result = null; ..... return result;
and in the wrapper I'd like to add the logic `
if(String.isBlank(is.Description__c))
{
is.Description__c.addError('Hey, don't forget to add a description');
}
else {result = stdCtrl.save();}
Obviously this doesn't work, or I wouldn't be here :-) So I guess my question is, how do I correctly declare the result variable in the wrapper class, and then how do I connect that to the PageReference save method in my custom controller class? Or is there a better way to do this?
UPDATE: I found a piece of code from the VF cookbook that I'm trying to use in my code. Their example is for the contact object. Is there a way I can do this?
public PageReference save()
{
PageReference result=null;
Contact cont=(Contact) stdCtrl.getRecord();
if ( (String.IsBlank(cont.Email)) )
{
cont.email.addError('Please enter an email address');
}
else
{
result=stdCtrl.save();
}
return result;
}
Where I'm stuck is with the else { result=stdCtrl.save();}
. How can I put that in the wrapper and have it be referenced in the PageReference method?
<apex:inputField>
for the description?