Welcome Guest, Not a member yet? Register   Sign In
[solved] don't understand javascript variable scope; now talking about validation plugin
#12

[eluser]Fabdrol[/eluser]
Well, that depends. If you actually need to validate the contents of the field agains something in the database, you could use AJAX. But, if it isn't sensible information, you could also load the value it should be at page load, and then check against that. If it ís sensitive information, I wouldn't check it using javascript at all.

Example, what you could do:
1. In your controller loading the form.
Code:
public function edit($page_id) {
    $field_should_be = 'foo';
    $data['form_contents'] = $this->page->id($page_id);
    $data['validations'] = json_encode(array(
        'first_name' => 'required|min(5)|max(10)',
        'last_name' => 'required',
        'email' => 'required|email',
        'other_field' => 'required|should_be('.$field_should_be.')'
    ));
    $this->load->view('form');
}

Then, on the page, you could do something like this:
Code:
var validations = <?=$validations?>; // gets loaded as an json object

$('form').submit(function() {
    var valid = true;
    for(key in validations) {
        var rules = validations[key];
        var field = key;

        if($('input[name="'+field+'"]').validates(rules) !== true) {
            valid = false;
        }
    }

    return valid;
});

In the above case you should implement a jQuery plugin "validates" which handles the rules you supplied via php. It's a bit more work than simple validation, but it'll be worth the effort: you can use it among all your projects and never need to rewrite it!

Fabian


Messages In This Thread
[solved] don't understand javascript variable scope; now talking about validation plugin - by El Forum - 01-26-2011, 08:12 AM



Theme © iAndrew 2016 - Forum software by © MyBB