You need to sign in to do that
Don't have an account?

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
sorry my bad:
All Answers
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).
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
Force.com Apex Developer's Guide:
http://www.salesforce.com/us/developer/docs/apexcode/index.htm
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?
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')}"/>
<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!
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}"/>
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)?
Nops.
If you have 5 fields then you should have 5 properties to bind the visual components to.
regards,
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.
sorry my bad:
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.
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 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; }