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

問題は、検証ルール名を配列でラップしたことである可能性があります。私の知る限り、パラメーターを含むルールのみをこの方法で入力する必要があります。

$this->validateErrorsまた、そのような変数が存在するかどうかもわかりませんが、常に使用します$this->Model->validationErrors

于 2013-05-16T08:46:41.900 に答える
0

これを試してください(すべての空ではない検証を使用して)

'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 に答える