CodeIgniter Forums
Dropdown not getting validated - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Dropdown not getting validated (/showthread.php?tid=50109)

Pages: 1 2


Dropdown not getting validated - El Forum - 03-14-2012

[eluser]xtremer360[/eluser]
I'm trying to figure out what I'm doing wrong here. For some reason the dropdown isn't getting validated correctly because it should be coming up with an error message saying this field is required but its not. It should be just like this page for the dropdown. I'm also trying to get it to display the Select an Option default option when the form opens and have that be the option that if its still selected when the form submits then it throws the error saying the field is required. Also not sure about why there's an inner border around the form.

http://www.kansasoutlawwrestling.com/peach/forms.html

Code:
<?php echo form_label('Recipient', 'recipient'); ?>
<?php
$options = array();
$options[0] = '<option></option>';
foreach($users AS $user)
{
    $options[$user->user_id] = $user->first_name.' '.$user->last_name;
}
?&gt;
&lt;?php echo form_dropdown('recipient', $options, -1, 'class=required'); ?&gt;

This is what the rendered page looks like

http://jsfiddle.net/tNFNY/


Dropdown not getting validated - El Forum - 03-14-2012

[eluser]InsiteFX[/eluser]
Code:
&lt;?php
$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, 'large');
?&gt;



Dropdown not getting validated - El Forum - 03-14-2012

[eluser]CroNiX[/eluser]
Try set_value('recipient', 0); instead of -1 for the selected value to set the select value to what the last form submission was or 0 (your default value) if no form was submitted.


Dropdown not getting validated - El Forum - 03-14-2012

[eluser]xtremer360[/eluser]
So I ended up changing it to a muliple select with the following and trying having it validate and nothing I'm still not getting a validation error.

Code:
&lt;?php echo form_label('Recipient', 'recipient'); ?&gt;
                                    &lt;?php
                                    $options = array();
                                    foreach($users AS $user)
                                    {
                                        $options[$user->user_id] = $user->first_name.' '.$user->last_name;
                                    }
                                    ?&gt;
         &lt;?php echo form_multiselect('recipient', $options, set_value('recipient', 0), 'class=required'); ?&gt;



Dropdown not getting validated - El Forum - 03-14-2012

[eluser]CroNiX[/eluser]
Well, now you removed the default option with the 0 index.

Show where you set the validation rules for recipient and where you execute them.


Dropdown not getting validated - El Forum - 03-14-2012

[eluser]xtremer360[/eluser]
Here's the controller code for when the user submits the form:

Code:
// Function used for login form validation
    function pm_submit()
    {
        $outputArray = array('error' => 'yes', 'message' => 'unproccessed');
        $outputMsg = '';
        
        // Sets validation rules for the login form
        $this->form_validation->set_rules('recipient', 'Recipient', 'required|integer');
  $this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
  $this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');
        $this->form_validation->set_rules('sender', 'Sender', 'required|integer');
        
        // Checks to see if login form was submitted properly
        if (!$this->form_validation->run())
        {
        
            $outputArray['message'] = 'There was a problem submitting the form! Please refresh the window and try again!';
            
        }
        else
        {
            // Retrieves users from database with posted user ids
            $recipient = $this->users->get_user_by_user_id($this->input->post('recipient'));
            $sender = $this->users->get_user_by_user_id($this->input->post('sender'));
            
            // Verifies users were returned from database
            if (($recipient == FALSE) || ($sender == FALSE))
            {
                // Users was not found in the database
                $outputArray['message'] = 'Either the recipient or yourself could not be found in the database!';
            }
            else
            {
                if ($this->pmmmodel->allows_pms($this->input->post('recipient')))
                {
                    if ($this->pmmodel->send_pm($this->input->post('recipient'), $this->input->post('subject'), $this->input->post('message'), $this->input->post('sender')))
                    {
                        $outputArray = array('success' => 'Yes', 'message' => 'Message was sent successfully!');    
                    }
                    else
                    {
                        $outputArray['message'] = 'Message could not get sent!';
                    }
                }
                else
                {
                    $outputArray['message'] = 'Recipient does not allow to be messaged!';
                }
            }
        }
        echo json_encode($outputArray);
    }



Dropdown not getting validated - El Forum - 03-14-2012

[eluser]xtremer360[/eluser]
Keep in mind that this multiple select can have one selection or more.



Dropdown not getting validated - El Forum - 03-14-2012

[eluser]CroNiX[/eluser]
In your code you have pmmmodel and also pmmodel. Which is it or do you actually have 2 models named like that?


Dropdown not getting validated - El Forum - 03-14-2012

[eluser]xtremer360[/eluser]
oops but by that time the field should have already been validated



Dropdown not getting validated - El Forum - 03-14-2012

[eluser]CroNiX[/eluser]
All of your options have integers as values, so it will always pass your rule because it has a value which will make it pass the Required rule and it is an integer.