Welcome Guest, Not a member yet? Register   Sign In
Dropdown not getting validated
#1

[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/
#2

[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;
#3

[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.
#4

[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;
#5

[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.
#6

[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);
    }
#7

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

[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?
#9

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

[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.




Theme © iAndrew 2016 - Forum software by © MyBB