Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add support for per field/rule error messages to the Form Validation Library #1252

Closed
wants to merge 4 commits into from

3 participants

@jbottigliero

This commit gives the ability to pass an associative array of error
message as the fourth parameter of the set_rules method. This adds a
simple way to pass field/input specific error messages rather than rule
specific messages that set_messsage provides.

@jbottigliero jbottigliero Add support for 'flash error messages' to the Form Validation Library
This commit gives the ability to pass an associative array of error
message as the third parameter of the set_rules method. This adds a
simple way to pass field/input specific error messages rather than rule
specific messages that set_messsage provides.
03f52d2
jbottigliero added some commits
@jbottigliero jbottigliero Revert "Add support for 'flash error messages' to the Form Validation…
… Library"

This reverts commit 03f52d2.
74764a6
@jbottigliero jbottigliero Add support for field specific Form Validation error messages
This commit seems to make more sense, and is a bit more elegant. Adds a
third parameter to the set_message method of the Form Validation
library which will allow the ability to set field specific error
messages "on the fly"!
b9a7876
@jbottigliero jbottigliero reopened this
@jbottigliero

The second set of commits moves this functionality from the set_rules method to the set_messages method which makes more sense when reading the code.


$this->load->library('form_validation');

$this->form_validation->set_rules('name', 'name', 'required');
$this->form_validation->set_rules('email', 'email', 'required');
$this->form_validation->set_message(
  'required', 
  'Hey, we need to know what to call you by!', 
  'name'
);
system/libraries/Form_validation.php
((6 lines not shown))
{
+ if($name){
@cryode
cryode added a note

Should always do more specific IF checks than this. For instance, '0' will be interpreted as false, even though it's a valid string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@narfbg
Owner

Really outdated and given the lack of comments - most likely not that useful, I can't think of a use case for it.

@narfbg narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 12, 2012
  1. @jbottigliero

    Add support for 'flash error messages' to the Form Validation Library

    jbottigliero authored
    This commit gives the ability to pass an associative array of error
    message as the third parameter of the set_rules method. This adds a
    simple way to pass field/input specific error messages rather than rule
    specific messages that set_messsage provides.
  2. @jbottigliero
  3. @jbottigliero

    Add support for field specific Form Validation error messages

    jbottigliero authored
    This commit seems to make more sense, and is a bit more elegant. Adds a
    third parameter to the set_message method of the Form Validation
    library which will allow the ability to set field specific error
    messages "on the fly"!
Commits on Apr 14, 2012
  1. @jbottigliero
This page is out of date. Refresh to see the latest.
Showing with 17 additions and 5 deletions.
  1. +17 −5 system/libraries/Form_validation.php
View
22 system/libraries/Form_validation.php
@@ -204,8 +204,12 @@ public function set_data($data = '')
* @param string
* @return object
*/
- public function set_message($lang, $val = '')
+ public function set_message($lang, $val = '', $name = FALSE)
{
+ if($name !== FALSE){
+ $lang = $name.'.'.$lang;
+ }
+
if ( ! is_array($lang))
{
$lang = array($lang => $val);
@@ -527,17 +531,21 @@ protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
// Set the message type
$type = in_array('required', $rules) ? 'required' : 'isset';
- if ( ! isset($this->_error_messages[$type]))
+ if ( ! isset($this->_error_messages[$type]) && ! isset($this->_error_messages[$row['field'].'.'.$type]))
{
if (FALSE === ($line = $this->CI->lang->line($type)))
{
$line = 'The field was not set';
}
}
- else
+ else if (! isset($this->_error_messages[$row['field'].'.'.$type]))
{
$line = $this->_error_messages[$type];
}
+ else
+ {
+ $line = $this->_error_messages[$row['field'].'.'.$type];
+ }
// Build the error message
$message = sprintf($line, $this->_translate_fieldname($row['label']));
@@ -667,17 +675,21 @@ protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
// Did the rule test negatively? If so, grab the error.
if ($result === FALSE)
{
- if ( ! isset($this->_error_messages[$rule]))
+ if ( ! isset($this->_error_messages[$rule]) && ! isset($this->_error_messages[$row['field'].'.'.$rule]))
{
if (FALSE === ($line = $this->CI->lang->line($rule)))
{
$line = 'Unable to access an error message corresponding to your field name.';
}
}
- else
+ elseif (! isset($this->_error_messages[$row['field'].'.'.$rule]))
{
$line = $this->_error_messages[$rule];
}
+ else
+ {
+ $line = $this->_error_messages[$row['field'].'.'.$rule];
+ }
// Is the parameter we are inserting into the error message the name
// of another field? If so we need to grab its "field label"
Something went wrong with that request. Please try again.