2

I have a custom label that prints 'Enter at least 2 characters to search.'

I would like to make the 2 a parameter that gets passed in so that the user can adjust it at their discretion. So the label would be 'Enter at least {0} characters to search.'

My issue is that this is being called from javascript so I don't have access to apex:param. Is there a workaround for this?

Here is the javascript that is inline on a VF page

window.$Label = window.$Label || {};
$Label.Alert_message_for_search_function = '{!JSENCODE($Label.Alert_message_for_search_function)}';

function validate(){
    var input = document.getElementById('{!$Component.EntireRelationshipWidget.headerForWidget.searchAndClearButtons.searchBarForm.searchBar}').value;
    if(input.length < 2){
        alert($Label.Alert_message_for_search_function);
        return false;
    }
    searchFunction();
    return true;
}

Essentially I have a search bar that I want to impose a minimum character limit on. The javascript is called via commandButton

<apex:commandButton value="{!$Label.Search}" rerender="EntireRelationshipWidget" status="statusId" onclick="return validate();"/>
  • So, where do you call this javascript function? I don't understand why don't you simply use something like {!param} where 'param' is property of controller of the page (or controller extension) – Patlatus Jul 17 '14 at 13:06
  • I am calling it via a commandButton. I have edited the post to reflect. Also how would I use the {!param} from a javascript method and passing it to the label for an alert? – Louis Pujol Jul 17 '14 at 13:13
  • So what do you have already tried? Did you try to declare a property in controller and try to retrieve its values through {!param} in your javascript function? – Patlatus Jul 17 '14 at 13:15
  • I have not been able to try anything because since this is a Label I'm not certain at all on how to pass params to it without an apex:param. however, since this is javascript I don't have that at my disposal – Louis Pujol Jul 17 '14 at 13:16
  • Look at using javascript remoting – PepeFloyd Jul 17 '14 at 13:17
2

I ended up just adding a few lines to the javascript to manipulate the label. So the label says "Enter at least {0} characters to search." in the value section.

Then on the page javascript I used the .replace() method for strings to replace the {0} with the minLength that the user wants.

function validate(){
    var input = document.getElementById('{!$Component.EntireRelationshipWidget.headerForWidget.searchAndClearButtons.searchBarForm.searchBar}').value;
    var minLength = 3;
    if(input.length < minLength){
        var stringToPrint = $Label.Alert_message_for_search_function.replace('{0}', minLength.toString());
        alert(stringToPrint);
        return false;
    }
    searchFunction();
    return true;
}
0

Just declare a property in controller.

If you have "YourCustomPage" and in its header you declare a controller for it

<apex:page controller="YourCustomPageController">

Then, in controller declare following

public class YourCustomPageController{
string goal, label;

public setGoal(string n){
   this.goal = n;
}

public getGoal(){
   return this.goal;
}

public setLabel(string n){
   this.label= n;
}

public getLabel(){
   return this.label;
}
public  YourCustomPageController{
    goal = '2';
    label = String.format('Alert_message_for_{0}search_function', new string[] {goal});
}
}

Then, in your page just use

function validate(){
    var input = document.getElementById('{!$Component.EntireRelationshipWidget.headerForWidget.searchAndClearButtons.searchBarForm.searchBar}').value;
    if(input.length < {!goal}){
        alert('{!label}');
        return false;
    }
    searchFunction();
    return true;
}
  • He would like the label to match the logic too. – Keith C Jul 17 '14 at 13:25
  • Yes I am wanting to pass the parameter to the custom label – Louis Pujol Jul 17 '14 at 13:26
  • Ok, I have updated my code to include passing parameter to label using String.format function. – Patlatus Jul 17 '14 at 13:48

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.