I'm using an inputField that is binded directly to a custom objects field in the controller.
The following will generate a dropdown list with a label.
<apex:inputField value="{!Agency_Profile.Location_Principal_Activity__c}" />
My problem is that I need to add the required mark next to the inputField without losing the label or having default error msgs.
when I used
<apex:inputField value="{!Agency_Profile.Location_Principal_Activity__c}" required="true"/>
I got the required mark but I lost my custom error msgs for validation.
when I used
<apex:outputPanel styleClass="requiredInput" layout="block">
<apex:outputPanel styleClass="requiredBlock" layout="block"/>
<apex:inputField value="{!Agency_Profile.Location_Principal_Activity__c}" />
</apex:outputPanel>
the labels near the dropdown list didnt show anymore..
Is there a way I can accomplish what I need?
I ended up using this.
//this part to add the missing label.
<apex:outputLabel styleclass="labelCol" value="{!$ObjectType.Agency_Profile__c.fields.Location_Principal_Activity__c.Label}" />
<apex:outputPanel styleClass="requiredInput" layout="block">
<apex:outputPanel styleClass="requiredBlock" layout="block"/>
<apex:inputField value="{!Agency_Profile.Location_Principal_Activity__c}" />
</apex:outputPanel>
The best way is to add validation rule for this field for this object.
using raym0nds approach, this is how it looks for a custom controller variable, in my case with the name from a custom field of an object.:
//this part to add the missing label.
<apex:outputLabel for="myId" styleclass="labelCol" value="{!$ObjectType.Agency_Profile__c.fields.Location_Principal_Activity__c.Label}" />
<apex:outputPanel styleClass="requiredInput" layout="block">
<apex:outputPanel styleClass="requiredBlock" layout="block"/>
<apex:inputText id="myId" required="true" value="{!myCustomField}" label="{!$ObjectType.Agency_Profile__c.fields.Location_Principal_Activity__c.Label}" />
</apex:outputPanel>
note the apex:inputText type which now has an label, id and required attribute. The apex:outputLabel now has a for attribute. The for/id is just so clicking on the label will put the cursor into the right field. The required enables form validation, because the rest is just make-up. The label adds a good field name to that validation error - otherwise it would show the internal field id there.
the whole approach is interesting if you have a mass edit table in which all records share certain values (e.g. add multiple leads for the same company)
Related
I have a field set in SF that I am using in a Visual Force page and the formatting of the field labels is not cooperating exactly.
Here is my code.
<apex:pageblocksection columns="1" title="[...]" collapsible="false">
<apex:repeat value="{!fields2}" var="c">
<apex:inputfield value="{!ghostacc[c.fieldPath]}" required="true" style="white-space: nowrap; position: relative;"/>
</apex:repeat>
</apex:pageblocksection>
The result is a label for the input field that allows for word wrap. Is there something that I am missing?
Thank you!
style attribute on <apex:inputField> applies to the input itself, not the label next to it.
Try explicitly specifying the label and the input, something like this?
<apex:pageBlockSectionItem>
<apex:outputLabel value="{!c.label}" style="nowrap magic goes here">
<apex:inputfield value="{!ghostacc[c.fieldPath]}" required="true" />
</apex:pageBlockSectionItem>
I am new to salesforce environment and I am trying to achieve simple thing. I want to create new Lead screen with custom lead questions and save that as a Lead.
I created apex form, overridden new lead button page, but when I press save button on that page, I dont get any error but it also doesnt save the Lead. Cancel button seems to be working.
Do I need to write custom code for save or apex:pageblockbuttons default action should work ?
I will just show short snippet of the code, as most of it is just setting the input fields
<apex:form id="mySection">
<apex:pageBlock title="New Lead: {!Lead.Name}">
<apex:pageBlockSection title="Lead information" columns="1" >
<apex:inputField value="{!Lead.FirstName}" tabOrderHint="1"/>
<apex:inputField value="{!Lead.LastName}" tabOrderHint="2"/>
<!-- Other fields are skipped -->
<apex:inputField value="{!Lead.Project_Value__c}" tabOrderHint="3"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
I found the solution, which was not related to the apex itself, but more to a one custom field which was mandatory but not on the page. I decided to leave the answer here, because while I was trying to find the solution to this issue, I discovered command which actually shows why the input form failed.
<apex:pageMessages />
Maybe that is a common knowledge to Visualforce, apex users, but for me it was a helpful tool and thats why I decided to leave it in an answer, if someone will have similar issue to remember to add that line.
I'm trying to display the grandparent of a custom Salesforce Object in a pageblocktable in a Visualforce page.
I can display the parent as expected ie:
<apex:column headerValue="Related Item Parent">
<apex:outputField id="ItemParent" value="{!item.Parent__c}" />
</apex:column>
works great and gives me a nice table with a 'Related Item Parent' column that is linked to the Item parent exactly as I wanted.
but when I try to go up a further level, the Visualforce Page editor tells me
Could not resolve the entity from value binding
'{!item.Parent_c.Parent_c}'. can only be used
with SObject fields.
Is there anyway that this can be made to work? I'm a bit stuck!!
Below is the complete listing for my pageBlockTable , which works correctly if the Related Item Grandparent column is removed.
<apex:pageBlockTable id="RelatedItems" value="{!contact.RelatedItems__r}" var="item">
<apex:column headerValue="Related Item Name">
<apex:outputField id="ItemName" value="{!item.Name}" />
</apex:column>
<apex:column headerValue="Related Item Parent">
<apex:outputField id="ItemParent" value="{!item.Parent__c}" />
</apex:column>
<apex:column headerValue="Related Item Grandparent">
<apex:outputField id="ItemGrandParent" value="{!item.Parent__c.Parent__c}" />
</apex:column>
</apex:pageBlockTable>
Really appreciate any help that anybody can provide.
Best regards
Pete
When referencing fields of relationship objects, you will want to use the __r suffix. For example, you could use item.Parent__c to refer to the Id of the parent record, but accessing any of the fields on that record would require something like item.Parent__r.Name.
In this instance, you're looking for {!item.Parent__r.Parent__c} (which will give you the Id of the grandparent), or even {!item.Parent__r.Parent__r.Name} (if you wish to access fields on the grandparent record).
I have an object with a lookup field, and want to use InlineEditSupport on that field within a VisualForce page.
However the apex:outputField tag renders the lookup field as a hyperlink, which overrides the inlineEditSupport logic.
For example
<apex:page standardController="Contact">
<apex:outputField value="{!Contact.LastName}">
<apex:inlineEditSupport event="ondblclick"/>
</apex:outputField>
<apex:outputField value="{!Contact.AccountId}">
<apex:inlineEditSupport event="ondblclick"/>
</apex:outputField>
</apex:page>
In this example, the LastName field would be rendered by the apex:outputField tag, and convert to an editable field when double clicked.
However the 'Account' field renders as a hyperlink to the Account record itself, which takes action before the click event can fire.
I can make this work by using an alternate javascript event - i.e. mouseover, however that's not particularly user friendly. I need to maintain a consistent user experience and use a double click action.
Is there any way to prevent the Account field from rendering as a link, or a way of inserting an 'edit' icon next to it somehow?
I ran into the same problem with inline editing in a pageBlockTable format. We felt this was not user friendly.
I used JavaScript (jQuery) to convert the link into plain-text and make this behave like other inlineEditable fields:
/// Overwrite default functionality for lookup columns (links to object page)
/// Change to text-only of name to prevent navigation away
$('TABLE[id$=checklistTable] TBODY TR TD A[id^=lookup]').each(function() {
var text = $(this).html();
$(this).parent().html(text);
});
This will convert all SalesForce lookup fields within the matching jQuery selector. It is also dependent upon the resulting output HTML creating anchor links that have ID's starting with "lookup" (meaning SalesForce could change/break this with future updates).
This might not be the simplest way but what I've done before is to create a select list of the names of the records in the lookup. I think this is nicer for the user anyway than a salesforce lookup box (unless there are hundreds, I suppose). What you would need to do is create a list of select options in the controller, and assign them to some variable like theList, and then reference {!theList} where you want to put it (roughly; see documentation). The select options would need to have the record name as label and the record id as value. Then you can assign the Id directly to the lookup field.
Did you try with salesforce doc?
<apex:page standardController="Contact">
<apex:form >
<apex:pageBlock mode="inlineEdit">
<apex:pageBlockButtons >
<apex:commandButton action="{!edit}" id="editButton" value="Edit"/>
<apex:commandButton action="{!save}" id="saveButton" value="Save"/>
<apex:commandButton onclick="resetInlineEdit()" id="cancelButton" value="Cancel"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:outputField value="{!contact.lastname}">
<apex:inlineEditSupport showOnEdit="saveButton, cancelButton" hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
</apex:outputField>
<apex:outputField value="{!contact.accountId}"/>
<apex:outputField value="{!contact.phone}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
I have a requirement in which a Text field has to be made editable or rendered when a check box is checked, how can I achieve this?
Here's a strictly Visualforce code sample that will work as well:
<apex:pageBlock id="theBlock">
<apex:inputCheckbox value="{!theSObject.CheckBox__c}">
<apex:actionSupport event="onchange" rerender="theBlock"/>
</apex:inputCheckbox>
<apex:inputText value="{!theSObject.TextField__c}" disabled="false" rendered="{!(theSObject.CheckBox__c == true)}"/>
<apex:inputText value="{!theSObject.TextField__c}" disabled="true" rendered="{!(theSObject.CheckBox__c != true)}"/>
<!-- Or to simply have a field that appears only when the box is checked -->
<apex:inputText value="{!theSObject.TextField__c}" rendered="{!(theSObject.CheckBox__c == true)}"/>
</apex:pageBlock>
In addition to this, you can add an action in the action support if you wish to do further processing in Apex, like this:
<apex:actionSupport event="onchange" action="{!myMethod}" rerender="theBlock"/>
Hope that helps
You can use javascript to toggle the disabled attribute of the text input element. Below is a sample page showing how, note, there are a few oddities in here.
First, if you disable the field initially using disabled="true" on the <apex:inputText> then even if you enable it, values entered will not be sent back to the controller, hence disabling the field on load with javascript. I believe this is to prevent any chance of updating a field's value when the developer has specified that it should be disabled.
The second odd point is that even though you set element.disabled to "disabeld" to disable an element, to check whether it is disabled you have to treat it as a boolean value!
<apex:page standardController="Contact">
<apex:form >
<script type="text/javascript">
function ToggleInput(theId)
{
var e = document.getElementById(theId);
if(e != null)
{
e.disabled = (e.disabled ? false : "disabled");
}
}
window.onload = function () { document.getElementById('{!$Component.textInput}').disabled = "disabled"; }
</script>
<apex:inputCheckbox onchange="ToggleInput('{!$Component.textInput}');" value="{!Contact.Some_Checkbox__c}"/>
<apex:inputText id="textInput" value="{!Contact.Some_Text_Field__c}"/>
<apex:commandButton action="{!Save}" value="Update"/>
</apex:form>
</apex:page>
To do all this without javascript, I imagine you'd set the <apex:inputText>'s disabled attribute based on the value of the checkbox field, then use an <apex:actionSupport> tag to fire an action when the checkbox changes and specify the id of the <apex:inputText> in the rerender attribute. You'd also have to wrap it in an <apex:actionRegion> to prevent other fields being sent and causing validation errors if required fields aren't filled etc.. This also means you have to wait for a request to change the state of the text field, so the javascript is the best route for speed.