开发者

CakePHP: Why does validation fail to run?

I have started to look into CakePHP to increase my productivity. The problem is that I have run into a small problem, which has kept me occupied in more than one hour - when the form is submitted it is not validated.

Even though I intention开发者_如何学Goally leave both fields blank it still saves and gives the OK-message.

Hope somebody can help me get going again. If somebody knows an active CakePHP-forum i'd appreciate a link.

goods_controller.php

<?php
class GoodsController extends AppController {
 var $name = 'Goods';

  function index() {
    $this->set('goods',$this->Good->find('all'));
  }

  function view($id = NULL) {
    //list of fields+id, null meaning take all
    $this->set('good',$this->Good->read(NULL, $id));
  }

  function add() {
    //Is it not empty? Then lets go on and save the data  
    if(!empty($this->data)) {      
      $this->Good->Create();
        if($this->Good->save($this->data)){
          $this->Session->setFlash('Varen blev gemt succesfuldt');
          //$this->redirect(array('action'=>'index'));
        } else {
          $this->Session->setFlash('Varen kunne desværre ikke gemmes, prøv venligst igen!');
        }
    } 
  }

} 
?>

add.ctp

<h1>Add Post</h1>
<?php
  echo $form->create('Good', array('action'=>'add'));
  echo $form->input('headline_dk');
  echo $form->error('headline_dk');
  echo $form->input('text_dk');
  echo $form->error('text_dk');
  echo $form->end('Indsæt vare');
?>

goods.php

class Good extends AppModel {
  var $name = 'Good';

  var $validate = array(    
    'headline_dk' => array(
      'rule' => 'notEmpty',
      'message' => 'Angiv venligst en titel'
    ),

    'headline_dk' => array(
      'rule' => array('between', 5, 255),
      'message' => 'Titlen skal være mellem fem og 255 tegn'
    )

    'text_dk' => array(
    'required' => true,
    'message' => 'Angiv venligst en beskrivelse af varen'
    )
};


You're assigning multiple validation rules for a single field, but in your model the var $validate code has two assignments for 'headline_dk' ... instead it should look something like this:

var $validate = array(
  'headline_dk' => array(
    'headline-rule-1' => array(
      'rule' => 'notEmpty',  
      'message' => 'Angiv venligst en titel'
    ),
    'headline-rule-2' => array(
      'rule' => array('between', 5, 255),  
      'message' => 'Titlen skal være mellem fem og 255 tegn'
    )  
  )
);

This might not be the only issue ,but it is something that could cause the validation to not work. See also this page in the documentation.


"Create" in $this->Good->create(); should be lowercase.

Update: Try this syntax:

var $validate = array(    
    'headline_dk' => array(
        'between' => array(
            'rule' => array('between', 5, 255),
            'message' => 'Titlen skal være mellem fem og 255 tegn'
        )
    )
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜