21

I have a parametrized helper function in my controller, which formats text according to requirements.

public static string getFormatMe(string a) {
    // do manipulation here
    return a;
}

How do I call this from Visualforce, to format my data?

This does not work:

<apex:outputText value="{!formatMe('test')}" />

Save error: Unknown function formatMe. Check spelling.

Here is a solution to a similar question, but that is for calling a function without parameters: How can I call a method from visualforce?

  • How about storing your string in a variable and calling the getter function to work on that parameter? – ipavlic May 16 '13 at 10:18
  • @ipavlic, thank you for your time and comment. I would not use variables because of the sheer number of the SOQL fields which need formatting. – Shumon Saha May 16 '13 at 12:35
  • Perhaps a List which holds your fields? – ipavlic May 16 '13 at 13:20
18

You cannot pass parameters to method bindings made in your Visualforce page to your controller, as per the help topic here, you can only provide 'action', 'get' and 'set' methods.

In order to implement your requirement I would recommend you consider looking at Visualforce Components, such that you can have something like

 <c:myOutputText myValue="test"/>
 <c:myOutputText myValue="{!MyObject__c.MyFieldToFormat__c}"/>

This component would internally use the apex:outputText component as above, however its controller would have been passed the 'test' value as an attribute to the component. On the controller you would have a getFormattedValue to output the formatted version.

<apex:component controller="MyOutputTextController">
  <apex:attribute name="myValue" type="String" description="Value to format" assignTo="{!myValueAttr}"/>
  <apex:outputText value="{!formattedValue}"/>
</apex:component>
  • @SumanKrishnaSaha your very welcome! :) – Andrew Fawcett May 16 '13 at 16:56
  • 2
    I don´t understand your response Andrew Fawcett. where do you specify to which function you use the attribute whose name is my value? and how do you get the returned string at the end? Thanks for your help. – user20917 Jun 1 '15 at 11:14
4

It wouldnt be possible to do this - you can only invoke getters or action methods.

A workaround would possible be to have javascript (or an actionFunction) invoked, which can invoke controller methods via js-remoting and pass in parameters, you would then have to use javascript to replace text on the VF Page.

As an aside, if the text to be displayed is known when the page renders, you could just include the formatting in your getter, so that your getter returns formatted text ? The visualforce doesn't have to be aware of the need for formatting?

1

Visualforce:

<apex:commandButton value="Del" action="{!delCont}" rerender="all">
    <apex:param name="contIdParam" value="{!cont.id}" assignTo="{!contIdChosen}"/>
</apex:commandButton>

apex:

public String contIdChosen {get; set;}
public PageReference delCont() {
    Contact todel = new Contact(id = contIdChosen);
    delete todel;
    return null;
}

You can find more information at this article.

Also look at the docs about apex:param

0

Let's assume you need to call this action and at the same time you want to pass some variable. Just like you are trying to achieve in your question.

Class:
public void myMethod(){
//Fetch the value of parameter you are sending from the page,
String check = Apexpages.currentPage().getParameters().get('paramValue');
}

VF:
//Pass the value using apex:param and use action attribute to call the controller method.
//The value attribute accepts expression that can be evaluated to generate desired results. I am passing a simple string here.
<apex:commandLink action="{!myMethod}" value="Call my method">
<apex:param name="paramValue" value="testing"></apex:param>
</apex:commandLink>

Instead of creating a method with parameters, you can create a simple method (without parameter) and pass variable using apex:param. There are other methods too that involve the use of apex properties or getter and setters. However, this is a simple example to show how we can pass values to a method from VF page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.