Welcome Guest, Not a member yet? Register   Sign In
Unvalidated form input not being repopulated
#1

[eluser]mihaibaboi[/eluser]
Hello. I have a problem that I think I know how to work around, but maybe you guys have a better idea.

So, I've got a form with the following inputs: Name, Email, URL and Comment. I use Form Validation Class to check if the input data is alright. The problem is, that the URL field is not required, so I don't validate it. As a result, if some other field fails the validation, the URL field is not repopulated.

I figure, if there's no rule set for it, the framework doesn't include it in the dataset sent back to the view.

Now, as I said, I think I have a workaround. I could just apply a trim rule to the URL, but it still bugs me a little. I should be able to validate some of the fields, without loosing the data in the others.

Am I doing something wrong, or is this just CodeIgniter normal behavior?
#2

[eluser]Walter Coots[/eluser]
Without seeing the code it's hard to know for certain. Are you using set_value('field name') when the form is generated? Or are the values being repopulated in some other way?
#3

[eluser]mihaibaboi[/eluser]
My Controller:
Code:
function send()
{
    $this->load->library('form_validation');
    
    $this->form_validation->set_rules('name', 'Name', 'required|min_length[3]');
    $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');
    $this->form_validation->set_rules('comment', 'Comment', 'required');

    if(!$this->form_validation->run())
        $this->load->view('contact');
    else
        $this->load->view('contact');
}

And my View:

Code:
<?php
$inputName = array(
    'name' => 'name',
    'id' => 'name',
    'value' => set_value('name'),
    'maxlength' => '100',
    'size' => '50',
    'style' => 'width: 150px'
);

$inputEmail = array(
    'name' => 'email',
    'id' => 'email',
    'value' => set_value('email'),
    'maxlength' => '100'
);

$inputWebsite = array(
    'name' => 'website',
    'id' => 'website',
    'value' => set_value('website'),
    'maxlength' => '100'
);

$textArea = array(
    'id' => 'comment',
    'name' => 'comment',
    'value' => set_value('comment'),
    'cols' => '45',
    'rows' => '8'
);

$js = 'onClick="some_function()"';

?>

<div id="form">

    &lt;?php echo validation_errors(); ?&gt;
    &lt;?php echo form_open('contact/send'); ?&gt;

    &lt;?php echo form_hidden('id', 0); ?&gt;

    &lt;?php echo form_fieldset('Add comment'); ?&gt;

    <p>&lt;?php echo form_input($inputName); echo form_label('Name', 'name') ?&gt;</p>
    <p>&lt;?php echo form_input($inputEmail); echo form_label('E-mail', 'email') ?&gt;</p>
    <p>&lt;?php echo form_input($inputWebsite); echo form_label('Website', 'website') ?&gt;</p>
    <p>&lt;?php echo form_label('Comment', 'comment') ?&gt;<br />&lt;?php echo form_textarea($textArea); ?&gt;

    &lt;?php echo form_fieldset_close(); ?&gt;

    &lt;?php echo form_submit('sumbit', 'Save'); ?&gt;

    &lt;?php echo form_close(); ?&gt;

I use set_value() for the URL just like the others. Maybe there's something I'm missing.
#4

[eluser]Unknown[/eluser]
heyaaa, as far as i know, to repopulate a field even if you dont need it validated requires a validation rule
Code:
$this->form_validation->set_rules('website', 'Website', '');
You can leave the 3rd parameter on the set_rules empty since you dont want that field as "required" or whatever else. Smile

Everything else on your code looks good to me.
#5

[eluser]mihaibaboi[/eluser]
[quote author="luisrootzor" date="1292558218"]heyaaa, as far as i know, to repopulate a field even if you dont need it validated requires a validation rule[/quote]

That's what I needed to confirm. Now that I think about it, I guess it's a good idea to at least trim the field, so there is no real problem.

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB