Welcome Guest, Not a member yet? Register   Sign In
Getting validation message from Model to view
#1

[eluser]Hermann007[/eluser]
Hi guys and girls,

I'm busy with my first codeigniter "practice" project. I've used flexi_auth as a validation library and really liked the way that they implement the form validation in the model and not in the controller, thus sticking to MVC(Fat models) principal.

I'm trying to build another one of my forms with the same logic but for the life of me I can't seem to get the error message set by validation errors from the model via the controller to the view. Please note I'm using a layout helper, thus the abnormal call to view.

Any help will be appreciated. Here is my code:

Controller:

//Start Information Desk Query()
public function information()
{
if ($this->input->post('informationForm_submit'))
{
$this->load->model('contact_model');
if ($this->contact_model->information_post())
// information_post validation passed
{

}
// information_post validation failed
else
{
$this->data['title'] = 'Information Desk';
$this->layout->show('contact/information_view', $this->data);
}
}

}
//End Information Desk Query()




Model:

//Start Information Request()
public function information_post()
{

$validation_rules = array(
array('field' => 'informationEmail', 'label' => 'Email', 'rules' => 'required|valid_email'),
array('field' => 'informationQuery', 'label' => 'Message', 'rules' => 'required|max_length[500]')
);

$this->form_validation->set_rules($validation_rules);

if ($this->form_validation->run())
{
return TRUE;
}
else
{
$this->data['message'] = validation_errors('<li class="error_msg">', '</li>');
return FALSE;
}

}
//End Information Request()




View:

&lt;?php

if (! empty($message))
{
echo '<div class = "error_message">
<div class ="error_header">Please correct the following</div>
<div class = "error_image my-icons-Actions-window-close-icon"></div>
<ul class = "error_text">'.$message.'</ul></div>';
}
?&gt;




****Please note that the form validation is functional in this code. If I put the following:
echo $this->data['message'];

....in the model then the errors are displayed but obviously not as a part of my view file but before it.
PS. please take it easy if this is a stupid question but I am a newbie to codeigniter and actually to MVC.
#2

[eluser]Pert[/eluser]
Instead of returning false, return error message as non-boolean variable. It can be string or it could be array of errors.

Then in controller do this:

Code:
$result = $this->contact_model->information_post();

if ($result === true)
{
   // validation passed
}
else
{
   $this->data[‘title’] = ‘Information Desk’;
   $this->data[‘message’] = $result;
   $this->layout->show(‘contact/information_view’, $this->data);
}

PS, make sure to use === instead of ==
#3

[eluser]Hermann007[/eluser]
Thank you Pert,

I have figured this out since the post but your solution makes sense. What I have done is set the message in the model and then call it in the controller like this : $this->data['message'] = $this->Contact_Model->data['message'];
#4

[eluser]Pert[/eluser]
[quote author="Hermann007" date="1370174046"]$this->data['message'] = $this->Contact_Model->data['message'];[/quote]

Yep, that will work as well - glad to see you managed to figure it out Smile




Theme © iAndrew 2016 - Forum software by © MyBB