Welcome Guest, Not a member yet? Register   Sign In
Form validation variables...
#1

[eluser]newsun[/eluser]
So I've been working with the form_validation library in CI2 and I like that I can use %s to get the human readable data, but what about just the the actual field name.

For example, let's say I wanted to do something like this:

Code:
$this->form_validation->set_rules('phone', 'Phone', 'trim|required');
$this->form_validation->set_message('required', '<style>input[name=XXX] {border: 1px solid #a00; background-color: #faa;}</style>%s is Required');

Where XXX is the field name I want, not the human readable one. This would net me an inserted style applicable to the proper field and a readable error all in one line.
#2

[eluser]Aken[/eluser]
You'd have to extend CodeIgniter to include this functionality; it is not built in.
#3

[eluser]Samus[/eluser]
[quote author="Aken" date="1333774184"]You'd have to extend CodeIgniter to include this functionality; it is not built in.[/quote]
Not necessarily.

This can be done quite simply:

e.g
Code:
$this->form_validation->set_rules('phone', 'Phone', 'trim|required');
$this->form_validation->set_message('required', '%s is Required');

And then in your view

Code:
<span &lt;?php echo ($this->input->post() ? (form_error(('phone')) ? "&lt;style&gt;input[name='phone'] {border: 1px solid #a00; background-color: #faa;}&lt;/style&gt;" : "") : ""); ?&gt;"></span>

Checks to see if there is any POST data. If there is, it checks to see if there is an error for the 'phone' field, if there is an error it adds the required style.
#4

[eluser]newsun[/eluser]
Samus, this works and is what we already have in place more or less. Only thing is it requires a line item for the style of each field, where I should be able to do it more automatically if only I had access to the field name I use when setting up the rule in the first place.

[quote author="Samus" date="1333797184"][quote author="Aken" date="1333774184"]You'd have to extend CodeIgniter to include this functionality; it is not built in.[/quote]
Not necessarily.

This can be done quite simply:

e.g
Code:
$this->form_validation->set_rules('phone', 'Phone', 'trim|required');
$this->form_validation->set_message('required', '%s is Required');

And then in your view

Code:
<span &lt;?php echo ($this->input->post() ? (form_error(('phone')) ? "&lt;style&gt;input[name='phone'] {border: 1px solid #a00; background-color: #faa;}&lt;/style&gt;" : "") : ""); ?&gt;"></span>

Checks to see if there is any POST data. If there is, it checks to see if there is an error for the 'phone' field, if there is an error it adds the required style.
[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB