Welcome Guest, Not a member yet? Register   Sign In
get validation errors
#1

[eluser]tastebuds[/eluser]
Hi Fellas!

I just wanna know how i can get all error messages from form_validation lib..
Code:
function verify(){
              $data = //get all errors in form_validation;
              return $data;
        }

The reason why i want to do this is i am trying to use ajax in a registration form so i need to get the error messages first Thanks in Advance!!
#2

[eluser]dhiya[/eluser]
function verify()
{
$data['message'] = validation_errors();
return $data;
}
#3

[eluser]dhiya[/eluser]
function verify()
{
$data['message'] = validation_errors();
return $data;
}
#4

[eluser]CroNiX[/eluser]
The only built in way is using validation_errors(), as dhiya pointed out. However, this will return a string containing all error messages. If you would like an array that contain the field names along with the error messages for each field, you would have to extend the validation library and create a new method that returns $_error_array.
#5

[eluser]tastebuds[/eluser]
Thanks for the help.. Mr Dhiya really appreciated...

And for Mr CroNix i will try that out.. thank you for the help!
#6

[eluser]tastebuds[/eluser]
[quote author="CroNiX" date="1334855196"]The only built in way is using validation_errors(), as dhiya pointed out. However, this will return a string containing all error messages. If you would like an array that contain the field names along with the error messages for each field, you would have to extend the validation library and create a new method that returns $_error_array.[/quote]

I extend the library as just u pointed out and here's the code..
Code:
class MY_Form_validation extends CI_Form_validation{
    
    var $_error_array = array();
    
    
    function __construct($config=array()){
        parent::__construct($config);
    }
    
    function error_array(){
        if(count($this->_error_array)===0){
            return FALSE;
        }else{
            return $this->_error_array;
        }
    }
    
    function get_tae(){
        return "TAE!";
    }
}


and i echo'ed out the errors in json format in php validation and i get this

This is the desirable output
{"username":"The username field is required.","email":"The email field is required."}

but when i try in jquery ajax it just returning false, here's the snippet for the jquery code btw

Code:
$(function(){
               $('#my_form').submit(function(e){
                    $.get('ajax_verify', function(data){
                        console.log(data);
                    })                    
                    e.preventDefault();
                })
                
        });

i dont know if im doing it wrong and i am very very sorry for the noobish question, need help please >.<
#7

[eluser]CroNiX[/eluser]
Don't redeclare the $_error_array. It's declared in the original class that you are extending. That's probably why it's returning FALSE each time. Also, if you are using this with ajax, you probably want to json_endcode() array instead of returning it. You can't "return" things from php to javascript. You have to echo something. Personally, I'd just echo the (json_encoded) array.
#8

[eluser]tastebuds[/eluser]
[quote author="CroNiX" date="1334900425"]Don't redeclare the $_error_array. It's declared in the original class that you are extending. That's probably why it's returning FALSE each time. Also, if you are using this with ajax, you probably want to json_endcode() array instead of returning it. You can't "return" things from php to javascript. You have to echo something. Personally, I'd just echo the (json_encoded) array.[/quote]

yeah i already got the error messages in just PHP VALIDATION

what the problem is in ajax validation i should get the error messages but i'm just getting a FALSE String.

for clarification this is my complete code

CONTROLLER

Code:
function ajax_verify(){
        $this->form_validation->set_rules('username', '', 'max_length[25]|min_length[4]|trim|required|xss_clean');
        $this->form_validation->set_rules('email', '', 'valid_email|required|trim|xss_clean');  
        
        if($this->form_validation->run()==FALSE){
                 $errors = $this->form_validation->error_array();
                 echo json_encode($errors);  
        }else{
            
        }
    }

LIBRARY

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation{
    
      
    function __construct(){
        parent::__construct();
    }
    
    function error_array(){
        if(count($this->_error_array)===0){
            return FALSE;
        }else{
            return $this->_error_array;
        }
    }
    
    function get_tae(){
        return "TAE!";
    }
}

And the VIEW

Code:
<!DOCTYPE HTML>
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
[removed][removed]
&lt;title&gt;Title&lt;/title&gt;
&lt;style&gt;
    input{
        display:block;
    }
&lt;/style&gt;
&lt;body&gt;

&lt;?php //echo validation_errors('<div class="error">', '</div>');?&gt;
&lt;?php echo form_open('ajax/ajax_verify', array('id'=>'my_form'));?&gt;
&lt;?php echo form_label('Username'); ?&gt;
&lt;?php echo form_input('username', '', array('id' => 'username'));?&gt;
&lt;?php echo form_label('Email Address'); ?&gt;
&lt;?php echo form_input('email', '', array('id' => 'email')); ?&gt;
&lt;?php echo form_submit('submit', 'Submit'); ?&gt;
&lt;?php echo form_close();?&gt;

[removed]
      $(function(){
               $('#my_form').submit(function(e){
                    $.get('ajax_verify', function(data){
                        console.log(data);
                    })                    
                    e.preventDefault();
                })
                
        });
[removed]
&lt;/body&gt;
&lt;/html&gt;





Theme © iAndrew 2016 - Forum software by © MyBB