Welcome Guest, Not a member yet? Register   Sign In
Some help with flashdata and form_validation
#1

[eluser]Unknown[/eluser]
Hello all,

I am a little stuck with this one. I need a form in the footer of all of my pages. I have a shell template view that calls the form view inside of it. Since I obviously don't want to rewrite all of my validation code for every page, I have created a separate controller to handle the form. I then assumed I could set the form validation errors in the flashdata session, redirect to the previous page, and display any errors above the form..

I guess my question is.. am I setting this up wrong? Here is my controller code:
Code:
function submit()
{
    if (count($_POST) > 0)
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('company', 'Company', 'trim|xss_clean');
        $this->form_validation->set_rules('phone', 'Phone Number', 'trim|required|is_numeric|exact_length[10]');
        
        if ($this->form_validation->run())
        {
            redirect('form_success');
        }
        else
        {
            $this->session->set_flashdata('errors', $this->form_validation->error_string);
            redirect('root');
        }
    }
}

And then my view:
Code:
<?php if(validation_errors()): ?>
<div id="error-list" class="error">
    <h3>There were some errors in your submission</h3>
    <ol>
        &lt;?php echo validation_errors();?&gt;
    </ol>
</div>
&lt;?php
    endif;
    echo form_open(site_url('consultation/submit'), array('id' => 'consultation-form'));
?&gt;
<ol class="inline-labels">
    <li class="form-item">
        <label for="name">Name</label>
        &lt;?=form_input(array('name' => 'name', 'id' => 'name', 'class' => 'text-input', 'value' => set_value('name'))) . "\n"; ?&gt;
    </li>
    <li class="form-item">
        <label for="co">Company</label>
        &lt;?=form_input(array('name' => 'co', 'id' => 'co', 'class' => 'text-input', 'value' => set_value('co'))) . "\n"; ?&gt;
    </li>
    <li class="form-item">
        <label for="phone">Phone #</label>
        &lt;?=form_input(array('name' => 'phone', 'id' => 'phone', 'class' => 'text-input', 'value' => set_value('phone'))) . "\n"; ?&gt;
    </li>
    <li class="form-item clear-block">
        <label for="comments">Comments?</label>
        &lt;textarea name="comments" id="comments" rows="25" cols="25"&gt;&lt;/textarea>
    </li>
    <li class="buttons">
        &lt;?=form_submit(array('name' => 'consultation', 'class' => 'submit', 'value' => 'Woot!'));?&gt;
    </li>
</ol>
&lt;?php echo form_close(); ?&gt;

The flashdata session doesn't seem to actually get set. Is it set up so that the flashdata is dumped before it's redirecting to the root page?

Please go easy on me, I'm new to both CI and php..

Thanks,
#2

[eluser]jedd[/eluser]
Welcome to the CI forums.

Quote:The flashdata session doesn't seem to actually get set. Is it set up so that the flashdata is dumped before it's redirecting to the root page?

This is almost definitely what's happening. If you have a read of the [url="http://ellislab.com/codeigniter/user-guide/libraries/sessions.html"]CI User Manual[/url] - about two-third the way down that page it talks about how you can keep your flashdata through more than one page load.

Can you give that a go?




Theme © iAndrew 2016 - Forum software by © MyBB