CakePHP的表单验证问题 [英] Form validation issues with CakePHP

查看:50
本文介绍了CakePHP的表单验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CakePHP 2.3.8,并尝试通过CakePHP验证图像上传,但是Cake给我带来了验证问题。我知道验证将被触发,因为如果我从验证规则中删除特定的扩展名,那么我会收到一条验证错误消息。 (例如:从验证列表中删除 jpg,然后我上传一个.jpg文件,然后显示错误消息)。

I'm using CakePHP 2.3.8 and trying to validate an image upload with CakePHP but Cake is giving me trouble with the validation. I know the validation is being triggered because if I remove the specific extension from the validation rules, I then receive a validation error message. (EX: remove 'jpg' from the validation list and I upload a .jpg file it then shows the error message).

'image' => array(
    'extension' => array(
        'rule' => array('extension', array('png','jpg','jpeg', 'gif')),
              'message' => 'Only jpeg, png and gif images are supported',
        )
),

这就是我的控制器中的

if($this->request->is('post')){
    $this->Setting->set($this->request->data);
    if($this->Setting->validates() == true){
        $this->Session->setFlash('success');    
    }
    else{
        $this->Session->setFlash('fail');
    }
}

如果我上传图片,请说image2.jpg 失败消息,但我收到验证消息。如果我从扩展名列表中删除 .jpg并上传jpg文件,则会收到失败消息和验证错误消息。

If I upload, say image2.jpg I get the 'fail' message but I don't get a validation message. If I remove '.jpg' from the extensions list and upload a jpg file, I then get the 'fail' message and the validation error message.

调试时->请求->数据,我得到以下响应

When I debug this->request->data I get the following response

array(
'Setting' => array(
    'id' => '5', //hidden field
    'current_image' => '2_52547d56-1d80-410e-b739-09cecbdd56cb', //hidden field
    'image' => array(
        'name' => 'IMG_4385.JPG',
        'type' => 'image/jpeg',
        'tmp_name' => '/Applications/XAMPP/xamppfiles/temp/phpilL54j',
        'error' => (int) 0,
        'size' => (int) 1608456
    )
)
)

正如我所说,当我从验证规则中删除jpg扩展名时,该消息节目。我知道这可能是发现已发布数据中的图像。

As I stated, when I remove the jpg extension from the validation rules, the message shows. I know it's probably "finding" the image in the posted data.

编辑-我发现了问题

我正在使用的视图有2种形式。一种用于提交图像,另一种用于文本输入。在设置模型中,我对文本输入和图像进行了验证。尽管表格是完全分开的,但似乎仍在尝试验证这些文本输入,但在发送的数据中没有收到任何有关它们的信息。我将验证调用更改为仅验证图像,并且验证了图像

The view I'm working with has 2 forms. One for submitting images, and one for text input. In the Setting model I have validation for the text inputs and the image. Despite being completely separate forms it appears that it was trying to validate those text inputs but it wasn't receiving any information for them in the sent data. I changed the validation call to only validate the image and it worked

if($this->Setting->validates(array('fieldList' => array('image'))) == true){...


推荐答案

我想出了答案。无论如何,谢谢您的提示。

I figured out the answer. Thanks for your tips anyway.

我正在使用的视图有2种形式。一种用于提交图像,另一种用于文本输入。在设置模型中,我对文本输入和图像进行了验证。尽管表格是完全分开的,但似乎仍在尝试验证这些文本输入,但在发送的数据中没有收到任何有关它们的信息。我将验证调用更改为仅验证图像,并且验证了图像

The view I'm working with has 2 forms. One for submitting images, and one for text input. In the Setting model I have validation for the text inputs and the image. Despite being completely separate forms it appears that it was trying to validate those text inputs but it wasn't receiving any information for them in the sent data. I changed the validation call to only validate the image and it worked

if($this->Setting->validates(array('fieldList' => array('image'))) == true){
    //do stuff
}

这篇关于CakePHP的表单验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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