• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter Form Validation not working

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 (
                                'field' => 'post_title',
                                'label' => 'Post title',
                                'rules' => 'trim|required|alpha_numeric_spaces'
                                'field' => 'post_desc',
                                'label' => 'Post Description',
                                'rules' => 'trim|required|alpha_numeric_spaces'
                                'field' => 'post_content',
                                'label' => 'Post content',
                                'rules' =>  'trim|required'
                                'field' => 'headerimage',
                                'label' => 'File',
                                'rules' => 'required|callback_file_check'
                    $info['errors'] = validation_errors();
                    $info['success'] = false;
                    $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
['success'] = true;
                    $info['message'] = "Successfully added blog post";

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 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/*" />
            <input type="text" placeholder="Enter your post description" name="post_desc" class="form-control">
            <label class="control-label text-muted">Post Body:</label>
                 <textarea id="post_content"></textarea>
                <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.

            $post_content = $('#post_content').summernote('code');
                data: $('#blogform').serialize() + "&post_content=" + $post_content,
                type: "POST",
                dataType: 'json',
                encode: true,
                success: function(data){
                            $('#blog-message').html(data.errors).addClass('alert alert-danger');

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


Have you resolved this?

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 )

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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.