Welcome Guest, Not a member yet? Register   Sign In
Form Validation RUNS first time page is loaded
#1

[eluser]Ickes[/eluser]
Trying to do something a little different (and perhaps illogical) but it isn't working...

When loading this page, form validation automatically runs and errors out. As a result, my set_value never sets the initial value. Any ideas?

Code:
$this->form_validation->set_rules('title', 'Title', 'required|min_length[5]|max_length[70]|xss_clean');

if($this->form_validation->run() == TRUE)
{
  //YES, true - if success,
  //code to update database
}

//code for form_validation->run() == FALSE
$data['title'] = $this->sample_model->_get_title();

$this->load->view('title_view', $data);

And then the "title_view"
Code:
<?php
$title = array(
        'name'        =>    'title',
        'value'        =>    set_value('title', $title),
        'size'        =>    '70',
        'maxlength'    =>    '70'
    );
?>

<b>Title:</b><br />
&lt;?php echo form_input($title).'<br />'.form_error($title['name']);?&gt;

Any ideas? I editted out a lot of code so if you need more, please ask. I believe the necessary info is there.

The first time the page is loaded, I receive a message "The Title field is required." and the database value $title is never shown to the user.

Thanks.
#2

[eluser]acoda[/eluser]
I might be mistaken but I beleve all you have to do is switch your if statement to a negative check in the controller:

Code:
if ($this->form_validation->run() == FALSE)
{
    $data['title'] = $this->sample_model->_get_title();
    $this->load->view('title_view', $data);
}
else
{
    //YES, true - if success,
    //code to update database
}

Not checked it but I'm pretty sure. Hope this helps Smile
#3

[eluser]Colin Williams[/eluser]
I'm not sure how you are getting a validation error when nothing is posted, or is this in fact what is happening? Do you have a link to this page or a form that posts to this page initially? If for some reason it's the latter you can define the rul conditionally:

Code:
if ($this->input->post('title'))
{
   $this->form_validation->set_rules('title', 'Title', 'required|min_length[5]|max_length[70]|xss_clean');
}
#4

[eluser]Ickes[/eluser]
Ah yes, I was dumb. I was using a form to direct conditionally to my new form. Since the previous form was submitted, it was running form validation from the start. I removed the form navigation which was poor design to begin with.

Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB