Because its wrong. Its a good idea to take a look at the
validation.php library class.

The VALID_NOT_EMPTY constant is a regex string, so it would not apply
at all the way you are doing it.

You are also calling custom functions wrong.

var $validate = array(
        'title' => array(
                'required' => true,
                'length' => array(
                        'rule' => array('validateLength', 5, 100),
                        'message'  => 'Invalid length'
                ),
                'notEmpty' => array(
                        'rule' => 'notEmpty',
                        'message'  => 'Required title'
                )
         ),
        'body' => 'notEmpty'
);

On Sep 11, 12:53 am, Dave <[email protected]> wrote:
> For some reason this is not checking validation and is allowing me to
> enter empty fields in both title and body.
> Code for model/cont/view is below. I'm using cake 1.2.5
> Any pointers would be muchly appreciated
>
> // model
> class Post extends AppModel {
>     var $name = 'Post';
>         var $validate = array(
>         'title' => array(
>                 'required' => VALID_NOT_EMPTY,
>                 'length' => array( 'rule' => 'validateLength', 'min'
> => 5, 'max' => 100 )
>         ),
>         'body' => VALID_NOT_EMPTY
>         );
>         function validateLength($value, $params = array()) {
>         $valid = false;
>
>         $params = am(array(
>             'min' => null,
>             'max' => null,
>         ), $params);
>
>         if (empty($params['min']) || empty($params['max'])) {
>             $valid = false;
>         } else if (strlen($value) >= $params['min'] && strlen($value)
> <= $params['max']) {
>             $valid = true;
>         }
>
>         return $valid;
>     }
>
> }
>
> //controller
>
> class PostsController extends AppController {
>         var $name = 'Posts';
>         function index() {
>                 $this->set('posts', $this->Post->find('all'));
>         }
>         function view($id = null) {
>                 $this->Post->id = $id;
>                 $this->set('post', $this->Post->read());
>         }
>         function add() {
>         if (!empty($this->data)) {
>             // We don't do any real saving, we just validate the model
>
>             if ($this->Post->validates() && $this->Post->save($this->data)) {
>
>                                 $this->Session->setFlash('Your post
> has been saved.');
>                                 $this->redirect(array('action' =>
> 'index'));
>             }
>         }
>         }
>         function delete($id) {
>                 $this->Post->delete($id);
>                 $this->Session->setFlash('The post with id: '.$id.'
> has been deleted.');
>                 $this->redirect(array('action'=>'index'));
>         }
>         function edit($id = null) {
>                 $this->Post->id = $id;
>                 if (empty($this->data)) {
>                         $this->data = $this->Post->read();
>                 } else {
>                         if ($this->Post->save($this->data)) {
>                                 $this->Session->setFlash('Your post
> has been updated.');
>                                 $this->redirect(array('action' =>
> 'index'));
>                         }
>                 }
>         }
>
> }
>
> // add view
>
> <h1>Add Post</h1>
> {javascript func=link url="/tiny_mce/tiny_mce.js" assign=""
> __show_call=""}
> {assign_assoc var='error' value='error=>Please specify a valid body'}
> {form func='create' assign='' __show_call=false}
> {form func=input fieldName=Title class="test" size="30" assign=""
> __show_call=""}
> {form func=label fieldName=Body  assign="" __show_call=""}
> {form func=textarea fieldName=Body class="test" size="30" assign=""
> __show_call="" error=$error}
> {form func='submit' fieldName=Submit assign='' __show_call=false}
> {form func='end' fieldName=Submit assign='' __show_call=false}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to