CodeIgniter Forums
Codeigniter Form Validation not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Codeigniter Form Validation not working (/showthread.php?tid=67580)



Codeigniter Form Validation not working - nullstrike - 03-12-2017

So I have form with fields and input type file.
Why is that form validation still return an error even though the input file is not empty

Here's my controller together with my callback function

   
PHP Code:
public function add_post()
        {
                $validation = array (
                            array(
                                'field' => 'post_title',
                                'label' => 'Post title',
                                'rules' => 'trim|required|alpha_numeric_spaces'
                                ),
                            array(
                                'field' => 'post_desc',
                                'label' => 'Post Description',
                                'rules' => 'trim|required|alpha_numeric_spaces'
                                ),
                            array(
                                'field' => 'post_content',
                                'label' => 'Post content',
                                'rules' =>  'trim|required'
                                ),
                            array(
                                'field' => 'headerimage',
                                'label' => 'File',
                                'rules' => 'required|callback_file_check'
                                )
    
                        
);
    
                $this
->form_validation->set_rules($validation);
                
                if
($this->form_validation->run()===FALSE)
                {
                    $info['errors'] = validation_errors();
                    $info['success'] = false;
                }
                else
                
{
                    $this->save_image();
                    $datetime $this->get_time();
                    $data = array(
                                "post_title" => $this->input->post('post_title'),
                                "post_content" => $this->input->post('post_content'),
                                "post_image" => $this->uploadFileName,
                                "post_created" => $datetime
                            
);
    
                    $this
->Blog_model->add_post($data);
    
                    $info
['success'] = true;
                    $info['message'] = "Successfully added blog post";
               }
            $this->output->set_content_type('application/json')->set_output(json_encode($info));
        

Here's the form mark-up
 

   
PHP Code:
  <?php echo form_open_multipart('#',array("class"=>"form-horizontal","id"=>"blogform")); ?>
          <div class="row">
                    <div class="col-md-6">
                <input type="text" placeholder="Enter your post title" name="post_title" class="form-control">
            </div>
              <div class="col-md-6 form-group">
                <label for="file" class="control-label col-md-4">Select header image:</label>
                  <div class="col-md-8">
                  <input type="file" id="header" name="headerimage" accept="image/*" />
                </div>
            </div>
          </div>
            <input type="text" placeholder="Enter your post description" name="post_desc" class="form-control">
              
            <label class="control-label text-muted">Post Body:</label>
               <div>
                 <textarea id="post_content"></textarea>
               </div>
                <button class="btn btn-default pull-right" type="button" id="save-post"><i class="fa fa-save"></i> Save Post</button>
                <div class="clearfix"></div>
           <?php echo form_close(); ?>

I call the add_post controller function through AJAX.

   
Code:
$(document).on('click','#save-post',function(){
            $post_content = $('#post_content').summernote('code');
            $.ajax({
                url:site_url('Blog/add_post'),
                data: $('#blogform').serialize() + "&post_content=" + $post_content,
                type: "POST",
                dataType: 'json',
                encode: true,
                success: function(data){
                    if(!data.success){
                        if(data.errors){
                            $('#blog-message').html(data.errors).addClass('alert alert-danger');
                        }
                    }else{
                        alert(data.message);
                    }
                }
            });
    });


I don't understand why validation is not working properly or I think the controller doesn't get the input file.


RE: Codeigniter Form Validation not working - php_rocs - 03-28-2017

@nullstrike,

Have you resolved this?


RE: Codeigniter Form Validation not working - InsiteFX - 03-29-2017

Try change your Ajax url

PHP Code:
url"<?php echo site_url('Blog/add_post'); ?>"



RE: Codeigniter Form Validation not working - dave friend - 03-29-2017

What does your browswer's web dev tool of choice tell you about the data being posted?