Welcome Guest, Not a member yet? Register   Sign In
form_error() won't work!
#11

[eluser]TheFuzzy0ne[/eluser]
You wouldn't. You'd just load the view again, and you keep reloading the view whilst the validation is failing. When the validation passes, then you do the redirect.

./system/application/controllers/validation_example.php
Code:
class Validation_example extends Controller {
    
    function index()
    {
        $this->load->library('form_validation');
        
        if ($this->input->post('submit') && $this->_run_validation())
        {
            die('The validation has passed, so we redirect');
        }

        # Load the view as the form is either not being submitted, or validation hasn't passed.
        $this->load->view('validation_example');
    }
    
    function _run_validation()
    {
        $this->form_validation->set_error_delimiters('', '');
        
        $this->form_validation->set_rules(
                'text_input',
                'Text Input',
                'required|alpha'
            );
            
        return $this->form_validation->run();
    }
    
}

./system/application/views/validation_example.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
    &lt;head&gt;
        &lt;title&gt;Form Validation Example&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form action="/validation_example" method="post"&gt;
            <div>
                <label>Text Input:
                    <span style="color:red; font-weight: bold;">&lt;?php echo form_error('text_input'); ?&gt;</span><br />
                    &lt;input type="text" name="text_input" value="" /&gt;
                </label>
                
            </div>
            <div>
                &lt;input type="submit" name="submit" value="Validate" /&gt;
            </div>
        &lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;
#12

[eluser]IamPrototype[/eluser]
I know that would work, but still this form is on my single entry page where the comments for the entry is. It's just stupid to seperate the form from the entry - much better to show the entry, comments AND form on the same page.
#13

[eluser]TheFuzzy0ne[/eluser]
Then make the following changes to the validation method:
Code:
function index()
{
    $this->load->library('form_validation');
        
    if ($this->input->post('submit') && $this->_run_validation())
    {
        redirect('entries/view/' . $this->input->post('id'));
    }

    # Instead of loading the view, just call on the original controller method.
    $this->view($this->input->post('id'));
}

This assumes that the "view" method in the controller accepts a single argument which is the id of the thread. Obviously you can change this to your liking, but I hope this shows you one way to achieve what you want to do.




Theme © iAndrew 2016 - Forum software by © MyBB