0
votes

I'm trying to find out how to get the actual message from my validate() array which contains all the rules to validate a submission within my model.

Basically I'm POSTing ajaxily and I'd like to return all of the error messages in the form that have failed validation, but it's sending them anyway even when they have passed validation.

So in my

SubmissionsController I'm doing this:

if ($this->request->is('ajax')) {
    $formData = $this->Submission->invalidFields();
    $this->set(compact('formData'));
}

In my Submission model I have:

 var $validate = array(
    'title' => array(
        'title' => array(
            'rule' => 'notEmpty',
            'required' => true,
            'allowEmpty' => false,
            'message' => 'Please enter a title'
        ),
        'minLength' => array(
            'rule' => array('minLength', 5),
            'message' => 'Please make your title longer (e.g. IJL John F. Kennedy donated his presidential salary to charity)'
        ),
        'maxLength' => array(
            'rule' => array('maxLength', 300),
            'message' => 'Your title needs to be shorter'
        ),
    ),
    'description' => array(
        'shortDescription' => array(
            'rule' => array('shortDescription'),
            'message' => 'Your description needs to be longer'
        ),
        'longDescription' => array(
            'rule' => array('longDescription'),
            'message' => 'Your description needs to be shorter'
        ),
    ),
    'source' => array(
        'source' => array(
            'rule' => 'notEmpty',
            'required' => true,
            'allowEmpty' => false,
            'message' => 'Enter a valid source URL (e.g. http://en.wikipedia.org/wiki/Penguins)'
        ),
        'website' => array(
            'rule' => 'url',
            'message' => 'Enter a valid source URL (e.g. http://en.wikipedia.org/wiki/Penguins)'
        ),
    ),
    'category' => array(
        'category' => array(
            'rule' => 'notEmpty',
            'required' => true,
            'allowEmpty' => false,
            'message' => 'Please choose a category'
        )


    )
);

In my Submissions/json/submit.ctp file I have:

<?php
$fragment = $this->element('errors/flash_error');
$toReturn = array(
    'formData' => $formData
    );

echo json_encode($toReturn);

If I enter in a valid title or any other valid field, I still am getting back the error message instead of nothing.

Is there something I'm missing that invalidFields() needs in order to NOT return fields which HAVE passed validation?

EDIT:

As Leo suggested below, I wasn't calling save before invalidFields()

The correct code should be:

            if ($this->Submission->save($this->request->data)) {
                $formData = null;
            } else {
                $formData = $this->Submission->invalidFields();
            }
            $this->set(compact('formData'));
1
bob where are you doing the validation? You seem to be just calling invalidfields() without saving or validating? - Leo
@Leo wow man what the hell was I (not) thinking? Totally overlooked that. Thanks! - bob_cobb

1 Answers

0
votes

You're calling invalidFields() without validation either by a save() call or validates()!