Welcome Guest, Not a member yet? Register   Sign In
Custom validation error messages per rule
#11

[eluser]xwero[/eluser]
Why make it so difficult? If you want different messages for the same rule why not create aliases for the rule.
MY_Validation.php
Code:
function password($str)
{
   return $this->required($str);
}
And add the password line to the language/idiom/validation.php file.

Personally i like to add the originating rule to the function to make it easier later to see which rule is aliased, even in the language file. This makes password in the snippet required_password.
#12

[eluser]Ollie Rattue[/eluser]
[quote author="xwero" date="1219061286"]Why make it so difficult? If you want different messages for the same rule why not create aliases for the rule.
MY_Validation.php
Code:
function password($str)
{
   return $this->required($str);
}
And add the password line to the language/idiom/validation.php file.

Personally i like to add the originating rule to the function to make it easier later to see which rule is aliased, even in the language file. This makes password in the snippet required_password.[/quote]

But it isn't just different error messages per rule, it is different error messages when their are two validation rules on one field...
#13

[eluser]xwero[/eluser]
i don't see how it would be different.
Code:
$rules[’password’] = “trim|password|xss_clean|matches[passwordconfirm]”;
#14

[eluser]Ollie Rattue[/eluser]
[quote author="xwero" date="1219196037"]i don't see how it would be different.
Code:
$rules[’password’] = “trim|password|xss_clean|matches[passwordconfirm]”;
[/quote]

How would your method work if it was:

Code:
$rules[’password’] = “trim|password|xss_clean|required|matches[passwordconfirm]

And I wanted to say "You must enter a password" if they entered nothing and "Your passwords do not match" if password doesn't equal passwordconfirm?
#15

[eluser]xwero[/eluser]
Code:
// MY_Validation.php
function required_custom($str)
{
   return $this->required($str);
}

function matches_custom($str,$param)
{
   return $this->matches($str,$param);
}
// validation_lang.php
$lang['required_custom'] = “You must enter a password”;
$lang['matches_custom'] = “Your passwords do not match”;
// controller
$rules[’password’] = “trim|required_custom|xss_clean|matches_custom[passwordconfirm]”;
#16

[eluser]codex[/eluser]
[quote author="xwero" date="1219197467"]
Code:
// MY_Validation.php
function required_custom($str)
{
   return $this->required($str);
}

function matches_custom($str,$param)
{
   return $this->matches($str,$param);
}
// validation_lang.php
$lang['required_custom'] = “You must enter a password”;
$lang['matches_custom'] = “Your passwords do not match”;
// controller
$rules[’password’] = “trim|required_custom|xss_clean|matches_custom[passwordconfirm]”;
[/quote]

Maybe I just don't see the advantage in your method, but I feel my solution works better. Or better said: it works for me ;-)
#17

[eluser]codex[/eluser]
[quote author="Khoa" date="1219060156"]Hi codex, this is exactly what I'm looking for. But can you just upload the lib as a whole so that people can download, and can you also give an example of how it can be used? :-P Sorry that I'm a bit lazy, just want the quickest way to make the thing work. Thanks.[/quote]


If you want I can send you the class. Haven't looked into uploading yet, so I don't yet know how that works.

How the class works is no different from the normal way. You set the rules exactly like so
Code:
$rules['password'] = “trim|password|xss_clean|required|matches[passwordconfirm]

If there's a custom rule for this field the custom error will be shown, otherwise the default will be shown.

Code:
$lang['default']['required'] = “You must enter a something”;

$lang['password']['required'] = “You must enter a password”;
$lang['password']['matches_custom'] = “Your passwords do not match”;
#18

[eluser]xwero[/eluser]
[quote author="codex" date="1219198796"]

Maybe I just don't see the advantage in your method, but I feel my solution works better. Or better said: it works for me ;-)[/quote]
the advantage is i don't have to overwrite methods. I use what's available.

If it's used only one time it better should be done with a callback. but the code is almost the same.
Code:
// controller
$rules[’password’] = “trim|callback_required_custom|xss_clean|callback_matches_custom[passwordconfirm]”;

function required_custom($str)
{
   $this->validation->set_message('required_custom', “You must enter a password”);
   return $this->validation->required($str);
}
#19

[eluser]Khoa[/eluser]
[quote author="xwero" date="1219197467"]
Code:
// MY_Validation.php
function required_custom($str)
{
   return $this->required($str);
}

function matches_custom($str,$param)
{
   return $this->matches($str,$param);
}
// validation_lang.php
$lang['required_custom'] = “You must enter a password”;
$lang['matches_custom'] = “Your passwords do not match”;
// controller
$rules[’password’] = “trim|required_custom|xss_clean|matches_custom[passwordconfirm]”;
[/quote]

This looks good and very clean. So we basically just have 1 function + 1 lang rule per custom rule that we want to have. But just one question, if we do this, do we need to put the required_custom and matches_custom functions inside the same controller? I think it might be better to store all those functions separately (eg. in a lib), but in that case how could we put that function into the rules declaration?

Code:
$rules[’password’] = "trim|".$this->myRulesLib->required_custom."|xss_clean|".$this->myRulesLib->matches_custom[passwordconfirm]; //I doubt this work!!!

Of course, if we put it inside the same controller, it's still fine. But, just wondering.
#20

[eluser]xwero[/eluser]
The rule aliases go in the extended Validation class. This makes it possible to use them where ever you want.

That is why i added in a later response the possibility to do it as a callback of one time custom messages.

I the form only contains one required rule you can use the validation->set_message method because then you don't have to look at what the other required rules output.

The Validation class, and CI in general, gives you a lot of freedom and you should search first for solutions within the reach of CI before you start overwriting methods. It's a mistake i made, and still make now and then, too.




Theme © iAndrew 2016 - Forum software by © MyBB