Welcome Guest, Not a member yet? Register   Sign In
Form Validation Issue
#1

[eluser]alejandronanez[/eluser]
Hello I've been trying to do this guys:

I got 2 fields in 1 form, the max_length for one is ' 3 ' and for the other is ' 25 ' .

I did this.

Code:
$max1 = 3;
$this->form_validation->set_rules('field1','Field 1',"trim|required|max_length[$max1]|xss_clean");
$this->form_validation->set_message("max_length","%s max length of must be $max1");

$max2 = 25;
$this->form_validation->set_rules('field2','Field 2',"trim|required|max_length[$max2]|xss_clean");
$this->form_validation->set_message("max_length","%s max length must be $max2");

The problem is when I write more than 3 chars in FIELD 1 the system says

Code:
Field 1 max length must be 25

And I want this

Code:
Field 1 max length must be 3

Thanks for helping me.

Smile
#2

[eluser]slowgary[/eluser]
Strange. Have you tried replacing your variables with the actual numbers?
#3

[eluser]alejandronanez[/eluser]
Yep I tried but it doesn't work Sad
I'm still getting the same message.

Code:
Field 1 max length must be 25
#4

[eluser]devbro[/eluser]
that won't work. the problem with validation class is that error messages are per function, not field-function pair.

I believe there was a class to achieve this but cannot recall. worst case senario try to port kohana's validation class.
#5

[eluser]alejandronanez[/eluser]
Ahh... :S I have to take a look to Kohana's =( #Fail any other suggestion?
#6

[eluser]devbro[/eluser]
I remember a modification of the form_validation class that did field-function pair error message. search in forum and u might be able to find it.

however, I suggest that you try to add the functionaly by extending the current form_validation class.
#7

[eluser]LuckyFella73[/eluser]
As I understand the user guide, you overwrite the error message
of the "max_lenght" rule and therefore get this error.

I think this line:
Code:
$this->form_validation->set_message("max_length","%s max length must be $max2");

Is not meant to define the error message for "field2" but more to define
the error message for the max_length rule in general.

Try to implement this lines in your form to check if your rules are working:
Code:
<?php echo form_error('field1'); ?>

and
Code:
<?php echo form_error('field2'); ?>

Maybe you better display which field has how many max-chars in your form
and set the error message more flexible like "Please notice the max values
for the formfields like displayed next to the title .."

For english is not my native language it's a bit hard to explain what I mean
(as you can see by reading my "flexible text suggestion") but I hope you understand
what I'm trying to say ...
#8

[eluser]kenjis[/eluser]
How about this?

Code:
$this->form_validation->set_message("max_length", "%s max length must be %s");
#9

[eluser]LuckyFella73[/eluser]
[quote author="Kenji @ CodeIgniter Users Group in Japan" date="1266949951"]How about this?

Code:
$this->form_validation->set_message("max_length", "%s max length must be %s");
[/quote]

That should produce an error message like:
Field 1 max length must be Field1

Or am I missing something?




Theme © iAndrew 2016 - Forum software by © MyBB