Welcome Guest, Not a member yet? Register   Sign In
Dynamic class: Form Validation
#1

[eluser]Samus[/eluser]
i'm trying to add a class depending on whether my input elements have passed validation and can't quite think how to do this.

Here's a snippet of my view:

Code:
<span class="formf &lt;?= (form_error(('content')) ? "control-group error" : ""); ?&gt;">
    &lt;?=form_label('Content', 'fcontent');?&gt;
    &lt;?= form_error('content', '<div class="error_message">', '</div>'); ?&gt;
    &lt;?=form_textarea(array('name' => 'content', 'value' => set_value('content'), 'id' => 'fcontent', 'rows' => 3, 'title' => 'Content shit here'));?&gt;
</span>

When the validation is run, and an input element does not pass validation the classes 'control-group error' is appended on the span tag. That works all fine an dandy.

But what I want it to do is check if an input element has passed validation and if it has then append the classes 'control-group success' on the span element.

But obviously on page load, because no errors were returned it will append 'control-group success'.

How would I make it so that it only does that after the element has gone through validation?

Or can somebody suggest another method of doing this?
#2

[eluser]achilleusrage[/eluser]
One thing you could do is check for some other input (a hidden field) that flags if your form has been submitted. Then, if that flag exists, do your if your check to determine which CSS class to show.
#3

[eluser]Samus[/eluser]
[quote author="achilleusrage" date="1331257658"]One thing you could do is check for some other input (a hidden field) that flags if your form has been submitted. Then, if that flag exists, do your if your check to determine which CSS class to show.[/quote]
Now that you mention that, I just thought of running a check to see if anything was posted before going on to do the validation check.

Thanks!

Updated code in case anybody has a similar issue:

Code:
<span class="formf &lt;?= ($this-&gt;input-&gt;post() ? (form_error(('content')) ? "control-group error" : "control-group success") : ""); ?&gt;">    
    &lt;?=form_label('Content', 'fcontent');?&gt;
    &lt;?= form_error('content', '<div class="error_message">', '</div>'); ?&gt;
    &lt;?=form_textarea(array('name' => 'content', 'value' => set_value('content'), 'id' => 'fcontent', 'rows' => 3, 'title' => 'Content shit here'));?&gt;
</span>




Theme © iAndrew 2016 - Forum software by © MyBB