Validate textarea field with summernote and php

Asked

Viewed 524 times

0

We’re using summernote in a project with Bootstrap. It’s like this:

inserir a descrição da imagem aqui

HTML is that way:

<textarea class="md-form-control md-static" id="summernote" name="Mensagem" rows="18" ></textarea>

The Jquery:

$(document).ready(function() {

              $('#summernote').summernote({
                toolbar: [
                       // [groupName, [list of button]]
                       ['style', ['bold', 'italic', 'underline', 'clear']],
                       ['font', ['superscript', 'subscript']],
                       ['fontsize', ['fontsize']],
                       ['color', ['color']],
                       ['para', ['ul', 'ol', 'paragraph']],
                       ['height', ['height']]
                     ],
                  height: 350,                 // set editor height
                  minHeight: null,             // set minimum height of editor
                  maxHeight: null,             // set maximum height of editor
                  placeholder: 'Digite sua mensagem aqui...',
                  focus: true               // set focus to editable area after initializing summernote
              });

              $('.inline-editor').summernote({
                  airMode: true
              });

The validation of the fields is in PHP, but we are not able to validate the textarea field. We have tried:

if(empty($mensagem)){
  // Mensagem de erro
}

When we gave the print_r() in $_POST, appears that way:

Array ( [TipoEnvio] => [Titulo] => [Mensagem] =>


) 1

I’ve used the strip_tags($mensagem,'<br><p>');, but did not help. According to documentation of summernote:

The Editing area needs <p><br></p> for Focus, Even if the editor content is Empty. So Summernote Supports this method for helping to check if editor content is Empty.

And suggest using:

if ($('#summernote').summernote('isEmpty')) {
  alert('editor content is empty');
}

Only we need to validate in PHP. When I give strlen($mensagem), it returns as it already has 11 characters, even without typing anything. How can I solve this?

  • Ever tried to use empty(trim($mensagem))?

  • Hi bio. I’ve tried using that combination, but it didn’t work either. If there is no solution, I will have to use Jquery to validate, but we are preferring it to PHP.

  • Hello dvd. I changed the post and included HTML with Jquery.

  • 1

    You’re getting it on $_POST['Mensagem']?

  • Yes. In the traditional way $mensagem = $_POST["Mensagem"];.

1 answer

1


Use the method indicated by the Summernote documentation to check if it is empty and if it is positive, empty the textarea before Ubmit:

if($('#summernote').summernote('isEmpty')){
   $('#summernote').val('');
}

This way, if nothing is typed in the editor, the value will be sent empty to PHP.

  • Hello dvd. Attended yes. Sorry, I completely forgot to give as accepted. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.