在CakePHP中查找invalidFields() [英] Looking for invalidFields() with CakePHP

查看:136
本文介绍了在CakePHP中查找invalidFields()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从我的validate()数组中获取实际的消息,该数组包含了验证模型中提交内容的所有规则。



基本上我发送ajaxily,我想返回所有的失败验证表单中的错误消息,但它仍然发送,即使他们已通过验证。



所以在我的



SubmissionsController 我这样做:

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

在我的提交我有:

  var $ validate = array(
'title'=& 'title'=> array(
'rule'=>'notEmpty',
'required'=> true,
'allowEmpty'=> false,
'message'=>'请输入标题'
),
'minLength'=> array(
'rule'=> array('minLength',5) b $ b'message'=>'请将您的头衔延长(例如IJL John F. Kennedy将其总统薪金捐给慈善机构)'
),
'maxLength'=& $ b'rule'=> array('maxLength',300),
'message'=>'您的标题需要缩短'
),
),
'description'=> array(
'shortDescription'=> array(
'rule'=> array('shortDescription'),
'message'=>描述需要更长的'
),
'longDescription'=> array(
'rule'=> array('longDescription'),
'message'=>'您的描述需要更短'
),
) b $ b'source'=> array(
'source'=> array(
'rule'=>'notEmpty',
'required'=> true,
'allowEmpty'=& false,
'message'=>'输入有效的来源网址(例如http://en.wikipedia.org/wiki/Penguins)'
),
'website'=> ; array(
'rule'=>'url',
'message'=>'输入有效的源URL(例如http://en.wikipedia.org/wiki/Penguins)
),
),
'category'=> array(
'category'=> array(
'rule'=>'notEmpty',
'required'=> true,
'allowEmpty'=& false,
'message'=>'请选择一个类别




);

在我的提交/ json / submit.ctp file我有:

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

echo json_encode($ toReturn);

如果我输入有效的标题或任何其他有效字段,我仍然收到错误消息



有一些我缺少的 invalidFields()需要为了不返回字段



如下面Leo所建议的,我没有调用save before

code> invalidFields()



正确的代码应为:

  if($ this-> Submission-> save($ this-> request-> data)){
$ formData = null;
} else {
$ formData = $ this-> Submission-> invalidFields();
}
$ this-> set(compact('formData'));


解决方案

您无需验证就调用invalidFields一个save()调用或validates()!


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

解决方案

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

这篇关于在CakePHP中查找invalidFields()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆