CakePHP - 验证错误显示两次

CakePHP - Validation errors displayed twice

我正在使用 CakePHP 2.10.19。我有一个用于输入项目类型的表格。我还有其他模型用于输入适当的数据库对象。我遇到的错误是我的验证错误针对此表单显示了两次。对于其他形式,它运作良好。这是模型:

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);
}
}

此外,debug($this->ItemType->invalidFields()) 显示每个字段有两个字段的数组,如下所示:

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'
)
)

...所以我猜某个模型犯了某种错误。

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')); ?>

任何人都有想法。问题是,其他控制器具有基本相同的逻辑,但不会出现验证错误显示两次的错误。

显然,在删除调试语句后,它的工作方式与应有的一样,每种类型只有一个验证错误。不确定为什么调试会出现问题。

一般是因为验证触发了两次。这会触发两次,一次是在您调用 $this->ItemType->validates()) 时,另一次是在您调用 debug($this->ItemType->invalidFields());

请评论并删除所有调试语句。