05.26 php saved
dardosordi
Tags add more
example, mvc and simple validation  
Note
MVC example for a simple validation
  1. MODEL:
  2. -----
  3.  
  4.  
  5. <?php
  6.  
  7. class Post extends AppModel {
  8.  
  9.     var $name = 'Post';
  10.  
  11.     var $validate = array(
  12.         'title' => array(
  13.                 'rule' => array('minLength', 1),
  14.              'required' => true,
  15.              'allowEmpty' => false,
  16.              'message' => 'Please set a title'
  17.             )
  18.     );
  19.  
  20. }
  21.  
  22. ?>
  23.  
  24.  
  25. CONTROLLER:
  26. ----------
  27.  
  28. <?php
  29. class PostsController extends AppController {
  30.  
  31.     var $name = 'Posts';
  32.  
  33.  
  34.     function index() {
  35.  
  36.         $this->set('posts', $this->paginate());
  37.  
  38.     }
  39.  
  40.     function add() {
  41.         if (!empty($this->data)) {
  42.             if ($this->Post->save($this->data)) {
  43.                 $this->Session->setFlash('Post added.');
  44.                 $this->redirect(array('action' => 'index'));
  45.             }
  46.  
  47.             debug($this->Post->validationErrors);
  48.         }
  49.     }
  50.  
  51. }
  52. ?>
  53.  
  54.  
  55.  
  56. VIEW:
  57. -----
  58.  
  59. <h2>Add Post</h2>
  60.  
  61. <?php echo $form->create('Post'),
  62.     $form->input('title'),
  63.     $form->input('body'),
  64. $form->end('Add'); ?>
  65.  
  66.  
Parsed in 0.071 seconds, using GeSHi 1.0.7.14

Modify this Paste