Reputation: 937
I am not able to figure out how to repopulate my birthday drop down helper in CodeIgniter when the validation fails and the page reloads. I am trying to use set_value('field_name')
It's a little more involved than my other drop downs because there are three elements involved.
My code is available here
thanks in advance
Here are the validation rules
$this->form_validation->set_rules('relation', 'Relation', 'trim|required');
$this->form_validation->set_rules('location', 'Location', 'trim|required');
$this->form_validation->set_rules('city', 'City', 'trim|required');
$this->form_validation->set_rules($birthdate, 'Birthday', 'trim|required');
$this->form_validation->set_rules('gender', 'Gender', 'trim|required');
Upvotes: 0
Views: 1272
Reputation: 4565
Here's a stripped down version of what I use to validate birthdate (and repopulate the dropdowns). FYI I have some logic in here you may want to remove.
Validation Rules:
$this->form_validation->set_rule('birthdate-month','Birthdate Month','required|is_natural_no_zero|greater_than[0]|less_than[13]');
$this->form_validation->set_rule('birthdate-day','Birthdate Day','required|is_natural_no_zero|greater_than[0]|less_than[32]');
$this->form_validation->set_rule('birthdate-year','Birthdate Year','required|is_natural_no_zero|greater_than[1930]|less_than['.(date("Y") - 18).']');
Controller:
if ($this->form_validation->run()) {
$birthdate = array(
$this->form_validation->set_value('birthdate-year'),
$this->form_validation->set_value('birthdate-month'),
$this->form_validation->set_value('birthdate-day')
);
$data = array(
'birthdate' => implode("-", $birthdate)
);
}
// Then pass $data to your model and do whatever you want, i.e.:
$this->user_model->update_user($data);
Then the PHP/HTML in the view:
<select name="birthdate-month" id="birthdate-month">
<option value=""></option>
<?php foreach($months as $month_number => $month_name) { ?>
<option value="<?php echo $month_number; ?>" <?php echo set_select('birthdate-month', $month_number); ?>><?php echo $month_name; ?></option>
<?php } ?>
</select>
<select name="birthdate-day" id="birthdate-day">
<option value=""></option>
<?php for($i=1; $i<=31; $i++) { ?>
<option value="<?php echo $i; ?>" <?php echo set_select('birthdate-day', $i); ?>><?php echo $i; ?></option>
<?php } ?>
</select>
<select name="birthdate-year" id="birthdate-year">
<option value=""></option>
<?php for($i=(date("Y") - 18); $i>=1930; $i--) { ?>
<option value="<?php echo $i; ?>" <?php echo set_select('birthdate-year', $i); ?>><?php echo $i; ?></option>
<?php } ?>
</select>
Also FYI, $months
is an array:
array(
'01' => 'January',
'02' => 'February',
'03' => 'March',
'04' => 'April',
'05' => 'May',
'06' => 'June',
'07' => 'July',
'08' => 'August',
'09' => 'September',
'10' => 'October',
'11' => 'November',
'12' => 'December'
);
Upvotes: 2
Reputation: 2745
The documentation tell :
set_select()
If you use a menu, this function permits you to display the menu item that was selected. The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).
Example:
<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option>
</select>
Upvotes: 1