简体   繁体   中英

Form validation with model but no table in cakePHP

I have a form that requires that all fields are filled in.

Upon submit, it should validate, before doing anything else. However, it submits even if there are some fields that were not filled in.

I've searched on here, but can't seem to find anything that is off use for me.

Here is my method in my InductionPpe controller:

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

My model has all the rules for the fields set up and completes the rest of the process just fine. But it does not validate.

Where do I start looking for the issue? And how do I fix it?

UPDATE

Validation rules in the InductionPpe Model:

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

}

UPDATE 2

This is the validation debug I get (if I run 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.'
    )
)

And my form where these are entered:

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

Try changing the notempty rule to this

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

The problem may be that you wrapped validation rule names in an array. AFAIK only rules with parameters should be entered this way.

Also I don't know about $this->validateErrors and whether such variable exists but I always use $this->Model->validationErrors

Try this (with all your notempty validations)

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

putting required and allowEmpty on bottom. Also, reading your comments, you mention the error blocks doesn't show. Read this post , if you do $this->Model->save(), it'll take care of that, otherwise you'll have to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM