Welcome Guest, Not a member yet? Register   Sign In
select validation
#1

[eluser]runrun[/eluser]
Hi,

I don't know whats wrong but every other form elements validated well, except this.

Here's my code:
Code:
controller:

$data['Dchoices'] = array('date', 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);

$rules['date'] = 'required';

Code:
view:

<select name='date' id='date'>
&lt;?php foreach($Dchoices as $Dkey=>$Dchoice):?&gt;
<option value ="&lt;?=$Dkey?&gt;">&lt;?=$Dchoice?&gt;</option>
&lt;?php endforeach; echo"\n";?&gt;
</select>
#2

[eluser]pistolPete[/eluser]
What errors did you get?
What didn't work exactly?
Post more code.
#3

[eluser]TheFuzzy0ne[/eluser]
$Dkey does not have a value, as the value is derived from the array key, and the array has no keys.
#4

[eluser]pistolPete[/eluser]
[quote author="TheFuzzy0ne" date="1236792077"]the array has no keys[/quote]

The array has numerical keys.
Code:
$data = array('date', 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
foreach($data as $key => $value)
{
   echo $key.' - '.$value.'<br />';
}

result:
Code:
0 - date
1 - 1
2 - 2
3 - 3
4 - 4
5 - 5
6 - 6
(...)
#5

[eluser]Evil Wizard[/eluser]
try using the form helper "form_dropdown" it makes building the select list easier, also isn't a value from a select populated because if no index has been "selected" the index 0 is selected by default, meaning that the validation rule to require it is filled out defunct?

[edit]
I now see that the first element is not a valid selection option by your rules, Sad sorry
[/edit]
#6

[eluser]TheFuzzy0ne[/eluser]
[quote author="pistolPete" date="1236793249"][quote author="TheFuzzy0ne" date="1236792077"]the array has no keys[/quote]

The array has numerical keys.
[/quote]

Well I'll be... I knew that, no idea why it slipped my mind...
#7

[eluser]TheFuzzy0ne[/eluser]
Please paste your controller method.
#8

[eluser]drewbee[/eluser]
Are you passing this information to the view? Show full code please.

Also, an easier way to do this is to use the range() function. No need to type it all out Smile

Code:
$data['Dchoices'] = array_merge(array('' => 'date'), range(1,31));
#9

[eluser]runrun[/eluser]
Thanks for the tips drewbee.

controller:
Code:
&lt;?php
class Register extends Controller
{
    function index()
    {
        $this->load->library('validation');
                
        $rules['email'] = 'trim|required|valid_email|xss_clean';
        $rules['password'] = 'trim|required|matches[password_confirm]|min_lenght[5]|md5|xss_clean';
        $rules['password_confirm'] = 'trim|required';
        $rules['username'] = 'trim |required|min_lenght[3]|max_lenght[25]|xss_clean ';
        $rules['name'] = 'trim|required|max_lenght[50]|xss_clean';
        $rules['sex'] = 'required';
        $rules['date'] = 'required';
        $rules['month'] = 'required';
        $rules['year'] = 'trim|required';
        $rules['land_line'] = 'trim|required|numeric';
        $rules['mobile'] = 'trim|numeric';
        $rules['address'] = 'trim|required';
        $rules['region'] = 'trim|required';
        $rules['city'] = 'trim|required|max_lenght[50]|xss_clean ';
        $rules['activation_code'] = 'trim|xss_clean';

        $this->validation->set_rules($rules);
        
        $fields['email'] = 'Email';
        $fields['password'] = 'Password';
        $fields['password_confirm'] = 'Password confirm';
        $fields['username'] = 'Username';
        $fields['name'] = 'Name';
        $fields['sex'] = 'Sex';
        $fields['date'] = 'Date';
        $fields['month'] = 'Month';
        $fields['year'] = 'Year';
        $fields['land_line'] = 'Land line';
        $fields['mobile'] = 'Mobile phone';
        $fields['address'] = 'Address';
        $fields['region'] = 'Region';
        $fields['city'] = 'City';
        
        $this->validation->set_fields($fields);
                
        $data['Dchoices'] = array_merge(array(0 => 'date'), range(1,31));
        
        $data['Mchoices'] = array_merge(array(0 => 'month'), range(1,12));

        $data['Ychoices'] = array_merge(array(0 => 'year'), range(1920,1999));

        $data['Rchoices'] = array( 'region'
                                  ,'Hồ Chí Minh'
                                  ,'Hà Nội'
                                  ,'Huế'
                                  ,'Hải Phòng'
                                  ,'Đà Nẵng'
                                  ,'Đà Lạt'
                                  ,'Vinh'
                                  ,'Nha Trang'
                                  ,'Nam Định'
                                  ,'Kin Hon'
                                  ,'Cần Thơ'
                                  ,'Vũng Tàu'
                                  ,'Cam Ranh');
        
        $data['activation_code'] = uniqid().time();
        
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('register_view', $data);
        }
        else
        {
            $this->db->insert('user', $_POST);
            
            redirect('register/insert_success');
        }    
    }
    function register_success()
    {
        $this->load->view('insert_success');    
    }
    
}
?&gt;

part of the view:

Code:
&lt;form id="register_form" method="post"&gt;          
<select name='date' id='date'>
                    &lt;!--select date--&gt;
                        &lt;?php foreach($Dchoices as $Dkey=>$Dchoice):?&gt;
                        <option value ="&lt;?=$Dkey?&gt;">&lt;?=$Dchoice?&gt;</option>
                        &lt;?php endforeach; echo"\n";?&gt;
                        </select>
                    &lt;!--select month--&gt;
                        <select name="month" id="month">
                        &lt;?php foreach($Mchoices as $Mkey=>$Mchoice):?&gt;
                        <option value ="&lt;?=$Mkey?&gt;" &lt;?=$this->validation->set_select('month', $Mkey)?&gt;>&lt;?=$Mchoice?&gt;</option>
                        &lt;?php endforeach; echo"\n";?&gt;
                        </select>
                    &lt;!--select year--&gt;
                        <select name="year" id="year">
                        &lt;?php foreach($Ychoices as $Ykey=>$Ychoice):?&gt;
                        <option value ="&lt;?=$Ykey?&gt;" &lt;?=$this->validation->set_select('year', $Ykey)?&gt;>&lt;?=$Ychoice?&gt;</option>
                        &lt;?php endforeach; echo"\n";?&gt;
</select>
&lt;input type="submit" name="register" value="register" /&gt;
&lt;/form&gt;
#10

[eluser]runrun[/eluser]
When the selects submitted empty, the form just lets me through without display errors




Theme © iAndrew 2016 - Forum software by © MyBB