1
votes

Okay, so, in short, I'm trying to validate a form, as I've done a million times before with no trouble. However, I'm finding that on logging the validation errors, all the invalidated fields have an index in the validationErrors array but the messages are empty.

Yes, I've definitely set the validation messages in the model so I'm unsure why it's empty.

Here are my model validations:

public $validate = array(
    'effective_policy_date' => array(
        'date' => array(
            'rule' => array('date'),
            'message' => 'Invalid date format',
        ),
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'Policy date required',
        ),
    ),
    'business_address' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'Business address required',
        ),
    ),
    'city' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'City required',
        ),
    ),
    'zip_code' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'Zip code required',
        ),
    ),
    'state_id' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'State required',
        ),
    ),
    'contact_person' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'Contact person required',
        ),
    ),
    'phone' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'Phone number required',
        ),
    ),
    'email' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'Email address required',
        ),
        'email' => array(
            'rule' => array('email'),
            'message' => 'Invalid email format',
        ),
    ),
);

Here's the output of the validationErrors array after submitting the form with empty fields:

[AccountsRequest] => Array
    (
        [effective_policy_date] => Array
            (
            )

        [policy_number_one] => Array
            (
            )

        [policy_number_two] => Array
            (
            )

        [policy_number_three] => Array
            (
            )

        [policy_number_four] => Array
            (
            )

        [business_address] => Array
            (
            )

        [city] => Array
            (
            )

        [zip_code] => Array
            (
            )

        [state_id] => Array
            (
            )

    )

For completeness sake, here's my Form->create array I use in the view and the controller action responsible for handling the form submission:

Form create() method

<?php 
    echo $this->Form->create(
        'Request', 
        array(
            'novalidate' => 'novalidate', 
            'action' => 'new_request', 
            'inputDefaults' => array(
                'label' => false, 
                'div' => false,
                'error' => array(
                    'attributes' => array(
                        'wrap' => 'label', 'class' => 'error'
                    )
                )
            )
        )
    );
?>

Controller action

public function new_request()
{
    $this->page_id = 'requester_newform';

    if($this->request->is('post'))
    {
        if($this->Request->saveAll($this->request->data, array('deep' => true, 'validate' => 'only')))
        {
            $this->Session->setFlash('Request saved successfully', 'flash/success');
        }
        else
        {
            $this->Session->setFlash('Could not save request. Please correct validation errors', 'flash/error');

        }
    }
}

You'll see some indeces in the validation array that aren't in the validationErrors, that's simply because I haven't quite finished converting the raw HTML to CakePHP forms.

Problem: The validationErrors array shouldn't be empty. It should contain the messages for the notEmpty rules and as a result there are empty error validation elements on the Form's frontend. Any ideas about what I'm doing wrong?

1

1 Answers

0
votes

Aarg, how annoying. I've figured it out and it's a lesson well learnt.

For anyone having a similar issue in the future, make sure that your input fields conform to the relationships of the current form's Model.

For instance, in this example, my form's model is 'Request'. Request hasMany 'AccountsRequest'. So my form inputs were something like:

AccountsRequest.effective_policy_date

where it should have been

AccountsRequest.0.effective_policy_date

With this change, my model validation messages are now showing without issue.

I'd still love to know, however, why CakePHP even picked up those fields as invalid and further, if it was intelligent enough to pick up those fields as invalid why it didn't give me validation messages.

Oh well.....