文件上传验证只在记录创建,不编辑 [英] File Upload validation only on record create, not edit

查看:111
本文介绍了文件上传验证只在记录创建,不编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单允许文件上传,我使用数据验证来检查它是什么类型的文件,这是好的。我想要做的是允许他们编辑同一个记录,但不需要当时的文件上传。

I've got a form that allows a file upload and I'm using data validation to check what type of file it is and that's all good. What I want to do is then allow them to edit that same record but a file upload at that time is not needed.

我的验证规则如下:

    'header_pic' => array(
        'extension' => array(
            'rule' => array('extension', array('jpeg', 'jpg', 'gif', 'png')),
            'message' => 'You must supply a GIF, PNG, or JPG file.',
        )
    )

但是这个规则需要一个文件。我发现了'on'参数,我可以将其添加到此规则,但它只会检查文件创建。不编辑。

But this rule requires a file. I found the 'on' parameter and I could add it to this rule but it would then only check files on creation. Not on editing.

我尝试了但是无法使用:

I tried this but it didn't work:

   'header_pic' => array(
        'extension' => array(
            'rule' => array('extension', array('jpeg', 'jpg', 'gif', 'png')),
            'required' => false,
            'allowEmpty' => true,
            'message' => 'You must supply a GIF, PNG, or JPG file.',
        ),
        'notEmpty' => array(
            'rule' => array( 'notEmpty'),
            'on' => 'create',
            'message' => 'You must supply a file.',
        ),
    )

我缺少什么? (先谢谢!)

What am I missing? (Thanks in advance!!)

推荐答案

我想出来了。 (感谢omabena和RSK的建议。)我想出了我可以设置两个验证规则,一个用于添加和一个用于创建。工作相当干净。

I figured it out. (Thanks to omabena and RSK for their suggestions.) I figured out I can just set up two verification rules and use one for the add and one for the create. Works pretty cleanly.

   'header_pic' => array(
        'extension' => array(
            'rule' => array('extension', array('jpeg', 'jpg', 'gif', 'png')),
            'message' => 'You must supply a GIF, PNG, or JPG file.',
            'required' => false,
            'on' => 'add'
        ),
        'extension2' => array(
            'rule' => array('extension', array('jpeg', 'jpg', 'gif', 'png')),
            'message' => 'You must supply a GIF, PNG, or JPG file.',
            'required' => true,
            'on' => 'create'
        ),
    )

这篇关于文件上传验证只在记录创建,不编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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