1

我有一个表格,要求填写所有字段。

提交后,它应该在做任何其他事情之前进行验证。但是,即使有一些字段没有填写,它也会提交。

我在这里搜索过,但似乎找不到任何对我有用的东西。

这是我在InductionPpe控制器中的方法:

public function request() {
    if ($this->request->is('post')) {
        $this->InductionPpe->set($this->request->data);
        if($this->InductionPpe->validates()) {
            // carry on
        } else {
            $this->validateErrors($this->InductionPpe);
            $this->render();
        }
    }
}

我的模型设置了字段的所有规则,并且可以很好地完成其余的过程。但它不验证。

我从哪里开始寻找问题?我该如何解决?

更新

InductionPpe 模型中的验证规则:

class InductionPpe extends AppModel {

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'hr_employee_id' => array(
            'numeric' => array(
                'rule' => array('numeric')
            ),
        ),
        'name' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter your name.'
            ),
        ),
        'shoes' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter the shoe size required.'
            ),
        ),
        'reflective_jacket' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter the size reflective jacket required.'
            ),
        ),
        'metaguards' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter the size of metaguards required.'
            ),
        ),
        'glasses' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter the number of glasses required.'
            ),
        ),
        'earplugs' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter the amount of earplugs reguired.'
            ),
        ),
        'hardhats' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter the amount of hardhats required.'
            ),
        ),
        'gloves' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter the amount of gloves required.'
            ),
        ),
        'kit_bags' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter the amount of kit bags required.'
            ),
        ),
        'dust_mask' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter the amount of dust masks required.'
            ),
        ),
        'ppe_size' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter the size of the PPE items.'
            ),
        ),
        'activities' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please specify which activities the PPE items are required for.'
            ),
        ),
        'site' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter the site that you will be visiting.'
            ),
        ),
        'project_number' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter the project number applicable.'
            ),
        ),
        'date_required' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter the date that the PPE equipment is required.'
            ),
        )
    );

}

更新 2

这是我得到的验证调试(如果我运行debug($this->InductionPpe->validationErrors);)。

array(
    'name' => array(
        (int) 0 => 'Please enter your name.'
    ),
    'shoes' => array(
        (int) 0 => 'Please enter the shoe size required.'
    ),
    'reflective_jacket' => array(
        (int) 0 => 'Please enter the size reflective jacket required.'
    ),
    'metaguards' => array(
        (int) 0 => 'Please enter the size of metaguards required.'
    ),
    'glasses' => array(
        (int) 0 => 'Please enter the number of glasses required.'
    ),
    'earplugs' => array(
        (int) 0 => 'Please enter the amount of earplugs reguired.'
    ),
    'hardhats' => array(
        (int) 0 => 'Please enter the amount of hardhats required.'
    ),
    'gloves' => array(
        (int) 0 => 'Please enter the amount of gloves required.'
    ),
    'kit_bags' => array(
        (int) 0 => 'Please enter the amount of kit bags required.'
    ),
    'dust_mask' => array(
        (int) 0 => 'Please enter the amount of dust masks required.'
    ),
    'ppe_size' => array(
        (int) 0 => 'Please enter the size of the PPE items.'
    ),
    'activities' => array(
        (int) 0 => 'Please specify which activities the PPE items are required for.'
    ),
    'site' => array(
        (int) 0 => 'Please enter the site that you will be visiting.'
    ),
    'project_number' => array(
        (int) 0 => 'Please enter the project number applicable.'
    ),
    'date_required' => array(
        (int) 0 => 'Please enter the date that the PPE equipment is required.'
    )
)

还有我输入这些的表格:

<div class="inductionPpes form">
<?php 
    echo $this->Form->create('PpeRequest',array('type' => 'file')); ?>
    <fieldset>
        <legend><?php echo __('&emsp;PPE Request'); ?></legend>
        <?php
            echo $this->Form->input('name',array('value'=>$loggedInUsersName));
            echo $this->Form->input('shoes');
            echo $this->Form->input('reflective_jacket');
            echo $this->Form->input('metaguards');
            echo $this->Form->input('glasses');
            echo $this->Form->input('earplugs');
            echo $this->Form->input('hardhats');
            echo $this->Form->input('gloves');
            echo $this->Form->input('kit_bag');
            echo $this->Form->input('dust_masks');
            echo $this->Form->input('ppe_size');
            echo $this->Form->input('activities',array('label'=>'Type of activities that will be undertaken'));
            echo $this->Form->input('additional',array('label'=>'Additional PPE Requirements'));
            echo $this->Form->input('site',array('label'=>'Type of Site'));
            echo $this->Form->input('project_number');
            echo $this->Form->input('date_required');
        ?>
    </fieldset>
    <?php echo $this->Form->end(__('Submit')); ?>
</div>
4

2 回答 2

1

尝试将notempty规则更改为此

'notempty' => array(
    'required' => true,
    'allowEmpty' => false, // this may not be neccessary
    'rule' => 'notEmpty',  
    'message' => 'Please enter the ...',
)

问题可能是您将验证规则名称包装在一个数组中。AFAIK 仅应以这种方式输入带有参数的规则。

我也不知道$this->validateErrors这样的变量是否存在,但我总是使用$this->Model->validationErrors

于 2013-05-16T08:46:41.900 回答
0

试试这个(你所有的 notempty 验证)

'name' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Please enter your name.',
            'required' => true,
            'allowEmpty' => false
        ),
    ),

requiredallowEmpty放在底部。此外,阅读您的评论,您提到错误块没有显示。阅读这篇文章,如果你执行 $this->Model->save(),它会解决这个问题,否则你将不得不这样做。

于 2013-05-16T19:48:28.823 回答