Welcome Guest, Not a member yet? Register   Sign In
form_validation
#1

[eluser]HooJee[/eluser]
Hi Guys

I am working on an AJAX app and need to do some form input validations. Does form_validation support AJAX? If no, are there any work arounds ?
#2

[eluser]darkhouse[/eluser]
Your application cannot tell the difference between a request made by submitting a form and a request made by ajax, so everything is "supported" by ajax.
#3

[eluser]Colin Williams[/eluser]
That's a weird way of wording it, darkhouse, but your point is spot on. The web is request/response. Always. Your role on the server is the response. So, if it's an Ajax request, you provide a different response: XML, JSON or partial HTML. If it's a regular request from the browser, you probably respond with a full HTML document. Use your controllers to determine what kind of response to provide, and use your views to format appropriately.
#4

[eluser]HooJee[/eluser]
Great. I'm asking because the set_rules method requires 3 parmeters, the second one being used to create an error message string. That param is kind of pointless for ajax unless there is a way to derive the message ?
Anyway, I have implemented the form_validation controller but everytime I execute it the run() method keeps returning false. I am using jQuery to make a POST request. Code below:

Code:
function send() {
    // SET VARIABLES
    $ADMIN_EMAIL = "[email protected]";
    $response = array('type'=>'');
    
    // LOAD LIBRARIES
    $this->load->library('form_validation', 'email');
    
    //SETUP VALIDATION RULES
    $this->form_validation->set_rules('name', 'Name', 'required|min_length[1]');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('comments', 'Comments', 'required|min_length[5]');
            
    if ($this->form_validation->run())
    {
        // send email
    }
    else
    {
        // Print Error Msg
    }
}
#5

[eluser]xzela[/eluser]
hi,

to output a json response from php just use:
Code:
$json = array();
  $json['username'] = 'foobar';
echo json_encode($json)

your json response will be:
Code:
username: 'foobar';

You can use jQueries responders to display messages from your server. See jQuery documentation:
http://docs.jquery.com/Post
#6

[eluser]David Johansson[/eluser]
[quote author="HooJee" date="1248712447"]I am using jQuery to make a POST request.[/quote]
I know you can get that kind of problems with AJAX if you forget to send the query string as post data. Just make sure you get the post data before trying to validate, at least during debugging. Smile




Theme © iAndrew 2016 - Forum software by © MyBB