Welcome Guest, Not a member yet? Register   Sign In
Codeigniter Form Validation not working
#1

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.
Reply
#2

@nullstrike,

Have you resolved this?
Reply
#3

Try change your Ajax url

PHP Code:
url"<?php echo site_url('Blog/add_post'); ?>"
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

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




Theme © iAndrew 2016 - Forum software by © MyBB