Welcome Guest, Not a member yet? Register   Sign In
Run functions in index method
#1

[eluser]Casperlarsen94[/eluser]
As i mention in my last topic i'm quite new in Codeigniter.

Is it possible to run a validation or something like that in a index method like this
Code:
function index()
    {
        $data['content'] = 'register_view';
        $this->load->view('template/template', $data);
    }


When i'm trying to do it, it shows my register form twice?

Anyone who can help me?

Thanks
#2

[eluser]prestondocks[/eluser]
Can you show us the code in your view and your template. The code in your method looks fine.

Simon
#3

[eluser]Casperlarsen94[/eluser]
If i put this in it:
Code:
$this->form_validation->set_rules('name', 'Name', 'required|min_length[3]|max_length[40]');
        $this->form_validation->set_rules('club', 'Club', 'required|min_length[3]|max_length[40]');
        $this->form_validation->set_rules('mail', 'Mail', 'required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[40]');

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

            $this->db->where('mail', $this->input->post('mail'));
            $this->db->where('password', md5($this->input->post('password')));
            $sql = $this->db->get('users');

            if($sql->num_rows == 0){
            
                $this->load->model('register_model');
                $this->register_model->create();
                redirect('login');
            
            }else{

                $this->load->view('register_view');

            }
            
        }else{

            $this->load->view('register_view');

        }
    }
#4

[eluser]prestondocks[/eluser]
I cant see anything wrong with that, but that is your controller method. Can you post your view and template files. I think that is where your problem may lie.

Thanks
#5

[eluser]Casperlarsen94[/eluser]
Yes, of course.

File view
Code:
<?php
echo form_open('register/validate');
echo form_input('name', set_value('name', 'Name'));
echo '<br />';
echo form_input('club', set_value('club', 'Club'));
echo '<br />';
echo form_input('mail', set_value('mail', 'Mail'));
echo '<br />';
echo form_password('password', set_value('password', 'Password'));
echo '<br />';
echo form_submit('submit', 'Register');
echo anchor('login', 'Login');
echo '<br />';
echo validation_errors();
?&gt;

Top of the template
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Title&lt;/title&gt;
&lt;link rel="stylesheet" href="&lt;?php echo base_url();?&gt;style/default.css" type="text/css" /&gt;
&lt;/head&gt;

&lt;body&gt;

Bottom of template
Code:
&lt;/body&gt;
&lt;/html&gt;

The file that merge the top and the bottom of the template together
Code:
&lt;?php
$this->load->view('template/top');
$this->load->view($content);
$this->load->view('template/bottom');
?&gt;
#6

[eluser]prestondocks[/eluser]
To be honest I am not quite sure how your controller is setup. it seems that in the index method you are loading the view inside a template, but in the form validation script you are loading the view directly with out the template.

Is your form validation code inside the index method or the validate method?

If the form validation code is also inside the index method then it looks as though you will be loading the register_view file twice.

Sorry I can help more than that.

Simon
#7

[eluser]nuwanda[/eluser]
Where is your validation? In the index method?

That won't work.

You need a method in your controller that will take the post data from your view. Then you validate the data.

Code:
function process_form(){

  //get the something input data
  $this->load->input('something');

  validate something;

  if validation fails{

    $this->load->view('myview');

  }

}
#8

[eluser]techgnome[/eluser]
nuwanda - why not? seems reasonable to me.

Casper - a couple things - 1) I don't see where you pass $content to the template. 2) Your form is posting to a validate method... so what did you post? The validate method or the index method?

Can you post your ENTIRE method? You post one set of code in the first post, then posted more code, with the comment of "If i put this in it:" w/o explaining HOW you put it all together.

-tg
#9

[eluser]nuwanda[/eluser]
[quote author="techgnome" date="1289593253"]nuwanda - why not? seems reasonable to me.[/quote]

It's not that it won't actually work, I just don't think it works in terms of good design.

I use the index method for setting up page data and loading the initial view. Now that view might contain a form which then has its action set to a method in the controller, not the index method.

That just makes more design sense to me.
#10

[eluser]techgnome[/eluser]
OK... I buy that... in that sense, yes I'd agree.

-tg




Theme © iAndrew 2016 - Forum software by © MyBB