phpcakephpcakephp-2.3

CakePHP - Validation errors displayed twice


I am using CakePHP 2.10.19. I have a form for entering Item Types. I also have other models for entering appropriate database objects. Error I am occurring is that my validation errors are displayed twice for this form. For other forms it works well. Here is the model:

ItemType.php

App::uses('AppModel', 'Model');

class ItemType extends AppModel {

public $classes = array(
    'product' => 'Proizvod',
    'kit' => 'Kit (bundle)',
    'material' => 'Repromaterijal'
);

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!'
        ),
        'between' => array(
            'rule' => array('lengthBetween', 3, 7),
            'message' => 'Code must be between 3 and 7 characters long'
        )
    ),
    'name' => array(
        'required' => array(
            'rule' => 'notBlank',
            'message' => 'A name is required'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'message' => 'This name already exists!'
        ),
        'between' => array(
            'rule' => array('lengthBetween', 3, 30),
            'message' => 'Name must be between 3 and 30 characters long'
        )
    ),
    '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
        )
    ),
    'tangible' => array(
        'bool' => array(
            'rule' => 'boolean',
            'message' => 'Incorrect value for the checkbox'
        )
    ),
    'active' => array(
        'bool' => array(
            'rule' => 'boolean',
            'message' => 'Incorrect value for the checkbox'
        )
    )
    );
public $hasMany = array(
    'Item' => array(
        'className' => 'Item',
        'foreignKey' => 'item_type_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);
}

ItemTypesController.php

<?php

class ItemTypesController extends AppController { 
    public function add() {
    if ($this->request->is('post')) {
        $this->ItemType->set($this->request->data);

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

                debug($this->ItemType->invalidFields());
                $this->Flash->error(__('The item type could not be saved. Please, try again.'));
            }
        }
        debug($this->ItemType->invalidFields());
        $this->Flash->warning($this->ItemType->validationErrors, array(
            'key' => 'negative'
        ));
    }
    $this->set('classes', $this->ItemType->classes);
}
}

Also, debug($this->ItemType->invalidFields()) is showing array with two fields for each field, like this:

array(
'code' => array(
    (int) 0 => 'Code must be between 3 and 7 characters long',
    (int) 1 => 'Code must be between 3 and 7 characters long'
),
'name' => array(
    (int) 0 => 'Name must be between 3 and 30 characters long',
    (int) 1 => 'Name must be between 3 and 30 characters long'
)
)

...so I am guessing a model is making some sort of mistake.

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', array('options' => $classes));
    echo $this->Form->input('tangible');
    echo $this->Form->input('active');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>

Anyone has an idea. Thing is, other controllers have basically the same logic but do not have this sort of a mistake where validation errors are displayed twice.


Solution

  • Generally this is because validation is triggered twice. This is triggered twice, once when you call $this->ItemType->validates()) and another time when you call debug($this->ItemType->invalidFields());

    Please comment and remove all the debug statements.