yii验证规则allowEmpty不起作用(yii validation rule allowEmpty not working)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
yii验证规则allowEmpty不起作用(yii validation rule allowEmpty not working)

我正在YII的管理部分中上传图片,

为了实现这一点,我必须将它包含在模型的rules函数中。

然而,我可以使表单工作的唯一方法是上传图像

public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( // Other rules array('slider_bg_image', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true), ); }

当allowEmpty设置为true ,我得到一个空白屏幕,但当我将其设置为false我被迫上传图像。

我为前端打开了错误,但后端似乎无法执行此操作。

谢谢

编辑:

<div class="row hide"> <div class="span4"><label>Slide Background Image</label></div> <div class="span5"><?php echo $form->fileField($model,'slider_bg_image'); ?></div> <div class="span3"><?php echo $form->error($model,'slider_bg_image'); ?></div> </div>

I am uploading images in a form in an admin section in YII,

To get this working I must include it in the rules function in the model.

However the only way I can get the form working is to upload an image

public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( // Other rules array('slider_bg_image', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true), ); }

I get a blank screen when allowEmpty is set to true, but when I have it as false I am forced to upload the image.

I have errors turned on for the front end ,but cannot seem to do so for the backend.

Thank you

EDIT:

<div class="row hide"> <div class="span4"><label>Slide Background Image</label></div> <div class="span5"><?php echo $form->fileField($model,'slider_bg_image'); ?></div> <div class="span3"><?php echo $form->error($model,'slider_bg_image'); ?></div> </div>

最满意答案

上面的代码没有错,

我只是试图在控制器中使用不存在的数据(因为图像无法上传),所以我收到错误...

PHP致命错误:在/storage/www/mysite/core/protected/modules/admin/controllers/CustompageController.php中的非对象上调用成员函数saveAs()

所以我添加了控制器中的条件以防止错误。

There was nothing wrong with the code above,

I was simply attempting to use data in the controller that didnt exist (as an image wouldnt be uploaded), so I was receiving the error...

PHP Fatal error: Call to a member function saveAs() on a non-object in /storage/www/mysite/core/protected/modules/admin/controllers/CustompageController.php

So I added in conditions in the controller to prevent the error.

更多推荐

本文发布于:2023-04-17 08:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/6ec9ffa95361eff510fd161f585f9195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   规则   allowEmpty   yii   rule

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!