编辑操作中的错误消息不会出现

时间:2011-07-08 14:07:59

标签: php validation cakephp action

我的模型中有验证规则,一切都很好。它会像我想的那样验证,但在编辑操作中,虽然它没有验证,但不要在文本框下显示de red错误标记。

任何提示?

感谢。

型号代码(型号名称为Safpercent):

var $validate = array(
        'sequencia' => array(
            'must_be_numeric' => array(
                'rule' => 'Numeric',
                'message' => 'Number Field: insert only numbers.' 
            )
        ),
    );

查看文字框:

echo $form->input('Safpercent.sequence', array('id' => 'sequence', 'options' => $criteria, 'label' => false, 'div' => false, 'style' => 'width: 300px'));

控制器代码:

function edit($id = null) {
                $criteria = $this->Safpercent->Safrequirement->find('list', array('fields' => array('Safrequirement.sequencia', 'Safrequirement.descricao'), 'conditions' => array('Safrequirement.tipo' => 'ILC')));
                $this->set('criteria', $criteria);
                if (!$id && empty($this->data)) {
                    $this->Session->setFlash(RecordNotValid, 'flash_failure');
                    $this->redirect(array('controller' => 'safpercents', 'action'=>'index'));
        }
        if (!empty($this->data)) {
                    $sequencia = $this->data['Safpercent']['sequencia'];
                    if($this->data['Safpercent']['tipo'] == ''){$tipo = 'ILC';}else{$tipo = $this->data['Safpercent']['tipo'];} 
                    $encontro = $this->Safpercent->Safrequirement->find('all', array('conditions' => array('sequencia' => $sequencia, 'tipo' => $tipo)));
            if($encontro <> array()){
                        if ($this->Safpercent->save($this->data)) {
                            $this->Session->setFlash(RecordSaved, 'flash_success');
                            $this->redirect(array('controller' => 'safpercents', 'action'=>'index'));
                        }else{
                            $this->Session->setFlash(RecordNotSaved, 'flash_failure');
                        }
                    }else{
                        $this->Session->setFlash('A Sequência que tentou Inserir não existe. Verifique a tabela de novo, por favor.');
                    }   
                }
        if (empty($this->data)) {
                    $this->data = $this->Safpercent->read(null, $id);
                    $this->set('id', $id);
        }
                $this->set('cod_percent',$this->Safpercent->read(null, $id));
    }

(某些情况下为葡萄牙语变量和文本)

2 个答案:

答案 0 :(得分:2)

试试

  

调试($这 - &GT; Safpercent-&GT; validationErrors)

并查看是否显示任何错误。

答案 1 :(得分:1)

我现在看到了。在您的控制器中,您正在使用您正在验证的sequencia字段,但在此阶段未进行任何验证。它将非数字传递给find查询,然后返回错误或其他内容,save永远不会被调用?

在此之前:

$sequencia = $this->data['Safpercent']['sequencia'];

您应该通过调用以下内容来检查数据是否有效:

$this->ModelName->set($this->data);
if ($this->ModelName->validates()) {
    ... //do your business here

基本上,改变:

if (!empty($this->data)) {

为:

$this->Safpercent->set($this->data);
if (!empty($this->data) && $this->Safpercent->validates()) {