[eluser]mdcode[/eluser]
I appear to be having another "stupid" day, and I am unabel to get form validation running. Here is the code of my controller:
Code: function add()
{
$this->auth->restrict(3);
$this->load->library('form_validation');
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('description','Description','required');
$data['site_title'] = $this->config->item('site_title');
$data['title'] = 'Add a project';
/* grab the customer list */
if ($customers = $this->projects_model->get_all_customers())
$data['customers'][$value['id']=0] = '';
{
foreach ($customers as $key=>$value)
$data['customers'][$value['id']] = $value['name'];
}
/* grab the division list */
if ($divisions = $this->projects_model->get_all_divisions())
$data['divisions'][$value['id']=0] = '';
{
foreach ($divisions as $key=>$value)
$data['divisions'][$value['id']] = $value['name'];
}
// ...more form fields...
/* check if the form has been submitted */
if($this->input->post('submit_form'))
{
$insert = $this->projects_model->insert();
if ($this->db->affected_rows() > 0)
{
$this->session->set_flashdata('flashmsg', '<div class="flash_green">The project has been added successfully.</div>');
redirect('/projects', 'refresh');
}
else
{
$this->session->set_flashdata('flashmsg', '<div class="flash_red">Project addition has failed. Please try again.</div>');
redirect('/projects', 'refresh');
}
}
if ($this->form_validation->run() == FALSE)
{
$this->template->write_view('content', 'default/admin/projects_add', $data);
}
else
{
$this->template->write_view('content', 'default/admin/projects_add', $data);
}
$this->template->render();
}
My model:
Code: /* inserting the project data */
function insert()
{
$data = array(
'customer'=>$this->input->post('customer', TRUE),
'division'=>$this->input->post('division', TRUE),
'dept'=>$this->input->post('dept', TRUE),
'state_code'=>$this->input->post('state', TRUE),
// ...yada yada yada
And another except from my view:
Code: <?php echo validation_errors(); ?>
<span class="fontTitle">Add a project to the Projects Database</span>
<?=br(2);?>
<span class="fontNormal">Use the following form to add a project to the database.<?php echo br(2);?>
<?php echo form_open('projects/add'); ?>
<?php echo form_fieldset('Main Details'); ?>
<table class="table100">
<tr>
<td width="10%">Customer</td>
<td width="40%"><?php echo form_dropdown('customer', $customers, '', $dds); ?></td>
<td width="10%">Division</td>
<td width="40%"><?php echo form_dropdown('division', $divisions, '', $dds); ?></td>
</tr>
As you can see from the validation rules in the controller, I have set the title and decription fields (ordinary text fields) to be required, but on submitting the form, a record is inserted into the database. If anyone can see anything blindingly obvious, I'd appreciate a response. Thanks muchly, always impressed with the community here.
[eluser]Thorpe Obazee[/eluser]
[quote author="mdcode" date="1246519569"]I appear to be having another "stupid" day, and I am unabel to get form validation running. Here is the code of my controller:
Code: if($this->input->post('submit_form'))
{
$insert = $this->projects_model->insert();
if ($this->db->affected_rows() > 0)
{
$this->session->set_flashdata('flashmsg', '<div class="flash_green">The project has been added successfully.</div>');
redirect('/projects', 'refresh');
}
else
{
$this->session->set_flashdata('flashmsg', '<div class="flash_red">Project addition has failed. Please try again.</div>');
redirect('/projects', 'refresh');
}
}
[/quote]
I think this is inserting data without getting to the part where you run the validation.
Code: function add()
{
$this->auth->restrict(3);
$this->load->library('form_validation');
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('description','Description','required');
$data['site_title'] = $this->config->item('site_title');
$data['title'] = 'Add a project';
/* grab the customer list */
if ($customers = $this->projects_model->get_all_customers())
$data['customers'][$value['id']=0] = '';
{
foreach ($customers as $key=>$value)
$data['customers'][$value['id']] = $value['name'];
}
/* grab the division list */
if ($divisions = $this->projects_model->get_all_divisions())
$data['divisions'][$value['id']=0] = '';
{
foreach ($divisions as $key=>$value)
$data['divisions'][$value['id']] = $value['name'];
}
// ...more form fields...
/* check if the form has been submitted */
if($this->input->post('submit_form'))
{
$insert = $this->projects_model->insert();
if ($this->db->affected_rows() > 0)
{
$this->session->set_flashdata('flashmsg', '<div class="flash_green">The project has been added successfully.</div>');
redirect('/projects', 'refresh');
}
else
{
$this->session->set_flashdata('flashmsg', '<div class="flash_red">Project addition has failed. Please try again.</div>');
redirect('/projects', 'refresh');
}
}
if ($this->form_validation->run() == FALSE)
{
$this->template->write_view('content', 'default/admin/projects_add', $data);
}
else
{
$this->template->write_view('content', 'default/admin/projects_add', $data);
}
$this->template->render();
}
[eluser]mdcode[/eluser]
Ah... nice idea, thanks, but I have just swapped around the validation stuff with the inserting stuff with no effect. Hopefully you have more ideas?
[eluser]Thorpe Obazee[/eluser]
You should post how you swapped around the validation stuff...
[eluser]mdcode[/eluser]
Sorry about that...
Code: if ($this->form_validation->run() == FALSE)
{
$this->template->write_view('content', 'default/admin/projects_add', $data);
}
else
{
$this->template->write_view('content', 'default/admin/projects_add', $data);
}
/* check if the form has been submitted */
if($this->input->post('submit_form'))
{
$insert = $this->projects_model->insert();
if ($this->db->affected_rows() === 1)
{
$this->session->set_flashdata('flashmsg', '<div class="flash_green">The project has been added successfully.</div>');
redirect('/projects', 'refresh');
}
else
{
$this->session->set_flashdata('flashmsg', '<div class="flash_red">Project addition has failed. Please try again.</div>');
redirect('/projects/add', 'refresh');
}
}
$this->template->render();
[eluser]Thorpe Obazee[/eluser]
[quote author="mdcode" date="1246521894"]Sorry about that...
Code: if ($this->form_validation->run() == FALSE)
{
$this->template->write_view('content', 'default/admin/projects_add', $data);
}
else
{
$this->template->write_view('content', 'default/admin/projects_add', $data);
}
/* check if the form has been submitted */
if($this->input->post('submit_form'))
{
$insert = $this->projects_model->insert();
if ($this->db->affected_rows() === 1)
{
$this->session->set_flashdata('flashmsg', '<div class="flash_green">The project has been added successfully.</div>');
redirect('/projects', 'refresh');
}
else
{
$this->session->set_flashdata('flashmsg', '<div class="flash_red">Project addition has failed. Please try again.</div>');
redirect('/projects/add', 'refresh');
}
}
$this->template->render();
[/quote]
Errmm... you're still inserting... now after the form validation has run................................
Check your curly braces.
comment: you should not even be checking if the form was submitted.
hint:
Code: if ($this->form_validation->run() == FALSE)
{
$this->template->write_view('content', 'default/admin/projects_add', $data);
}
else
{
$this->template->write_view('content', 'default/admin/projects_add', $data);
}
You should be inserting something in the second part of the conditional statement.
[eluser]mdcode[/eluser]
Then I'm a little confused -- if I don't check if the form has been submitted, it runs in an endless loop.
[eluser]Thorpe Obazee[/eluser]
[quote author="mdcode" date="1246522625"]Then I'm a little confused -- if I don't check if the form has been submitted, it runs in an endless loop.[/quote]
The form validation library checks that for you here (line 279).
Code: // Do we even have any data to process? Mm?
if (count($_POST) == 0)
{
return FALSE;
}
Anyway... You should have something along these lines.
Code: if ($this->form_validation->run() == FALSE)
{
$this->template->write_view('content', 'default/admin/projects_add', $data);
}
else
{
$insert = $this->projects_model->insert();
if ($this->db->affected_rows() === 1)
{
$this->session->set_flashdata('flashmsg', '<div class="flash_green">The project has been added successfully.</div>');
redirect('/projects', 'refresh');
}
else
{
$this->session->set_flashdata('flashmsg', '<div class="flash_red">Project addition has failed. Please try again.</div>');
redirect('/projects/add', 'refresh');
}
}
$this->template->render();
[eluser]mdcode[/eluser]
I didn't realize that, thank you! The validation appears to be working now but the errors are not showing on the page. Not sure why that is yet.
|