function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SlanganSlangan 

How to put line-breaks in a VF input text field?

Hey everyone,

 

I am hoping someone here with more knowledge than me may be able to offer some help/guidance/input. Anything is really appreciated.

 

I have some input fields on a VF page and I want the text to save and display on the main page (the display page) the same as the user types it in the input field (including line-breaks). Is there any documentation that explains how to do this?

 

Thanks again,

 

Shannon

Best Answer chosen by Admin (Salesforce Developers) 
Sam.arjSam.arj

sorry my bad:

 

public class myController {
Opportunity opp;

public myController(ApexPages.StandardController controller) {
opp = (Opportunity)controller.getRecord();
// you may also need this since standard controller will not load all the fields
opp = [select id, name,Tender_Name_or_Reason_for_Quotation__c from opportunity where id = :opp.id];
}


public string getHTMLFormatString()
{
//TODO: validate your data
string txt = opp.Tender_Name_or_Reason_for_Quotation__c;

txt = txt.replaceAll('\n', '<br/>');

return txt;
}

}

 

All Answers

Sam.arjSam.arj

Use the "replace" method of the string object to replace all carriage returns "\n" with "<br/>" HTML tag.    

 

This should happen at the time that you are loading data into your viewer (Panel, Label, etc). 

 

 

SlanganSlangan

Thanks for the reply.

 

I am very new to methods and classes - and I have never done any coding that involves strings. Is there any documentation I can look at that you would suggest?

 

Thanks again - your input is very helpful.

 

Shannon

Sam.arjSam.arj

Force.com Apex Developer's Guide:

 

http://www.salesforce.com/us/developer/docs/apexcode/index.htm

 

SlanganSlangan

So I have looked at the developers guide, but I am having troubles wrapping my head around patterns and matches (what I assume needs to be done to get this to work?). I thought by doing a pattern.split I could just split the string on any occurance of \n and thus not have to worry about replacing anything. Unfortunately, I can't figure out how to do it - my attempts fail with a "Method does not exist or incorrect signature: pattern.split(String)" error.

 

Can you offer me some quick guidance?

SlanganSlangan

Ok, so I have the following class :

 

----------------------------------------------------------------------

 

 

public class String_functions { public String_functions(ApexPages.StandardController controller) { } public static String convertCRtoBR(String StringIn) { String result = ''; if ((StringIn != null) && (StringIn != '')) { List<String> StringLines = StringIn.split('\n',0); for (String StringLine : StringLines) { if ((result != '') && (StringLine != null)) {result += '<BR>';} if (StringLine != null) {result += StringLine;} } } return result; } //****************** //Test Method //****************** static testMethod void TestMe() { String result = ''; result = convertCRtoBR('Test1\nTest2\nTest3'); System.assertEquals('Test1<BR>Test2<BR>Test3',result); } //The End }

 

and I am trying to call the function convertCRtoBR (to convert all \n to <br/> with the following code:

 

 

<apex:outputText escape="false" id="ReasonForQuotation" value="{!convertCRtoBR('Opportunity.Tender_Name_or_Reason_for_Quotation__c')}"/>

 

 My page reference to the class extension looks like this:

<apex:page standardController="Opportunity" extensions="String_functions" >

 

Yet I am receiving the error message: 

 

"Error: Unknown function convertCRtoBR. Check spelling."

 

 

Can anyone tell me what I am doing wrong? Again - I am very new at this, so any guidance is really appreciated!


 


 

Sam.arjSam.arj

In your controller code:

 

public class myController { Opportunity opp; public myController(ApexPages.StandardController controller) { opp = (Opportunity)controller.getRecord(); } public getHTMLFormatString() { //TODO: validate your data string txt = opp.Tender_Name_or_Reason_for_Quotation__c; txt = txt.replaceAll('\n', '<br/>'); return txt; } }

 In the VF page:

 

<apex:outputText escape="false" id="ReasonForQuotation" value="{!HTMLFormatString}"/>

 

 

 

SlanganSlangan

Great stuff - thanks. You rule.

 

Is there a way to make this work so I can add it to my visualForce page outputText items w/o hard-coding each field with separate variables (ie: have one 'function' I can apply to any text field to separate the line breaks)?

Sam.arjSam.arj

Nops.

If you have 5 fields then you should have 5 properties to bind the visual components to.

regards,

 

SlanganSlangan

That explains a lot. Thank you so much for your help. I really appreciate it.

 

I tried your code but it is throwing a compile error. I put it in exactly as you wrote it. Any ideas?

 

Compile Error: Invalid constructor name: getHTMLFormatString.

Sam.arjSam.arj

sorry my bad:

 

public class myController {
Opportunity opp;

public myController(ApexPages.StandardController controller) {
opp = (Opportunity)controller.getRecord();
// you may also need this since standard controller will not load all the fields
opp = [select id, name,Tender_Name_or_Reason_for_Quotation__c from opportunity where id = :opp.id];
}


public string getHTMLFormatString()
{
//TODO: validate your data
string txt = opp.Tender_Name_or_Reason_for_Quotation__c;

txt = txt.replaceAll('\n', '<br/>');

return txt;
}

}

 

This was selected as the best answer
SlanganSlangan

I have already added that - thanks.

 

Unfortunately, even with the new code, it still isn't working. I am still not getting any line-breaks. I noticed that the new code calls a 'get' function. Do I not need to 'set' the value as well for it to register?

 

Thanks again for the excellent help here - this is great learning for me.

Sam.arjSam.arj

You could try replacing the line feeds as well to see if you get a better results:

 

 

txt = txt.replaceAll('\n','<br/>'); txt = txt.replaceAll('\r','<br/>');

 

 It all depends how your text is formated, check the documentation for strings in Apex Code Developer's Guide.

 

SlanganSlangan

It is working now. I found out that the breaks only work on long text fields. Some of my fields were the 'short' input fields and once I changed them it worked fine. I shortened the code a bit to eliminate some extra stuff - final code pasted below.

 

I want to really thank you for taking the time to help a novice like me out. It means a lot to be able to come onto the forums and receive such positive feedback and excellent advice/assistance.

 

Kudos to helping make this a great community.

 

 

public class String_functions{ Opportunity opp; public String_functions(ApexPages.StandardController controller) { opp = (Opportunity)controller.getRecord(); opp = [select id, name,Tender_Name_or_Reason_for_Quotation__c from opportunity where id = :opp.id]; } public string getParagraphs() { string defineParagraphs = opp.Tender_Name_or_Reason_for_Quotation__c.replaceAll('\r\n','<br/>'); return defineParagraphs; }

 

 

 

SlanganSlangan
Sorry to bug you again - I now need to write a test for this but have literally never done this before and the documentation on it is...well...lacking. Can you give me a quick crash-course?