使用模型进行表单验证,但在cakePHP中没有表格

时间:2013-05-15 08:36:38

标签: php forms validation cakephp-2.0

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

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

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

以下是我的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>

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

尝试此操作(使用所有注释验证)

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

requiredallowEmpty放在最底层。另外,阅读您的评论,您提到错误块没有显示。阅读此post,如果您执行$ this-&gt; Model-&gt; save(),它会处理这个问题,否则您将不得不这样做。