CodeIgniter Forums
is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class? (/showthread.php?tid=25220)



is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class? - El Forum - 12-04-2009

[eluser]Yoosuf Muhammad[/eluser]
hello,

i am sort for working with CI and now i am having a practical problem (that can be done via Javascript, since i want to make accessible solution i need this requirement) is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class?

for example you can take similar like Ruby On Rails form validation


is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class? - El Forum - 12-04-2009

[eluser]CroNiX[/eluser]
Sure, in the view you can check if an error exists for a field and if it does apply a different class to that field, div, fieldset, whatever. Is that what you mean?


is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class? - El Forum - 12-04-2009

[eluser]Yoosuf Muhammad[/eluser]
Yes, how to set that CSS class to the Field?

e.g.

Code:
<input type="text" id="name" name="name" />

as above, how to set the class on runtime? if there is a way please let me know.

regards
Yoosuf


is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class? - El Forum - 12-04-2009

[eluser]CroNiX[/eluser]
Code:
<input type="text" id="name" name="name" <?php if(form_error('name')) echo 'class="some-highlight-class" '; ?>/>

Or something like that. Then just have "some-highlight-class" defined in your stylesheet.


is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class? - El Forum - 12-04-2009

[eluser]Yoosuf Muhammad[/eluser]
Awesome thanks for the response and he tiny code snip. it works perfectly. thanks allot @CroNiX


is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class? - El Forum - 12-05-2009

[eluser]CroNiX[/eluser]
glad it worked for ya Smile


is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class? - El Forum - 12-07-2009

[eluser]Whiplash[/eluser]
I was thinking about this and decided I didn't like having to put that code into my HTML for every single field on the page. So I wrote a small function to automatically insert a css class tag if the field doesn't validate. I created a MY_form_helper.php file and threw this in there, then I call it from _parse_form_attributes which I overrode to include the call to my function. Here's the function, and I wrote up a post on it on my blog. http://64sqft.wordpress.com/2009/12/08/apply-classes-based-on-codeigniter-form-validations/

Code:
function _apply_validation_tags(&$default)
{
    if(form_error($default['name']))
    {
        if (isset($default['class'])) {
            $class_terms = explode(' ', $default['class']);
            $class_terms[] = 'validation_error';
            $default['class'] = implode(' ', $class_terms);
        }
        else
        {
            $default['class'] = 'validation_error';
        }
    }
    return $default;
}

It seems to work pretty well. Let me know what you think, I'm really new to PHP and Codeigniter so suggestions are welcome.

Dana


is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class? - El Forum - 12-11-2009

[eluser]Yoosuf Muhammad[/eluser]
[quote author="Whiplash" date="1260272676"]

Code:
function _apply_validation_tags(&$default)
{
    if(form_error($default['name']))
    {
        if (isset($default['class'])) {
            $class_terms = explode(' ', $default['class']);
            $class_terms[] = 'validation_error';
            $default['class'] = implode(' ', $class_terms);
        }
        else
        {
            $default['class'] = 'validation_error';
        }
    }
    return $default;
}

It seems to work pretty well. Let me know what you think, I'm really new to PHP and Codeigniter so suggestions are welcome.

Dana[/quote]

how to use this helper function block?


is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class? - El Forum - 12-15-2009

[eluser]Whiplash[/eluser]
[quote author="Muhammad" date="1260580175"]how to use this helper function block?[/quote]

I just extended the _parse_form_attributes function from the form_helper class and then added a call to my function.

Dana


is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class? - El Forum - 12-15-2009

[eluser]Johan André[/eluser]
[quote author="Whiplash" date="1260914901"][quote author="Muhammad" date="1260580175"]how to use this helper function block?[/quote]

I just extended the _parse_form_attributes function from the form_helper class and then added a call to my function.

Dana[/quote]

...which basically means that you copy the MY_form_helper.php into your helpers directory and then the form_input(), form_textarea() and such will be modified to highlight your validation-errors...