Welcome Guest, Not a member yet? Register   Sign In
Form validation and re-population - Not Working
#1

[eluser]jaswinder_rana[/eluser]
Code
Code:
//Edit User Profile
    function user_profile()
    {
        if ($this->dx_auth->is_logged_in())
        {
            $this->load->library('Validation');
            $this->_validation_profile();
            
            if ($this->validation->run() !== FALSE){
                $data['user_id'] = $this->dx_auth->get_user_id();
                $data['id_country'] = $this->input->post('id_country');
                $data['website'] = $this->input->post('website');
                
                
            }else{
                //get User Profile data from database
                   $query = $this->user_profile->get_profile($this->dx_auth->get_user_id());
                $obj = $query->result();
                        
                $data['id_country'] = $obj[0]->id_country;
                $data['website'] = $obj[0]->website;
            }
            $data['countries'] = $this->_get_countries();
            $this->load->view('auth/user_profile',$data);
        }
    }
// --------------------------------------------------------------------

    function _validation_profile()
    {
        
        $rules['id_country'] = 'required|numeric';//'trim|required|htmlspecialchars|max_length[12]|alpha_dash';
        $rules['website'] = 'trim|htmlspecialchars';//'trim|htmlspecialchars|callback_dateIssued';
        $this->validation->set_rules($rules);
    
        $fields['id_country'] = $this->lang->line('site_id_country');
        $fields['website'] = $this->lang->line('site_website');
        $this->validation->set_fields($fields);

        $this->validation->set_error_delimiters('<span class="error">', '</span>');
    }

Then my form is (Not sure if I am doing it the proper way

Code:
&lt;?php $this->load->view('header');?&gt;
&lt;?php
    if(!$this->dx_auth->is_logged_in()){
        redirect('');
    }

$id_country =  set_value('id_country',$id_country);


$arr = array();
foreach($countries as $key=>$value){
    $arr[$value->id] = $value->name;
}


$website = array(
    'name'    => 'website',
    'id'    => 'website',
    'size'    => 50,
    'value' => set_value('website',$website)
);

?&gt;
<h1>&lt;?php echo $this->lang->line('site_user_profile');?&gt;</h1>
<fieldset>
<legend>User Profile</legend>
&lt;?php echo form_open('auth/user_profile'); ?&gt;

&lt;?php echo $this->dx_auth->get_auth_error(); ?&gt;

<dl>
    <dt>&lt;?php echo form_label('Country', 'country'); ?&gt;</dt>
    <dd>
        &lt;?php echo form_dropdown('id_country',$arr,$id_country); ?&gt;
        &lt;?php echo form_error('id_country'); ?&gt;
    </dd>

    <dt>&lt;?php echo form_label('Website', $website['id']); ?&gt;</dt>
    <dd>
        &lt;?php echo form_input($website); ?&gt;
        &lt;?php echo form_error($website['name']); ?&gt;
    </dd>

    
    <dt></dt>
    <dd>&lt;?php echo form_submit('update', 'Update Profile'); ?&gt;</dd>
</dl>

&lt;?php echo form_close(); ?&gt;
</fieldset>

&lt;?php $this->load->view('footer');?&gt;

It's populating because I am using $this->input->post(). If I try to do an echo on top of my view, it doesn't work.

Code:
echo set_value('id_country');


Thanks
#2

[eluser]Henry Weismann[/eluser]
Try:

Code:
&lt;?php echo form_dropdown('id_country',$arr,set_value('id_country',$id_country));

Also why are you checking if the user is logged in twice. You may want to think of checking if someone is logged in inside your controller class's constructor method. That will save you time from not having to write that logic more then once and it will make your application run faster without needing to process that function more then once.
#3

[eluser]jaswinder_rana[/eluser]
Thanks. I found the error. That class is actually loading validation library in constructor. I was, by mistake, loading it again and it was overwriting actual object and was giving me problem.

Thanks for that tip on Drop Down. I was hoping that there was another way to get Drop Down values rather than looping through object and creating array.

Reason for checking login in the function is, there are other functions in the same class that maybe run even if user is not logged in like register(), login() etc. I am adding above code in DX_Auth library to let users to edit profile.

thanks
#4

[eluser]Henry Weismann[/eluser]
[quote author="jaswinder_rana" date="1229986356"]
Reason for checking login in the function is, there are other functions in the same class that maybe run even if user is not logged in like register(), login() etc. I am adding above code in DX_Auth library to let users to edit profile.

thanks[/quote]

Ok that makes sense but why do you check it again in the view? And you could create a controller for all non logged in views...for example the ones you mentioned (register, login etc...) would be in an auth controller or something like that.
#5

[eluser]jaswinder_rana[/eluser]
[quote author="Henry Weismann" date="1229989586"]

Ok that makes sense but why do you check it again in the view?[/quote]

I was checking in view just to be careful in case it's accessed directly not that it will help them much. Now that I think about it, I don't need it in CI because of the way it's designed as everything will go through route.




Theme © iAndrew 2016 - Forum software by © MyBB