Search code examples
javascriptphpjqueryhtmlcodeigniter-2

Summernote - Text displayed as raw HTML on validation


I am using Codeiginter with Summernote on front end - Now In a form, when some fields are empty, the same page loads with validation checking. I am using the following JS and CI code:

 $(window).load(function(){
        <?php if(isset($article['text']))
         {
         ?>
        $('#summernote').code('<?php echo $article['text'];  ?>');
        <?php } else { ?>

        $('#summernote').code('<?php echo $_POST['contents'];  ?>').text();

        <?php } ?>
    });

Now the issue is when say, validation fails, the above code populates the Summernote editor with RAW HTML instead of formatted HTML as it should

This is an example of my first post Pellentesque libero tortor, tincidunt et, tincidunt eget, semper nec, quam. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque commodo eros a enim. Aenean imperdiet. Cras sagittis.
Nam at tortor in tellus interdum sagittis. Nullam nulla eros, ultricies sit amet, nonummy id, imperdiet feugiat, pede. Etiam ut purus mattis mauris sodales aliquam. Curabitur blandit mollis lacus. Phasellus tempus.

Any ideas why is this so? I have tried .val and .html - but they don't work. Kindly help.


Solution

  • Okay. After two days of intense efforts - I found out the reason - it's stupid. Codeigniter form-validation encodes all formatted data with escape characters and converts them into raw HTML. To solve this problem, remove the validation for contents in your form_validation.php or your form validation rules. Then you can output using

     $('#summernote').code(<?php echo $this->input->post('contents');?>);
    

    Thanks everybody for help, though.