CodeIgniter Forums

Full Version: Form Validation Using Form Helper
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]mmcglynn[/eluser]
I am trying to set up validation on a simple contact form that is created using the form helper. No validation at all occurs. Do these two things even work together?

In the below, the "good" keyword always shows, regardless of what is entered into the form, and the saved values set via set_value are never shown.

Controller
Code:
// Contact
    function contact() {
        $data['pageTitle'] = "Contact";
        $data['bodyId'] = "contact";
        
        $this->load->library('form_validation');
        
        $config_rules = array ('email' => 'required','message' => 'required');
        
        $this->form_validation->set_rules($config_rules);
        
        if ($this->form_validation->run() == FALSE) {
            echo "bad";            
            $data['include'] = "v_contact";    
            $this->load->view('v_template',$data);
            
        } else {
            echo "good";
            $data['include'] = "v_contact";        
            $this->load->view('v_template',$data);
        }
        
    }

View
Code:
echo validation_errors();
echo form_open('events/contact');

// name
echo form_label('Name', 'name');
$data = array (
        'name' => 'name',
        'id' => 'name',
        'maxlength' => '64',
        'size' => '40',
        'value' => set_value('name')
    );
echo form_input($data) . "\n<br />";

// email address
echo form_label('Email Address', 'email');
$data = array (
        'name' => 'email',
        'id' => 'email',
        'maxlength' => '64',
        'size' => '40',
        'value' => set_value('email')
    );
echo form_input($data) . "\n<br />";

// message
echo form_label('Message', 'message');
$data = array (
        'name' => 'message',
        'id' => 'message',
        'rows' => '8',
        'cols' => '35',
        'value' => set_value('message')
    );
echo form_textarea($data) . "\n<br />";

echo form_submit('mysubmit', 'Send Message');

echo form_close();

El Forum

[eluser]erik.brannstrom[/eluser]
It's most likely because you are not setting the validation rules correctly. Please read http://ellislab.com/codeigniter/user-gui...ationrules, or the section below on how to set rules using an array. If you are to use an array, it needs to be multi-dimensional with one array for each field.

El Forum

[eluser]mmcglynn[/eluser]
You are right about the message array. It is now correct and looks like the below. Any idea on how to preserve the entered values?

Code:
$config_rules = array(
               array(
                     'field'   => 'email',
                     'rules'   => 'required|valid_email'
                  ),
               array(
                     'field'   => 'message',
                     'rules'   => 'required'
                  )
            );

El Forum

[eluser]erik.brannstrom[/eluser]
Hm.. Are you getting "bad" echoed now? For the name field I think you should add it to the validation configuration array, though with an empty rules string, but as far as I can see the other fields should be populated.

El Forum

[eluser]mmcglynn[/eluser]
The conditional is now correct and I am getting the validation error text. What is NOT happening, however, is the fields with valid data are not getting repopulated when the form is redisplayed with the error messages.

El Forum

[eluser]erik.brannstrom[/eluser]
Quote:What is NOT happening, however, is the fields with valid data are not getting repopulated when the form is redisplayed with the error messages.
So everything works then? Wink Actually I don't have any good ideas of what the problem could be. If you look at the source, are the form fields printed correctly? If you use for example set_value('email', 'somevalue') instead, do you get somevalue as the default value when first opening the form, i.e. without having posted anything?

El Forum

[eluser]Ivar89[/eluser]
I don't really see the fault but wouldn't this eb WAY easier?:
Code:
<label id="message">Message:</label>
&lt;input type="text" name="message" id="message" value="&lt;?php echo set_value('message') ?&gt;" style="width:153px"&gt;
also you could define the Label in your fomr_validation array:
Code:
$config_rules = array(
               array(
                     'field'   => 'message',
                     'label' => 'Message',
                     'rules'   => 'trim|required'
                  ),

El Forum

[eluser]mmcglynn[/eluser]
Thanks Ivar89. I guess part of my question was style related, Isn't better to generate all of the mark up from PHP? That's why I was using the Form Helper, but I am open to options. the fact is the set_value doesn't work in the form Helper code, so I may be forced to type the mark up as you suggest.

That said, any idea why the set_value doesn't work in this context?

El Forum

[eluser]Ivar89[/eluser]
Seems a lot more work to me so, and this works pretty good^^
I don't know why it does not work srry