Welcome Guest, Not a member yet? Register   Sign In
Use Validation Rules without loading a page from a controller
#11

[eluser]Glen Swinfield[/eluser]
Ok, this is going to consist of 2 controller, 2 views and a model. I haven't used any CI helpers, I've just coded the form by hand - you use whatever method suits best:

First class is controller - just an example of any controller:
Code:
class front extends Controller
{
/**
  * Constructor
  */
function front(){
  parent::Controller();
}

/**
  * Any old action
  *
  */
function aGenericPage(){
  // Just some example vars used in the view
  $data['meta_title'] = 'The page title';
  $data['page_title'] = 'The page title';
  $data['content'] = 'Some example content';
  
  // load the view with $data
  $this->load->view('generic_page', $data);
}
}

We have loaded the 'generic_page' view, i assume you want a mini login form in this page so the view 'views/generic_page.php' looks like:

Code:
<!-- the html header (removed for quickness -->
<body>
<h1>&lt;?php echo $page_title; ?&gt;</h1>

<div id="content">
  &lt;?php echo $content; ?&gt;
</div> &lt;!-- END CONTENT --&gt;

<div id="sidebar">
  <div class="sidebar-module">
   &lt;!-- include the form fragment --&gt;
   &lt;?php $this->load->view('mini_login'); ?&gt;
  </div>
  
  <ul>
   <li>Perhaps a list here</li>
   <li>Perhaps not</li>
   <li>Perhaps a list here</li>
   <li>Perhaps not</li>
   <li>Perhaps a list here</li>
   <li>Perhaps not</li>
  </ul>
</div>&lt;!-- END SIDEBAR --&gt;

&lt;/body&gt;
&lt;/html&gt;

You login form may look like: (the redirect input is optional, it's just one way of sending a return page in needed)
Code:
&lt;form action="/auth/login" method="post" id="mini_login_form"&gt;
&lt;input type="hidden" value="&lt;?php echo $_SERVER['REQUEST_URI']; ?&gt;" name="redirect" /&gt;
<p><label>Username:</label> &lt;input type="text" name="username" size="20" /&gt;</p>
<p><label>Password:</label> &lt;input type="password" name="password" size="20" /&gt;</p>
<p>&lt;input type="hidden" valie="Login" /&gt;</p>
&lt;/form&gt;

When the login form is submitted it posts the data /auth/login as below:

Code:
class auth extends Controller
{
function auth(){
  parent::Controller();
  $this->load->database();
  $this->load->helper('url');
  $this->load->library('validation');

  // See further down for an explanation of this model
  $this->load->model('authmodel');
}

/**
  * this is the action that does the login
*/
function login(){
  // Set up validation parameters
  $validation = $this->authmodel->validationCriteria();

  // Just get the validation criteria from the model
  $this->validation->set_rules($validation['rules']);
  $this->validation->set_fields($validation['fields']);
  $this->validation->set_error_delimiters('<li>', '</li>');
  
  // put your login code here with validation run() etc.
  
  // At this point either user is logged in or not, where you send them is up to you
  
  // i.e. back to previous page, or admin area or whatever.

}
}

I would create a model, perhaps 'authModel' and have the following method in it:

Code:
function validationCriteria(){
   $fields['username'] = 'Username';
   $fields['password'] = 'Password';

   $rules['username'] = 'rules_here';
   $rules['password'] = 'rules_here';

   return array('fields' => $fields, 'rules' => $rules);
}

This is just for convenience - it gives 1 central place to keep validation rules so you don't have to repeat yourself, also it makes sense to me to keep validation rules in a model - but you could put them elsewhere.

This is just off of the top of my head and hasn't been tested, there are probably better ways - to recap here is what this achieves:

Display a page with a small login form included in the page. When the form is submitted it posts to another controller that loads validation info from a model, validates the request to login and then does whatever you choose - redirect, send to an admin area, log the action, the user just sees a short delay while the form is processed.

Like I said, I haven't tested this but hopefully you get the idea. I'm still not 100% I have understood your question.

Let me know how you get on.


Messages In This Thread
Use Validation Rules without loading a page from a controller - by El Forum - 07-19-2007, 08:31 AM



Theme © iAndrew 2016 - Forum software by © MyBB