0

I am trying to validate form and save to database, but ItemType->validates() is always true, even though I enter bad data.

ItemTypesController.php

<?php
App::uses('AppController', 'Controller');

class ItemTypesController extends AppController {

public function add() {
    if ($this->request->is('post')) {
        $this->ItemType->set($this->request->data);
        $this->ItemType->create();
        if($this->ItemType->validates()){
            debug($this->ItemType->validates());
            if ($this->ItemType->save($this->request->data)) {
                $this->Flash->success(__('The item type has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {

                $this->Flash->warning(__('The item type could not be saved. Please, try again.'));
            }
        }
        debug($this->ItemType->validationErrors);
        $this->Flash->warning($this->ItemType->validationErrors);

    }
}




}

ItemType.php

class ItemType extends AppModel {


public $validate = array(
    'code' => array(
        'required' => array(
            'rule' => 'notBlank',
            'message' => 'A code is required'
        ),
        'alphanum' => array(
            'rule' => 'alphanumeric',
            'message' => 'A code must be an alphanumeric value'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'message' => 'This code already exists!'
        )
    ),
    'name' => array(
        'required' => array(
            'rule' => 'notBlank',
            'message' => 'A name is required'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'message' => 'This name already exists!'
        )
    ),
    'class' => array(
        'valid' => array(
            'rule' => array('inList', array('product', 'material', 'kit', 'semi_product', 'service_product', 'service_supplier','consumable','inventory','goods','other')),
            'message' => 'Please enter a valid class',
            'allowEmpty' => false
        )
    ));

public $hasMany = array(
    'Item' => array(
        'className' => 'Item',
        'foreignKey' => 'item_type_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

}

add.ctp

<div class="itemTypes form">
<?php echo $this->Form->create('ItemType'); ?>
<fieldset>
    <legend><?php echo __('Add Item Type'); ?></legend>
<?php
    echo $this->Form->input('code');
    echo $this->Form->input('name');
    echo $this->Form->input('class');
    echo $this->Form->input('tangible');
    echo $this->Form->input('active');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">

So, when I enter data in a form and submit, it always tries to save to a db, even though validation should not permit, I have debugged with debug() function, and $this->ItemType->validates() is always true. What makes it weirder, when I try to send same data but to debug error messages in the else block, they are present as they should be (but validates() is true):

array(
'code' => array(
    (int) 0 => 'This code already exists!'
),
'name' => array(
    (int) 0 => 'A name is required'
),
'class' => array(
    (int) 0 => 'Please enter a valid class'
)
)

I don't get it how $this->ItemType->validates can be true and $this->ItemType->validationErrors have value at the same time.

1 Answer 1

1

This is happening because you are setting the data to validate using set method, but in the next line you are calling create. The create method clears everything and so you do not get any validation errors. According to the Docs

It does not actually create a record in the database but clears Model::$id and sets Model::$data based on your database field defaults. If you have not defined defaults for your database fields, Model::$data will be set to an empty array.

You need to move line $this->ItemType->create(); to just before your save Method.

Your code should be like below:

        $this->ItemType->set($this->request->data);
        //$this->ItemType->create();           //Commented this
        if($this->ItemType->validates()){
            debug($this->ItemType->validates());
            $this->ItemType->create();  //Move your create here.
            if ($this->ItemType->save($this->request->data)) {
                $this->Flash->success(__('The item type has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {

                $this->Flash->warning(__('The item type could not be saved. Please, try again.'));
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.