Welcome Guest, Not a member yet? Register   Sign In
Looping through error_string with JS
#1

[eluser]BrandonDurham[/eluser]
I know it sounds crazy, but hear me out.

I'm building a site for a client in CI that used to exist in HTML with JS form validation. They're very happy with the way it's working except they prefer the old-school javascript alert boxes letting the user know they've done something wrong in the form. I want to be able to tie it in directly with CI's validation class. I'm currently doing this in my view:
Code:
<?if ($this->validation->error_string) { ?>
    
        msg = '<?=$this->validation->first_name_error; ?>';
        ShowErrors(msg);
    
<? } ?>

... and this JS function is in the head:
Code:
function ShowErrors(str)
{
    msg  = "______________________________________________________\n\n";
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n";
    msg += str;
    alert(msg);
}

This works fine, however the form is approximately 20 fields long and I don't really want to add each field's error individually. Is there a way I can pass the "ShowErrors" JS function the $this->validation->error_string and have it cycle through all errors?

I appreciate any help.

Thanks!
#2

[eluser]BrandonDurham[/eluser]
By the way, if I try to pass it along like this:
Code:
ShowErrors('<?=$this->validation->error_string?>')

I get this JS error: "Unterminated string literal", which essentially means the string is too long. Typically the way to fix that error is to break the string up into smaller strings and concatenate with "+". That's why I wanna loop through.

Sigh.
#3

[eluser]deviant[/eluser]
It looks like the Validation class basically constructs error_string at the end of the run function by looping through the _error_array class variable (which is an array) and concatenating the elements into one long string (error_string). The way I see it you could either:

a) Grab the array straight out into the Validation class using $this->validation->_error_array

or

b) Extend the Validation class to provide a method which returns the array

The first option is dirtier seeing as that error_array is technically a private variable, but I don't know if you can be bothered extending the Validation class just for the hell of it Tongue




Theme © iAndrew 2016 - Forum software by © MyBB