0
votes

I have three forms in a page. Say, one for login, one for register and one for forgot password. All these form actions and the same. During the submit, submitted form is identified by a hidden input value.

echo form_hidden('formname', 'registration');
echo form_hidden('formname', 'login');
echo form_hidden('formname', 'assistance');

Every form have set of validation rules such as valid_email, min_length etc. and errors are thrown if invalid submission. But the problem is, when errors are thrown it is shown all above the three forms. Because every form have validation_errors() above it. And I cannot have single validation_errors in my page.

So is there any way I can place the error messages corresponding to login form in a <div> which ID is login-errors and registration form's errors in <div> which ID is registration-errors etc.?

<div id='registration-errors'>
<?php
validation_errors(); // For error messages of registration form
echo form_open('account', array('method'=>"POST", 'autocomplete'=>"off", 'class'=>'user-register'));
// Hidden input + Other inputs and labels + Submit button
?>
</div>


<div id='login-errors'>
<?php
validation_errors(); // For error messages of registration form
echo form_open('account', array('method'=>"POST", 'autocomplete'=>"off", 'class'=>'user-login'));
// Hidden input + Other inputs and labels + Submit button
?>
</div>

<div id='assistance-errors'>
<?php
validation_errors(); // For error messages of forgot password form
echo form_open('account', array('method'=>"POST", 'autocomplete'=>"off", 'class'=>'user-assistance'));
// Hidden input + Other inputs and labels + Submit button
?>
</div>
3
you have hidden and submit button on each form seperate? - Rakesh Sharma

3 Answers

1
votes

Define your validation errors in your controller.

In the view

<?php if(isset($registration_errors)){
     echo $registration_errors;
}

echo form_open('account', array('method'=>"POST", 'autocomplete'=>"off", 'class'=>'user-register'));
// Hidden input + Other inputs and labels + Submit button
?>

<?php if(isset($login_errors)){    
     echo $login_errors;
}

echo form_open('account', array('method'=>"POST", 'autocomplete'=>"off", 'class'=>'user-login'));
// Hidden input + Other inputs and labels + Submit button
?>


<?php if(isset($assistance_errors)){    
     echo $assistance_errors;
}
echo form_open('account', array('method'=>"POST", 'autocomplete'=>"off", 'class'=>'user-assistance'));
// Hidden input + Other inputs and labels + Submit button
?>

In the controller where you form validation takes place set errors based on which submit button is hit.

So if, for example, the Registration submit button is hit:

     if(isset($_POST['registration']){ //or whatever the name of the relevant input/submit value 

          if(!$this->form_validation->run()) {
              $vars['registration_errors'] = validation_errors();
          }else{
             //success stuff
          }
      }
1
votes

This is cause you have echoing validation_errors() on all forms so it always true if any validation error exist.

try to use :-

form_error('fieldname');

or send back a unique identifier to view from your controller:-

$this->data['register'] = $this->input->post('registration');
$this->data['login'] = $this->input->post('login');
$this->data['assistance'] = $this->input->post('assistance');
$this->load->view('myview', $this->data);

then check on your view:-

if(!empty($register))
echo validation_errors();

if(!empty($login))
echo validation_errors();

if(!empty($assistance))
echo validation_errors();

send some values in your all three hidden so it will be exist one value on one submit

0
votes

you can simlply do this if you are using validation_error() function

echo validation_errors('<div class="alert alert-danger">','</div>');

will show all errors in Bootstrap alert panel