tags:

views:

58

answers:

2

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 intentionally 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'
    )
};
+1  A: 

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.

Dan U.
Thank you very much for the link, I see that I constructed the validation incorrectly. But even with the new validation the form keeps beeing posted no matter what.
Tommy
Hmm, you might try it with only one rule to test if it has to do with the multiple rules. Come to think of it, do you really need notEmpty when you have between 5-255? Since empty will be less than 5, the between rule should be all you need.
Dan U.
The reason why I added it was that I suspected that "notEmpty" might not work. I've tried going back to basics, but still no validation:class Good extends AppModel { var $name = 'Good'; var $validate = array( 'headline_dk' => array( 'headline-rule-2' => array( 'rule' => array('between', 5, 255), 'message' => 'Titlen skal være mellem fem og 255 tegn' ) ) }Thank you very much for your assistance this far :)- Any chance you do privat teaching? ;)
Tommy
A: 

"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'
        )
    )
);
bancer
Thank you very much for the correction, i'll keep that in mind. It did not solve the problem - even if I out-comment that line it still saves (but without validation).
Tommy
Thank you very much for the update regarding the syntax.It stills insert it too the database:INSERT INTO `goods` (`headline_dk`, `text_dk`) VALUES ('11', '11')
Tommy