error checking at the controller level making fields required - salesforce

I have a VF page with a lot of fields. the requirement is that all the error messages on the fields need to be listed together at the top of the page and have field level error messages. Currently, we have something like this:
<apex:inputField value = 'f1' />
<apex:inputField value = 'f2' />
When the user clicks submit, at the controller level, I check if either of the fields are blank, and if they are, i return an aggregated error. but i don't know how to render the red required bar since all this is happening at the controller level
Basically, the requirement is
for the required fields have a red bar
if the fields are not filled in, display field level errors
For all the field level errors, display a message at the top of the page saying the user needs to enter in these values

You can use (Required="true") attribute. This attribute is of apex:inputField tag. This attribute will give you field level error. you dont need to do coding in controller.
And for all field level errors, you have to add one VF tag <apex:pagemessages />. this tag will show you all error messages together.

If you want to take it a step further and add errors for business logic rather than just empty fields, take a look at the addError() functionality within APEX code.
http://wiki.developerforce.com/page/An_Introduction_to_Exception_Handling

Related

How to retrieve parent object field values in SOQL to display in VF Page

I am new to SFDC, struggling to retreive parent objects fields through SOQL query on child object.
I have two objects namely Opportunity and Scope where Scope has a lookup to Opportunity.
Here is the query i have written in Controller.
ScopeController.cls:
List<Scope> scopeList=[Select id,Name,ScopeValue__c,Opportunity__c,Opportunity__r.opAmount__c from Scope__c];
System.debug('scopeList : '+scopeList);
Accessing the values of scopeList in below VF Page.
ScopePage:
<apex:repeat value="{!scopeList}" var="s">
<tr>
<td>{!s.Name}</td>
<td>{!s.ScopeValue__c}</td>
<td>{!s.Opportunity__c}</td>
<td>{!s.Opportunity__r.opAmount__c}</td>
</tr>
</apex:repeat>
But in the above VF Page not able to show the value of s.Opportunity__r.opAmount__c, it is empty. I debugged in Controller, the SOQL query itself not retrieving the value of "Opportunity__r.opAmount__c" hence shown empty in VF Page.
One reason could be that your profile does not have access to this field (Opportunity__r.opAmount__c).
Query looks fine to me. Do you see this value on the standard layout. Does your profile have access to this field?
From the management settings for the field’s object, go to the fields area.
Select the field you want to modify.
Click View Field Accessibility.

AngularJS - Form validation triggered on load

I added field validation attributes like "required" and "pattern" in my form, and the form is inside a ng-controller. The validation works. But it seems the validations are triggered on page load, and I see all the fields are marked as invalid with error message when the page load.
I tried to add "novalidation" attribute to the form as indicated in the examples on AngularJS website, but no luck.
I would like to have the validation triggered the first time the user tries to interact with it. How can I do that?
Update
Here's an example https://jsfiddle.net/davidshen84/00t197gx/
<div class="mdl-cell mdl-cell-6-col mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text" id="screenname" pattern="[a-zA-Z0-9]{3,}" ng-model="comment.screenname" required/>
<label class="mdl-textfield__label" for="screenname">Screen Name</label>
</div>
On load, you should see all the input fields had a red line under them which indicate they are in the invalid state. And the line turns to blue once validated.
Note: The style on the check button does not work...should not be a concern in the problem.
Angular is going to check the form the same way at any point (load or later) and render the result. If you don't want to display the results on load, add logic to check whether the form has been interacted with. You can hide your error messages using ng-if="yourFormName.$dirty", or display according to the status of an individual field with yourFormName.yourFieldName.$dirty.
Click here for live demo.
What is currently implemented (wrong IMHO) is that MDL automatically validates input and doesn't mind "novalidate" form attribute. I had to implement check for empty input value (skip validation and remove is-invalid class) and, since angular form validation requires "novalidate" attribute, check:
if (input.form.novalidate = true) // skip validation
that way you can actually turn off mdl validation and leave everything to angular.
One more thing is actually required. You can create angular directive which validates expression and add is-invalid class if necessary:
div class="mdl-textfield" mdl-validator="form.email.$error"

apex:inputField Binding Value not updated

I am working with Apex controller and Visual Force page.
Inside of the vf page I had a data table and each row of that table binds to a value from a list that generated from the controller
example code:
<apex:dataTable value="{!List}" var="item" styleClass="class1" >
...
<apex:column headerValue="Header1">
<apex:outputpanel rendered="{!NOT((a=='true'))}">
<div class='estimate-name-column'>
<apex:inputField value="{!item.Name}" required='true' rendered="{!(a=='false')}"/>
</div>
</apex:outputpanel>
</apex:column>
...
</apex:datatable>
As you can see, I was trying to hide some inputFields base on some conditions.
However, there was a problem. If I do the above, those inputFields who get rendered were not binded correctly. After submited the form with this data table, inside my controller all the records' in list Name are null. Even though I saw the 'Name' was posted in http request.
I am guessing is render interfere with the binding? because if I remove the rerendered conditions and display all InputField I can get the values inside the controller after submitting form
any ideas what happened?
If I recall correctly an apex tag must be present on the page in order to be rerendered.
In other words - something (maybe as simple as <span id="long:generated:salesforce:id"></span>) must be in HTML in order for later AJAX updates to inject new content into the placeholder. If it's not rendered, it will stay not rendered.(1)
Instead of rendered try to move your condition to styleor styleclass attributes. Something like
<apex:inputField value="{!item.Name}"
required="{!a=='false'}"
style="display:{!IF(a=='false','inline', 'none')}"/>
visibility:hidden (if you want them to occupy their space but not be seen) or display:none (to have them appear to be completely not there. See also What is the difference between visibility:hidden and display:none?
Footnote:
(1) unless of course you'll rerender a tag that contains "this" tag (something higher in the XML).

how do we perform validation on a apex:inputfield in a apex:repeat tag in VF page

I have a Vf page which shows a list in a apex:repeat tag. All the fields displayed are apex:inputfield. There are 2 fields
quantity__c
Change__c
I want to validate, if the quantity has changed then i want the change field should
be filled/mandatory. Can i do this validation at VF level?
Any other way of accomplishing this?
Thanks
Update: Here is code i am using as suggested by LaceySnr. I can see the apex message is thrown in the debug log but its not visible in Vfpage
for (integer i=0;i<List_FinalStdItems.size();i++)
{
system.debug('inside loop to check quantity is changed');
ItemSet.add(List_FinalStdItems[i].id);
system.debug('New quantity'+List_FinalStdItems[i].quantity__c +' old quantity'+MapStdItemsOldMap.get(List_FinalStdItems[i].id).quantity__c);
// system.debug('old quantity'+MapStdItemsOldMap.get(List_FinalStdItems[i].id).quantity__c);
if (MapStdItemsOldMap.get(List_FinalStdItems[i].id).quantity__c!=List_FinalStdItems[i].quantity__c)
{
system.debug('This quantity for item '+List_FinalStdItems[i].Name+ ' has changed');
if(List_FinalStdItems[i].Inventory_Change_Reason__c==null || List_FinalStdItems[i].Inventory_Change_Reason__c=='')
{
system.debug('This quantity for item '+List_FinalStdItems[i].Name+ ' has changed and Reason for change is empty');
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Reason for change not entered for changed Quantity'));
error=true;
}
}
}
01:04:55.541 (541902000)|SYSTEM_METHOD_ENTRY|[151]|ApexPages.addMessage(ApexPages.Message)
01:04:55.541 (541985000)|VF_PAGE_MESSAGE|Reason for change not entered for changed Quantity
01:04:55.542 (542008000)|SYSTEM_METHOD_EXIT|[151]|ApexPages.addMessage(ApexPages.Message)
Edit
<apex:outputpanel id="mess">
<apex:pageMessages />
</apex:outputpanel>
.
.
.
<apex:actionFunction name="save" action="{!Save}" rerender="mess"/>
You could do this through javascript directly, or you could use <apex:actionSupport> with the onChange event to refresh the second field, but to be honest I think the simplest and most robust way would be to just do the validation in the controller before saving a record.
Edit
I assume you have an <apex:pageMessages/> tag in the page, make sure you're rerendering it — i.e. if you're not doing a whole page refresh, put the messages tag in an output panel and specify that <apex:outputPanel> in the rerender attribute for your action.
Edit II
Is your action definitely returning a null Pagereference? Otherwise what you have looks correct. Are there any page blocks etc. between your action function and the output panel? If so you may need to drill down through an id hierarchy.

How to hide a section of fields using a checkbox?

How to hide a section of fields using a checkbox in visualforce pages?
Assuming the Salesforce approach (keeping the page weight down etc.), you could do something like the following:
<apex:inputCheckbox value="{!theBool}">
<apex:actionSupport event="onChange" action="{!myAction}" rerender="theFieldsPanel"/>
</apex>
<apex:outputPanel id="theFieldsPanel">
<apex:variable var="v" value="" rendered="{!theBool}">
<apex:inputField value="{!someField"} rendered/>
<!-- more fields etc. -->
</apex:variable>
</apex:outputPanel>
Note that I don't use the rendered attribute on the output panel itself, this is because if it's not rendered then it doesn't exist in the page, and as such, doesn't make for a good rerender target! Now you just require a simple action on the controller (you could do any other logic in here if need be):
public Pagereference myAction()
{
// any logic etc. goes here
return null;
}
The benefit of doing things this way, as opposed to with javascript is that you can ensure that if the fields are hidden then values won't be sent back to the controller for the variables they're bound to. Simply hiding things with javascript would not have the same effect, so say the user typed something in one of the fields and then hid them, whatever he/she typed would still end up in the related controller variables.

Resources