Amit Patil
Amit Patil

Reputation: 3067

codeigniter print form specific validation error

I have 2 forms on a page eg: 'create_user' and 'update_user' i want to show validation errors specific to each form just above the form. i mean, i want to show validation errors for 'create_user' form just above it and same for 'update_user'.

Right now if i submit update, form errors are showing above both of the forms. is there something like

<? echo validation_errors("create_user");   ?>

Upvotes: 1

Views: 3590

Answers (2)

landons
landons

Reputation: 9547

Simplest way is this:

  1. Pass some sort of form-identifying hidden field with each form
  2. Pass that same hidden field to the view error state, and use a basic IF condition to run validation_errors() in the correct location

You could probably extend the validation class somehow to do what you're asking, but it would be a very specific implementation, and hardly useful in a framework (which is why CI doesn't "know how" to do this by default). The added complexity of working with the extended class would likely counteract any benefit of not simply using basic logic in the view.

Upvotes: 2

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

You could do something like:


//pass validation_errors as data to your view from your controller, like
//in your controller
if ($this->form_validation->run()) {  //form create_user form
  //process
}
else {
   $data['create_user_form_errors'] = validation_errors();
}
//and for second form update_user
if ($this->form_validation->run()) {  //form create_user form
  //process
}
else {
   $data['update_user_form_errors'] = validation_errors();
}

//then in your view
if (isset($create_user_form_errors)) echo $create_user_form_errors; //show above first form
//and
if (isset($update_user_form_errors)) echo $update_user_form_errors; //show above second form

Something like that should work for showing errors based on multiple forms. Hope it helps

Upvotes: 4

Related Questions