Welcome Guest, Not a member yet? Register   Sign In
CI and js Form Validation
#1

[eluser]al404[/eluser]
is not clear to me if CI can validate form data only after submit or as some JS form validation before?

if not, can you tell me a plugin for inline ajax style validation?
#2

[eluser]Nick Husher[/eluser]
CodeIgniter only does server-side validation. It sets up logical framework for determining if the user input is correct and lets you determine how you want that validation to happen.

You can use the CI form validation over an Ajax link, although there's no boxed way to do it as far as I know. The basic workflow would be to hijack the "submit" event on the form, capture the form data, and send it to a CI method that could validate the data and return an appropriate response.

Code:
<form id="myform" action="some action" method="post">
... your form elements here ...
</form>

Code:
// using YUI, but you don't have to
YAHOO.util.Event.addListener("myform", function(eventObject) {
    // prevent the form from submitting
    YAHOO.util.Event.preventDefault(eventObject);

    YAHOO.util.Connect.setForm("myform");
    YAHOO.util.Connect.asyncRequest('POST',<?=base_url().'ajax_controller/method' ?>, callback);
}

Code:
var callback = {
    success: function(o) {
        if(o.responseText == 'false') {
            // handle invalid input
        } else {
            // handle valid input, probably something like:
            document.getElementById("myform").submit();
        }
    },
    failure: function(o) {
        // request timed out or failed to respond with a "200: OK" status code.
    },
    timeout: 5000
};

Code:
// in "ajax_controller.php"
function validate() {
    // run validation
    // if the validator checks out, echo 'true'
    // else echo 'false'
}


Also, I wrote up a tutorial on using Ajax and CI together in this thread. It makes use of server-side form validation over an ajax link, as described above.




Theme © iAndrew 2016 - Forum software by © MyBB