0
votes

Hello guys I have a little problem. My problem is I want to use jquery form validation plugin together with codeigniter. As of now I can validate my form using jquery validation rules. Like checking the required fields, checking length of input, checking valid email, etc... But for checking the availability of the data in my database I always got an error. I used the remote function but I can't validate my form. Here's my code I hope you can help me.

addNewItem.php

  <script type="text/javascript">

 (function($,W,D){

    var JQUERY4U = {};

    JQUERY4U.UTIL =
    {
        setupFormValidation: function()
        {
            $("#login-form").validate({
            rules: {
                name: {
                    required: true,
                    remote: {
                        type: 'post',
                        url: <?php echo site_url('category_model/checkName'); ?>,
                        data: {
                            'name': $('#name').val()
                        },
                        datatype: 'json'
                    }

                },
                description: {
                    required: true
                }
           },
           messages: {
                username: {
                    remote: "Category name already taken!"
                }
                description: "Please provide a description for category!"
           }

        });

    }
}

    //when the dom has loaded setup form validation rules
    $(D).ready(function($) {
        JQUERY4U.UTIL.setupFormValidation();
    });

})(jQuery, window, document);

</script>

 <div>
  <?php $attr = array('class'=>'form-signin','id'=>'login-form','novalidate'=>'novalidate'); ?>
  <?php echo form_open('category_controller/insertItem',$attr); ?>
    <h2 class="form-signin-heading"></h2>
        <h5 style="font-weight: normal;">Category Name:</h5>
        <input type="text" class="input-block-level" placeholder="Category Name" name="name" autofocus="autofocus" value="" required="requried" id="name" />
        <h5 style="font-weight: normal;">Desciption</h5>
        <input type="text" class="input-block-level" placeholder="Description" name="description" value="" required="required" id="description" />
        <br />
        <div align="right">
            <input type="submit" value="OK" class="btn btn-large btn-primary" />
            <input type="button" value="CANCEL" class="btn btn-large btn-primary" name='cancel' />
        </div>
  <?php echo form_close(); ?>

my model (category_model/checkName)

public function checkName(){

    $catname = ucwords($this->input->post('name'));
    $validate = "SELECT COUNT(*) AS valid FROM sales_category WHERE salescatname = '{$catname}'";
    $testvalidate = $this->db->query($validate);
    foreach($testvalidate->result_array() as $row){
        $is_valid = $row['valid'];
    }

    if($is_valid > 0){
        return TRUE;
    }else{
        return FALSE;
    }
}
1
what query return from db? did yo debug $is_valid? - Alex Kneller
Yes. My query is working. - rochellecanale
Is the ajax call working? you can use the Network tab on Chrome Dev Tools to analyze XHR requests. Also you cannot call a method from a model directly from a url, so in your url category_model should be the name of your controller unless of course you are using url routing. - omma2289
still not working. If i included the remote function my form stop working. If i remove the remote. I can add new category again. - rochellecanale

1 Answers

0
votes

Try changing the following

public function checkName()

{

   $catname = ucwords($this->input->post('name'));
    $validate = "SELECT COUNT(*) AS valid FROM sales_category 
                  WHERE salescatname = '{$catname}'";
    $testvalidate = $this->db->query($validate);
    foreach($testvalidate->result_array() as $row){
        $is_valid = $row['valid'];
    }

    echo $is_valid;

}