• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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.

#12
[eluser]esra[/eluser]
[quote author="Brian Bommarito" date="1184810756"]esra has it right.

I want to include a block of code to do the quick login on a number of pages. Now, looking at validation, I don't know if it would work without having the logic in all the views it's in because you have to perform the run() on it, and without knowing exactly where it is (IE what page it's on) I can't code that properly.[/quote]

Use Coolfactor's View library. The part method in the View library can be used to load multiple blocks and module-specific view fragments. The view fragment for the login block could have a dedicated helper which you could use to handle your block logic. This does not break CI's approach to MVC. You might do some searches on the forum to learn how to run a database query from a helper.

With the Coolfactor's View library, you can restructure your modules into module-related views, blocks (which I consider to be shareable resources), and templates. Also, a module could be composed of multiple view fragments and the part method can be used to assemble those module-specific view fragments. Combine this with Zawk's Modular Separation contrbution (i.e., an extended Loader and Router library), and you should have a CMS/portal like directory structure. I modified Zawk's Loader to handle blocks and templates as separate flavors of views for application directory storage reasons (i.e., I store blocks in application/blocks/ and templates in application/templates/). Aside from storing blocks and templates in separate directory structures, this approach allows you to install and uninstall blocks and templates easily (Just add or remove the directories as needed).

My modified view method in Zawk's Loader library is below:

Code:
function view($view, $vars = array(), $return = false)
{
  $module = '';
  $CI = &get;_instance();

  // Is the requested view a module view?
  if (is_dir(APPPATH . 'modules/' . $CI->uri->segment(1))) {
   $module = $CI->uri->segment(1);
  }

  // Is the requested view a module view in a sub-folder?
  if (is_dir(APPPATH . 'modules/' . $CI->uri->segment(2) . '/controllers/' . $CI->uri->segment(1))) {
   $module = $CI->uri->segment(2);
  }

  // Return the view request as a module view
  if (file_exists(APPPATH . 'modules/' . $module . '/views/' . $view . EXT)) {
   $view = '../modules/' . $module . '/views/' . $view;
  }

  // Return the view request as a template
  if (file_exists(APPPATH . 'templates/' . $view . '/' . $view . EXT)) {
   $view = '../templates/' . $view . '/' . $view;
  }

  // Return the view request as a block
  if (file_exists(APPPATH . 'blocks/' . $view . '/' . $view . EXT)) {
   $view = '../blocks/' . $view . '/' . $view;
  }

  return parent::view($view, $vars, $return);
}

To use the above, your template directory names under templates/ need to have the same name as the template. For example, a template called aria needs to be stored in templates/aria/aria.php. Similarly, a block called login needs to be stored in blocks/login/login.php.

You need to be very aware of naming conflicts because views, view fragments (blocks), and templates do not have separate namespaces. They are all views as far as CI is aware. The above change merely adds additional search paths to Loader, allowing your blocks and templates to be stored separately.

Hopefully, the above should send you in the direction I believe you want to go.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.