TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Codeigniter & JQuery validation problem (http://www.talkphp.com/advanced-php-programming/5196-codeigniter-jquery-validation-problem.html)

adham is me 12-29-2009 02:40 PM

Codeigniter & JQuery validation problem
 
hello everyone , i am trying to do some form validation using Condeiginter and Jquer's AJAX .

the view file
PHP Code:

</script>
<script type = "text/javascript">
    $(document).ready(function(){
        var dest = '<?php echo site_url("/users/fieldUpdate");?>';
     $('.item').editable(dest, {
         indicator : 'Saving...',
         tooltip   : 'Click to edit...',
         id   : 'fieldName',
         name : 'fieldValue',
         submit : 'Save',
         cancel : 'Undo',
         ajaxoptions: {
             dataType : 'json',
             success : function(data){
                 $("#response").html(data.msg).show(1000).slideDown();
             }
         }
        });
     });
</script>
<div id="response" class ="hidden">

</div>
    <label for ="username">Username : </label>
    <br>
    <h4 id="username" class ="item"><?php echo $userDetails['username'];?></h4>
    <br>
    <h4 id="password" class ="item"><?php echo $userDetails['password'];?></h4>
    <br>
    <h3 id="email"class ="item"><?php echo $userDetails['email'];?></h3>
    <h4 id="quote"class ="item"><?php echo $userDetails['quote'];?></h4>
    <br>
    <h4 id="avatarH4"><?php echo $userDetails['avatar'];?></h4>

the controller

PHP Code:


public function fieldUpdate(){

                if(isset(
$_POST['fieldName']) && isset($_POST['fieldValue'])){
                
                
// setting rules for CI validation class
            
$rules = array(
                               array(
                                     
'field'   => 'username',
                                     
'label'   => 'hblXhbl',
                                     
'rules'   => 'min_length[6]'
                                  
),
                                  array(
                                     
'field'   => 'password',
                                     
'label'   => 'Password',
                                     
'rules'   => 'required|min_length[5]|max_length[45]'
                                  
),
                                array(
                                     
'field'   => 'avtar',
                                     
'label'   => 'Avtar',
                                     
'rules'   => 'min_length[5]|max_length[105]|valid_url'
                                  
),
                                array(
                                     
'field'   => 'quote',
                                     
'label'   => 'Quote',
                                     
'rules'   => 'min_length[5]|max_length[100]'
                                  
)
                                  );
                
// getting proper rule
                
for($i=0;$i<5;$i++){
                if(
$_POST['fieldName'] == $rules[$i]['field']){
                    
// must be wrapped in an array to be in a valid format
                    
$fieldRules = array($rules[$i]);
                    break;
                    }
                }

                
$vArray $this->form_validation->metValidate($fieldRules);
                if(
$vArray[0]){
                    
// an array of DB field name and the new value of it
                    
$updateArray = array($_POST['fieldName']=>$_POST['fieldValue']);
                    
// no user can reach here untill he was signed in
                    
$userId $this->session->userdata("userid");
                    
// update userData in the DB
                    
$this->musers->updateUserdata($updateArray,$userId);
                    
// return a success msg
                    
$result = array("status"=>'success','msg'=>'userData updated !');
                }else{
                      
$result = array("status"=>'fail','msg'=>$vArray["validationErrors"]);
                }
                echo 
json_encode($result);
             }
    }

        
// a function used to update user data
        
public function personals(){
            
// check if the user is logged in
            
if(isLogged()){
                
$myArray whichStatus();
            
$data['status'] = $myArray[0];
                
$data['username'] = $myArray[1];
                
// the main view (a view contains user's personal information updatable using fieldUpdate() method)
                
$data['main']="Vpersonals";
                
$data['userDetails'] = $this->musers->getUserById(1,true);
                
$this->load->view("Vmain",$data);
              }else{
                    
// user is not logged in
                  
redirect("/","refresh");
              }
        } 

the validation method METvalidate()
PHP Code:

public $uniqueErrors = array();
    
    
    
// rules is an array of rules passed to the form validation class
    // uniques is an array of fields that must be unique
    
public function metValidate($rules,$uniques false){
        
$this->CI->form_validation->set_rules($rules);
            if(
$this->CI->form_validation->run()==false){
                            return array(
false,"validationErrors"=>validation_errors());
        }
        
        
// if no errors related with form validation then start checking uniques
        
if($uniques){
            
$this->CI->load->model("mglobal");
            
$tableName $uniques['tableName'];
            
            
// detecting non-unique fields 
            
foreach($uniques['uniques'] as $key=>$value){
                if(!
$this->CI->mglobal->isUnique($tableName,$key,$value)){
                    
$this->uniqueErrors[] = $value;
                }
            }
        if(
count($this->uniqueErrors)){
            return array(
false,"uniqueErrors"=>$this->uniqueErrors);
            }
        }
        
// if they are all fine just return true
        
return array(true);
    } 

the result that i get when executing this code is
1- when updating the username always i get userData updated as a 'msg' in the returned JSON object (even if the new username is lower the specified length or longer than it or even left blank !!).
2- any update in the password field results in the same message that says "The Password field is required".

so where is the problem !!


All times are GMT. The time now is 07:27 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0