Welcome Guest, Not a member yet? Register   Sign In
Form Validation - Global error messages.
#1

[eluser]iNfLuX[/eluser]
Hi,
Spent a lot of time on this issue, probably I am missing something.

How can i set a global error message for a specific form ?

for example the case when someone try to log into the system and submit invalid username or password.

as i saw in the user guide the way to set custom messages is writing a callback method that will handle the check of a specific form field. but it's not my case cause I am trying to validate not only the username field.

flashdata and view parameters is not what iam looking for.
#2

[eluser]TheFuzzy0ne[/eluser]
I'm not sure what you mean by "global". Surely you only have a single function handle the validation, right? If so, then that's where the validation messages should be set.
#3

[eluser]Thorpe Obazee[/eluser]
Yeah, still thinking about that 'global' word. Maybe you could explain a bit beter or show us some code or pseudo code of what you actually want.
#4

[eluser]bretticus[/eluser]
[quote author="iNfLuX" date="1240363613"]
How can i set a global error message for a specific form ?...

but it's not my case cause I am trying to validate not only the username field.[/quote]

Perhaps, you are having the issue that I am just addressing. I have 3 drop-downs for a date (month, day, and year.) Callback could work for this I suppose, I could have just referenced the 3 input fields in the callback method but I wanted a way to just inject an arbitrary error into Form Validation so that $this->form_validation->run() catches it as well. I decided to extend the Form_validation class with a very simple new method:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*** Brett: Thu Apr 23 16:48:00 GMT 2009
* Extension for adding arbitrary error messages  ***/


class MY_Form_validation extends CI_Form_validation {
    
    function MY_Form_validation($rules=array())
    {
        parent::CI_Form_validation($rules);
    }
    
    function create_arbitrary_error($alias, $message) {
        // Save the error message
        $this->_field_data[$alias]['error'] = $message;
        if ( ! isset($this->_error_array[$alias]))
        {
            $this->_error_array[$alias] = $message;
        }
    }    
}
?>

This seems to do the trick. That is, the $this->form_validation->run() fails if I call this method, and the form_error() function works for showing the error right where I want to on my form.

For example, I call this BEFORE calling $this->form_validation->run() :

Code:
if ( checkdate(
    $this->input->post('birthdate_month', TRUE),
    $this->input->post('birthdate_day', TRUE),
    $this->input->post('birthdate_year', TRUE)
    ) === FALSE )
{            
       $this->form_validation->create_arbitrary_error('birthdate', 'Date of Birth is invalid');
}

In my view, I simply have...

Code:
<p>
<label><strong>Birthdate:</strong> &lt;?php echo form_dropdown('birthdate_month', getmonths(), set_value('birthdate_month')); ?&gt; </label>
<label> &lt;?php echo form_dropdown('birthdate_day', array_merge(array('Day'), range(1, 31)), set_value('birthdate_day')); ?&gt; </label>
<label> &lt;?php echo form_dropdown('birthdate_year', array_merge(array('Year'), array_combine(range(1900, date('Y')), range(1900, date('Y')))), set_value('birthdate_year')); ?&gt; </label>
&lt;?php echo form_error('birthdate'); ?&gt;</p>

I am somewhat new to CI. However, this is working for me, but I would love to hear suggestions on how others have handled similar scenarios (excluding the suggestion that I should have just one date field instead of three drop-downs. I actually may go that route, but there are still other circumstances where something like I have done may be necessary. Surely, form validation rules can't address every scenario where you validate multiple related fields?)
#5

[eluser]Moobies[/eluser]
I think I understand what you meant. You have a sign-in form with email and password. You use CI form_validation to validate the validity of the email and password inputs, and write out field-specific errors just fine. But you want to know how CI helps you write out errors relating to logic, e.g. in this case "the email and password did not match, please check your details and try again". This is not a field-specific error but a form-specific (global as you call it) error, and usually you would put out an error message at the top of the form for this type of failure.

I think CI only handles field-specific errors, so you have to add your own logic-level error to the $data array passed to the view.

So I solve this with a custom helper function and the normal view data array

Helper

Code:
function formErrors($errors = null) {
        
        if (is_null($errors)) {
            return;    
        }
        
        $prefix = '<p class="error">';
        $suffix = '</p>';
        
        if (! is_array($errors)) {
            return $prefix . $errors . $suffix;                
        }    

        $err = $prefix . '<ul>';
        foreach ($errors as $error) {
            $err .= "<li>{$err}</li>";
        }
        return $err . '</ul>' . $suffix;
    }

Controller

Code:
if (! $this->mylib->authenticates($email, $password)) {
  $data['errors'] = "The email and password were not recognised, please try again.";
}

return $this->load->view('signin', $data);

View

Code:
&lt;?php echo formErrors($errors) ?&gt;

HTH




Theme © iAndrew 2016 - Forum software by © MyBB