Codeigniter: Validation not happenning because of

2019-06-07 20:47发布

问题:

I am using tinymce as textarea And i am submitting my form using AJAX.

My textarea is not validating because of this tinymce plugin. And if i remove this plugins from that textarea then this textarea is validating correctly.

Why this mess? Please help me to get out from this problem.

My VIEW page

  <script src="http://static.tinymce.com/tinymce/js/4.0b1/tinymce.min.js"></script>  
  <script type="text/javascript">
    tinymce.init({
        selector: "textarea", height: 300,
        theme_advanced_font_sizes: "10px,12px,14px,16px,24px",
        plugins: [
            "advlist autolink lists link image charmap print   anchor autolink ",
            "searchreplace visualblocks  visualchars code fullscreen",
            "insertdatetime media table contextmenu paste hr emoticons directionality"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image "
    }); 


    <!--------FORM SUBMIT AJAX ACTION -->
        $(document).ready(function() { 

            var _status = $('#status');
           $("#sendedits-form").submit(function(e)
    {
                    var postData = $(this).serializeArray();

               var formURL = $(this).attr("action");
                $.ajax({
                    url : formURL,
                    type: "POST",
                    data : postData,
                    async: true, 
                     dataType: "json",
                    success: function(dat) { 
                        if (dat.status === 'msgsent') {  // $('#status').empty();
                            alert("succes");
                        }
                        else if (dat.status === 'fail') {

                        }
                        else
                        {
                            $("#status").empty(); $('#subeditbtn').text('Submit');
                            $('#loadingimg').hide();
                            $('#subeditbtn').attr('disabled', false);
                            _status.html('<span class="err">' + dat.message + '</span>');
                            $("#status").fadeIn("slow");

                            $("#status").click(function() {
                                $("#status").fadeOut("slow");
                                return false;
                            });
                        }
                    },
                    error: function(err) {
                       // alert("Ooops! Try again later or else sends us  message regarding this issue. Thankyou!");
                    }
                });
                e.preventDefault();
            }); 


            $('.ckdeditor').keyup(function(){
          var keyed = $(this).val();
          $(".ckdeditor").html(keyed);
     });
             });
   </script>
 <!--- now my html form page --> 
 <?php $secattr = array('name' => 'sendeditsform', 'id' => 'sendedits-form'); ?>
                <?php echo form_open('article/sendedits', $secattr); ?>
                <table class="formtable" width="100%"  style="margin-top: 0%;">

                    <tr>
                        <td width="10%">&nbsp;</td>
                        <td width="90%" >&nbsp;</td>
                    </tr>

                    <tr>
                        <td width="10%" style="vertical-align: middle;text-align: right;font-weight: bolder;"><?php echo '<div id="level_removeMe">Body  &nbsp; </div>'; ?> </td>
                        <td width="90%"  align="left" valign="top"><?php echo form_textarea($fcontent);  //some text data in <ul><li></li> format generated from this same textarea ?></td>
                    </tr>

                    <tr>
                        <td width="10%" align="center" valign="bottom">&nbsp;</td>
                        <td width="90%" align="left"><input type="submit"  value="Submit"/></td>
                    </tr>    
                </table>  
                <?php echo form_close(); ?> 

Controller

 public function  sendedits()
{    $this->load->library('form_validation');
      $this->form_validation->set_error_delimiters('<li  class="errorlist">', '</li>')->set_rules('content', 'Body', 'trim|required');

        if (!$this->form_validation->run()) 
        {   //False  
            $this->_status['status'] = "error";
            $this->_status['message'] = $this->load->view('commonfiles/ajax_error_display_1', '', TRUE);
        } 

         else if ($this->form_validation->run() && $this->input->post('myId')=='')//myId=just for checking robot or human
          {     $this->_status['message'] = 'Thankyou for your edits. We will review it before publishing.'; 
            $this->_status['status'] = "msgsent";
             //send email
          } 
       echo json_encode($this->_status);    
} 

回答1:

Firstly: add ID attribute to your textarea. I was wrong saying 'editor selector'. It has to be editor ID :). Before serializing form, add this code:

 $('#textarea_id').html( tinymce.get('textarea_id').getContent() );

TinyMCE adds some HTML tags. So the solution is to compare strip_tags([your variable]) to empty, but of course it will make codeigniter verification useless