• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
$this->validation->error_string VS validation_errors()

#1
[eluser]hendrik[/eluser]
In older versions of CI for my login I used $this->validation->error_string to set a custom message when a username + password is in not the db.

Eg. $this->validation->error_string = "Your username and/or password are incorrect.";

In CI 1.7, how do I set a custom error message that is displayed with validation_errors();

Thanks!

#2
[eluser]JaRoLLz[/eluser]
Excerpt from the user-guide for Form Validation
============
Setting Error Messages

All of the native error messages are located in the following language file: language/english/form_validation_lang.php

To set your own custom message you can either edit that file, or use the following function:
Code:
$this->form_validation->set_message('rule', 'Error Message');

Where rule corresponds to the name of a particular rule, and Error Message is the text you would like displayed.

If you include %s in your error string, it will be replaced with the "human" name you used for your field when you set your rules.

In the "callback" example above, the error message was set by passing the name of the function:
Code:
$this->form_validation->set_message('username_check')

You can also override any error message found in the language file. For example, to change the message for the "required" rule you will do this:
Code:
$this->form_validation->set_message('required', 'Your custom message here');
========

#3
[eluser]hendrik[/eluser]
But is there any way to assign a custom string (error message) to be displayed when
1) I'm not using a callback to get a message
2) and there is no such rule specified

#4
[eluser]hendrik[/eluser]
I've found a solution that works for me. The error message is appended to the array that is returned in validation_errors();

Code:
array_push($this->form_validation->_error_array, "My custom message that is unrelated to any rule");

#5
[eluser]bretticus[/eluser]
[quote author="hendrik" date="1233682938"]I've found a solution that works for me. The error message is appended to the array that is returned in validation_errors();

Code:
array_push($this->form_validation->_error_array, "My custom message that is unrelated to any rule");
[/quote]

Sorry for hijacking this post...

I will more than likely do this. I think it's great that this was so easy to find in the forum. However, I was just wondering if you found a more standard way. I wanted to take a month, day, and year from three drop-downs and check for a valid date. Ant then hook an error into the error array (which is exactly what you do!) Ha. Oh well, I had to ask anyway.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.