Welcome Guest, Not a member yet? Register   Sign In
[Solved] Remember Selected Option Value If Form Errors
#1

(This post was last modified: 09-02-2016, 06:56 PM by wolfgang1983.)

Hi, If there are any form validation error that has been triggered I would like to know if there is a way to remember the seleted option value on view so the user does not have to go through the whole list


PHP Code:
<?php

class Add extends MY_Controller {

    private $error = array();

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('countries');
    }

    public function index() {
        $this->form_validation->set_rules('country''Country''trim|required|callback_country[country]');
        $this->form_validation->set_rules('timezone''Time Zone''trim|required|callback_timezone[timezone]');

        $this->form_validation->set_message('required''<b>{field}</b> must be set!');

        // Submits form if true
        if ($this->form_validation->run($this) == true) {
        
        
}

        if (validation_errors() != false) {
            $this->error['warning'] = validation_errors('<div class="alert alert-danger">''</div>');
        }

        if (isset($this->error['warning'])) {
            $data['warning_error'] = $this->error['warning'];
        } else {
            $data['warning_error'] = '';
        }

        $data['countries'] = $this->countries->get();
        $data['timezones'] = DateTimeZone::listIdentifiers(DateTimeZone::ALL);

        $data['header'] = Modules::run('admin/common/header/index');
        $data['footer'] = Modules::run('admin/common/footer/index');

        $this->load->view('user/add_view'$data);
    }

    public function country($str) {
        if ($str) {
            return true;
        } else {
            $this->form_validation->set_message('country''<b>{field}</b> must be set!');
            return false;
        }
    }

    public function timezone($str) {
        if ($str) {
            return true;
        } else {
            $this->form_validation->set_message('timezone''<b>{field}</b> must be set!');
            return false;
        }
    }




View

Code:
<?php echo $header;?>
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12 col-xs-12">

<?php echo form_open_multipart('admin/user/add', array('id' => 'form-user_add', 'class' => 'form-horizontal', 'role' => 'form'));?>

<?php echo $warning_error;?>

<div class="form-group">
<label class="col-lg-2 col-md-2 col-sm-12 col-xs-12" for="firstname">Country</label>
<div class="col-lg-10 col-md-10 col-sm-12 col-xs-12">
<select name="country" class="form-control" id="country">
    <option>-- Please Select --</option>
    <?php foreach ($countries as $code => $country) {?>
        <option value="<?php echo $code;?>"><?php echo $country;?></option>
    <?php }?>
</select>
</div>    
</div>

<div class="form-group">
<label class="col-lg-2 col-md-2 col-sm-12 col-xs-12" for="firstname">Time Zone</label>
<div class="col-lg-10 col-md-10 col-sm-12 col-xs-12">
<select name="timezone" class="form-control" id="timezone">
    <option>-- Please Select --</option>
    <?php foreach ($timezones as $timezone) {?>
        <option value="<?php echo $timezone;?>"><?php echo $timezone;?></option>
    <?php }?>
</select>
</div>    
</div>

</div>

<div class="panel-footer">
<div class="clearfix">
<div class="pull-left">
</div>
<div class="pull-right">
<button type="submit" id="form-button" class="btn btn-primary">Create</button>
</div>
</div>
</div>

</div>
<?php echo form_close();?>
</div>
</div>
</div>

<script type="text/javascript">
$(document).ready(function() {

});

</script>
<?php echo $footer;?>
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

(This post was last modified: 09-02-2016, 06:58 PM by wolfgang1983.)

I have found the solution using set_select()

Code:
<div class="form-group">
<label class="col-lg-2 col-md-2 col-sm-12 col-xs-12" for="firstname">Country</label>
<div class="col-lg-10 col-md-10 col-sm-12 col-xs-12">
<select name="country" class="form-control" id="country">
    <?php foreach ($countries as $code => $country) {?>
        <option value="<?php echo $code;?>" <?php echo set_select('country', $code) ;?>><?php echo $country;?></option>
    <?php }?>
</select>
</div>    
</div>

<div class="form-group">
<label class="col-lg-2 col-md-2 col-sm-12 col-xs-12" for="firstname">Time Zone</label>
<div class="col-lg-10 col-md-10 col-sm-12 col-xs-12">
<select name="timezone" class="form-control" id="timezone">
    <?php foreach ($timezones as $timezone) {?>
        <option value="<?php echo $timezone;?>" <?php echo set_select('timezone', $timezone) ;?>><?php echo $timezone;?></option>
    <?php }?>
</select>
</div>    
</div>
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB