Welcome Guest, Not a member yet? Register   Sign In
If form error how to keep on selected option in form_dropdown
#1

(This post was last modified: 05-01-2017, 02:49 AM by wolfgang1983.)

On my view I have a form_dropdown with some options


PHP Code:
<div class="row">
<
div class="col-sm-12">
<
div class="form-group">
<
label for="input_country">Country</label>
<?
php 

$options
[] = array();

foreach (
$countries as $key => $value) {       
$options
['0'] = '--- Select Country ---';
$options[$value['iso_code_2']] = $value['name'];
}

echo 
form_dropdown('country_id'$options'', array('class' => 'form-control''id' => 'input_country'));

?>
</div>
</div>
</div> 


The iso code for Australia is AU

Question: If there is a error on form and have selected country how can I make sure it stays on the country so do not have to go through list again


PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
Register extends CI_Controller 
{

public function 
__construct() 
{
parent::__construct();
$this->load->model('localisation/country_model');
}

public function 
index()
{
if ((
$this->input->server("REQUEST_METHOD") == 'POST') && $this->validate()) 
{
// Success form area
}

if (
$this->input->post('username')) 
{
$data['username'] = $this->input->post('username');
} else {
$data['username'] = '';
}

if (
$this->input->post('firstname')) 
{
$data['firstname'] = $this->input->post('firstname');
} else {
$data['firstname'] = '';
}

if (
$this->input->post('lastname')) 
{
$data['lastname'] = $this->input->post('lastname');
} else {
$data['lastname'] = '';
}

if (
$this->input->post('email')) 
{
$data['email'] = $this->input->post('email');
} else {
$data['email'] = '';
}

if (
$this->input->post('password')) 
{
$data['password'] = $this->input->post('password');
} else {
$data['password'] = '';
}

$data['countries'] = $this->country_model->get_countries();

if (
$this->input->post('country_id')) 
{
$data['country_id'] = $this->input->post('country_id');
} else {
$data['country_id'] = '';
}

$this->load->view('default/template/common/header'$data);
$this->load->view('default/template/account/register'$data);
$this->load->view('default/template/common/footer'$data);
}

public function 
validate() 
{
// Set the form validation rules

}



Attached Files
.php   Register.php (Size: 1.58 KB / Downloads: 102)
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

I think I have found my solution 

Because I have placed this on the controller 

PHP Code:
if ($this->input->post('country_id')) 
 
{
$data['country_id'] = $this->input->post('country_id');
} else {
$data['country_id'] = '';



All I need to do now is on the view in the third section


PHP Code:
echo form_dropdown('country_id'$options$country_id, array('class' => 'form-control''id' => 'input_country')); 


I think I have it correct now

PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
Register extends CI_Controller 
{

    public function 
__construct() 
    {
        
parent::__construct();
        
$this->load->model('localisation/country_model');
        
$this->load->library('form_validation');
    }

    public function 
index()
    {
        if ((
$this->input->server("REQUEST_METHOD") == 'POST') && $this->validate()) 
        {
            
// Success form area
        
}

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

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

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

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

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

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

        if (
$this->input->post('username')) 
        {
            
$data['username'] = $this->input->post('username');
        } else {
            
$data['username'] = '';
        }

        if (
$this->input->post('firstname')) 
        {
            
$data['firstname'] = $this->input->post('firstname');
        } else {
            
$data['firstname'] = '';
        }

        if (
$this->input->post('lastname')) 
        {
            
$data['lastname'] = $this->input->post('lastname');
        } else {
            
$data['lastname'] = '';
        }

        if (
$this->input->post('email')) 
        {
            
$data['email'] = $this->input->post('email');
        } else {
            
$data['email'] = '';
        }

        if (
$this->input->post('password')) 
        {
            
$data['password'] = $this->input->post('password');
        } else {
            
$data['password'] = '';
        }

        
$data['countries'] = $this->country_model->get_countries();

        if (
$this->input->post('country_id')) 
        {
            
$data['country_id'] = $this->input->post('country_id');
        } else {
            
$data['country_id'] = '';
        }

        
$this->load->view('default/template/common/header'$data);
        
$this->load->view('default/template/account/register'$data);
        
$this->load->view('default/template/common/footer'$data);
    }

    public function 
validate() 
    {
        
// Set the form validation rules

        
$this->form_validation->set_rules('username''username''required');
        
$this->form_validation->set_rules('firstname''firstname''required');
        
$this->form_validation->set_rules('lastname''lastname''required');
        
$this->form_validation->set_rules('email''email''required');
        
$this->form_validation->set_rules('password''password''required');

        if (
$this->form_validation->run() == false
        {

            if (
form_error('username')) 
            {
                
$this->error['username'] = form_error('username');
            }

            if (
form_error('email')) 
            {
                
$this->error['email'] = form_error('email');
            }

            if (
form_error('firstname')) 
            {
                
$this->error['firstname'] = form_error('firstname');
            }

            if (
form_error('lastname')) 
            {
                
$this->error['lastname'] = form_error('lastname');
            }

            if (
form_error('password')) 
            {
                
$this->error['password'] = form_error('password');
            }
        } 

        if (
$this->input->post('country_id') === '0') {
            
$this->error['country_id'] = 'You need to select your country';
        }

        return !
$this->error;
    }



Attached Files
.php   Register.php (Size: 3.59 KB / Downloads: 69)
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