Welcome Guest, Not a member yet? Register   Sign In
acceptable when using javascript?
#4

[eluser]Flemming[/eluser]
double coding - unfortunately it's the way!

After trying lots of different approaches, I've eventually settled on this (using jquery validate):

immediately after my CI validation rules, I write the same jquery validate rules like this...

Code:
$this->form_validation->set_rules('headline', 'Headline', 'trim|required|max_length[250]|xss_clean');
$this->form_validation->set_rules('article_body', 'Article', 'trim|required|xss_clean');

$rules_array = array();
$rules_array[] = 'headline: {required: true, maxlength: 250}';
$rules_array[] = 'article_body: {rte_required: true}';

$messages_array = array();
$messages_array[] = 'headline: {required: "Please enter a headline", maxlength:"The headline can not exceed 250 characters"}';
$messages_array[] = 'article_body: {rte_required: "Please enter the article text"}';

// pass the jquery validation stuff to the view in $data
$data['jqvalidate']['form_add_news']['rules'] = implode(',',$rules_array);
$data['jqvalidate']['form_add_news']['messages'] = implode(',', $messages_array);

then in my view, indside the <head> I check to see if there's any jquery validation to write:

Code:
<?php
if(isset($jqvalidate)) {
    foreach($jqvalidate as $k=>$v) {
?>
    $('#<?php echo $k?>').validate({
        rules: {<?php echo $v['rules']?>},
        messages: {<?php echo $v['messages']?>}
    });
<?php
    }
}
?>

this needs the jquery and the validation plugin but I find it cuts down a fair bit of work and repetition.

If anyone has any slicker way I'd love to hear them!


Messages In This Thread
acceptable when using javascript? - by El Forum - 06-08-2010, 12:18 PM
acceptable when using javascript? - by El Forum - 06-08-2010, 03:21 PM
acceptable when using javascript? - by El Forum - 06-08-2010, 05:30 PM
acceptable when using javascript? - by El Forum - 06-09-2010, 10:32 AM



Theme © iAndrew 2016 - Forum software by © MyBB