![]() |
Looping through error_string with JS - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Looping through error_string with JS (/showthread.php?tid=2793) |
Looping through error_string with JS - El Forum - 08-24-2007 [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) { ?> ... and this JS function is in the head: Code: function ShowErrors(str) 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! Looping through error_string with JS - El Forum - 08-24-2007 [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. Looping through error_string with JS - El Forum - 08-24-2007 [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 ![]() |