Jump to content

Newline to <br> in Santizer->textarea


kalimati
 Share

Recommended Posts

I am using sanitizer->textarea to sanitize and format the message inputted. I've tried various options including newlineReplacement but have been unable to convert newlines to <br> tags. Although the PHP nl2br function works as expected. Any suggestions?

Thanks.

Link to comment
Share on other sites

But the text and textarea sanitizers don't allow html tags by default and there is also this comment:

'multiLine' => false, // allow multiple lines? if false, then $newlineReplacement below is applicable

Hopefully this will help show what is happening and what the solutions are. You can either go with the option on line 5 or line 7 although if you go with 5 you might also want to adjust the 'maxLength' option from the default 255 depending on expected content.

image.png.30c854203e938fbac8a7a796f36e4c4b.png

 

  • Like 1
Link to comment
Share on other sites

  • 3 years later...
10 hours ago, KarlvonKarton said:

Is there a reason why multiLine and newlineReplacement are not in the API doc? (but still work)

They are in the docs right here for text():

https://processwire.com/api/ref/sanitizer/text/

but you are right, it is not clear from the docs if these also apply to textarea().

Edited by kongondo
clarification
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By Shohan Arafat
      I was looking for code sample where I can attach a textarea for setting up a content in my page. And I wanted to do it programatically. But I can't find a way to do that. Is it possible to add a content textarea with program?
    • By Juergen
      Hello @ all
      I want to share a new module with you, which makes the creation and validation of forms easy.  Take a look at the following example of a simple contact form:
      // A very simple example of a contactform for demonstration purposes $form = new Form('contactform'); $gender = new Select('gender'); $gender->setLabel('Gender'); $gender->addOption('Mister', '0'); $gender->addOption('Miss', '1'); $form->add($gender); $surname = new InputText('surname'); $surname->setLabel('Surname'); $surname->setRule('required'); $form->add($surname); $name = new InputText('name'); $name->setLabel('Name'); $name->setRule('required'); $form->add($name); $email = new InputText('email'); $email->setLabel('E-Mail'); $email->setRule('required'); $form->add($email); $subject = new InputText('subject'); $subject->setLabel('Subject'); $subject->setRule('required'); $form->add($subject); $message = new Textarea('message'); $message->setLabel('Message'); $message->setRule('required'); $form->add($message); $privacy = new InputCheckbox('privacy'); $privacy->setLabel('I accept the privacy policy'); $privacy->setRule('required')->setCustomMessage('You have to accept our privacy policy'); $form->add($privacy); $button = new Button('submit'); $button->setAttribute('value', 'Send'); $form->add($button); if($form->isValid()){ print_r($form->getValues()); // do what you want } // render the form echo $form->render(); This piece of code creates a simple contact form and validates it according to the validation rules set. Inside the isValid() method you can run your code (fe sending an email)
      Highlights:
      30+ validation types Support for UiKit 3 and Bootstrap 5 CSS framework SPAM protection Highly customizable Hookable methods for further customization Multi-language You can download and find really extensive information on how to use at https://github.com/juergenweb/FrontendForms.
      Please report errors or suggestions directly in GitHub.
      Best regards and happy testing 🙂
    • By prestoav
      Hi all and thanks for the great work on PW!

      One thing I find I have to do on any new site is add the Justify plugin to textarea fields as it is such a widely required feature for text headings in content.

      ANy chance this could be added to the core an automatically be installed on new textarea fields using CKEditor?

      Thank you!
    • By benbyf
      Hi, Looking to create form elements on a page–some input with a colection of form inputs and the appropriate labels and variables for that input. I've used ProForms in the past and rolled out my own when creating simply one off forms, but I wonder if anyone has found a good way of allowing form creation on page editing so that clients can adhocly make and edit forms?
      Thanks
    • By Mithlesh
      Changing it to null
×
×
  • Create New...