Validate textarea field with summernote and php

0

We're using summernote in a Bootstrap project. It looks like this:

HTMLlookslikethis:

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

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
              });

Field validation is in PHP, but we are not able to validate the textarea field. We've tried:

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

When we gave print_r() to $_POST , it looks like this:

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


) 1

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

  

The editing area needs <p><br></p> for focus, even if the editor   content is empty. So Summernote supports this method for helping   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 had 11 characters, even without typing anything. How can I resolve this?

    
asked by anonymous 24.05.2018 / 20:17

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 submitting:

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

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

    
24.05.2018 / 21:17