Welcome Guest, Not a member yet? Register   Sign In
is there any better way to higlite the form fields via Codeigniter FORM_VALICATION class?
#1

[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
#2

[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?
#3

[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
#4

[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.
#5

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

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

[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/a...lidations/

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
#8

[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?
#9

[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
#10

[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...




Theme © iAndrew 2016 - Forum software by © MyBB