Welcome Guest, Not a member yet? Register   Sign In
Form validation and error messages
#1

[eluser]AtlantixMedia[/eluser]
Hello,

I'm not clear on how CI validation works yet. Meaning...is there a way to return an error msg based on the actual error which occured? for example:

$rules['username'] = "required|min_length[3]|max_length[15]";
$this->validation->set_rules($rules);
etc

if the input passes all but the min_length test, does the system say that? ideally, I would like to be able to return a personalized msg based on each error. I guess I will have to write this from scratch.

someone please confirm this. thanks you
#2

[eluser]gtech[/eluser]
have you followed through all the examples in the validation documentation as the answers to your questions are in there.

yes the min_length will actually say that..

"The %s field must be at least %s characters in length.";

NOTE: you can set the custom error message

Code:
$this->validation->set_message('required', 'Error %s is required honest guv');

if you use set_fields

Code:
$fields['username']    = 'Username';
    $fields['password']    = 'Password';
    $fields['passconf']    = 'Password Confirmation';
    $fields['email']    = 'Email Address';

    $this->validation->set_fields($fields);
then the %s bit will be replaced with the value you set $fields[<variable name>] to, instead of the variable name eg .. the passconf field will display

Error Password Confirmations is required honest guv.

instead of

Error passconf is required honest guv.


also have a look in

system\language\english\validation_lang.php this is where the default error messages are stored.
#3

[eluser]LuckyFella73[/eluser]
Hi,

as far as I know there is no implemented way of
checking which part of your rule-segments didn't
match. What you can do is to personalize the
error messages in general (for each rule).

have a look at gtech's example (posted while
I was writing this post Wink
#4

[eluser]gtech[/eluser]
also might me worth reading the "showing errors individually" section of the validation documentation.
#5

[eluser]AtlantixMedia[/eluser]
thanks everybody for the heads up. basically, I intend to pass multiple rules, starting to test the most strict one. for example,

required|min_length[3]|max_length[8]

I test whether the input is null

1) if it is no more tests are done and a msg like 'sorry this field is required' is sent back.
2) if it's not, input is tested for min_lenght and so on until either it fails or passes all tests.

also, I would have to test a few rules which are not built in CI.

can this be implemented using CI validation library as is? thanks
#6

[eluser]gtech[/eluser]
[quote author="AtlantixMedia" date="1197504563"]thanks everybody for the heads up. basically, I intend to pass multiple rules, starting to test the most strict one. for example,

required|min_length[3]|max_length[8]

I test whether the input is null

1) if it is no more tests are done and a msg like 'sorry this field is required' is sent back.
2) if it's not, input is tested for min_lenght and so on until either it fails or passes all tests.
[/quote]
yep thats what happens I believe.


Quote:also, I would have to test a few rules which are not built in CI.

can this be implemented using CI validation library as is? thanks

yes read the documentation e.g. the bit that says "Callbacks: Your own Validation Functions"

sometimes the best thing to do is give it a go, cut and paste the examples.. test what happens and then if you get problems ask on the forum Smile. good luck!

Link to [url="http://ellislab.com/codeigniter/user-guide/libraries/validation.html"][Validation Docs][/url]




Theme © iAndrew 2016 - Forum software by © MyBB