jjzjj

php - saveMany 验证无法正常工作

coder 2024-04-18 原文

我正在尝试使用最新的 2.x CakePHP 版本来执行此操作,我正在尝试通过验证来上传多个文件表单,但无法正常工作...

DocsController.php( Action 添加)

if ($this->request->is('post')) {
    if ($this->Doc->saveMany($this->request->data, array('deep' => true))) {
        $this->Session->setFlash(__('Ok'));
    } else {
        debug($this->Doc->validationErrors); die();
        $this->Session->setFlash(__('Error'));
    }
    $this->redirect($this->referer());
}

添加.ctp

<?php
echo $this->Form->create('Doc', array('type' => 'file'));
echo $this->Form->input('files.', array(
    'label' => __("New document",true),    'type' => 'file', 
    'div' => 'input text no',
    'multiple' => 'multiple',
    'required' => true,
));
echo $this->Form->end('Upload');
?>

Doc.php

class Doc extends AppModel {

    public $belongsTo = array(
        'Project' => array(
            'className' => 'Project',
            'foreignKey' => 'project_id',
        )
    );

        public $validate = array(
        'files' => array(
                'rule' => array('extension', array('pdf')),
                'required' => false,
                'allowEmpty' => true,
                'message' => 'Only pdf files'     
        ),
    );
}

数组

这个错误是正常的:

Array
(
    [Doc] => Array
        (
            [files] => Array
                (
                    [0] => Array
                        (
                            [name] => images.jpeg
                            [type] => image/jpeg
                            [tmp_name] => /private/var/tmp/phpSqerRI
                            [error] => 0
                            [size] => 5740
                        )

                )

        )

)

/app/Controller/DocsController.php (line 112)
array(
    'Doc' => array(
        'files' => array(
            (int) 0 => 'Only pdf files'
        )
    )
)

这就是问题所在:

Array
(
    [Doc] => Array
        (
            [files] => Array
                (
                    [0] => Array
                        (
                            [name] => 4_54718093804437603.pdf
                            [type] => application/pdf
                            [tmp_name] => /private/var/tmp/phpCfUUDx
                            [error] => 0
                            [size] => 1441232
                        )

                )

        )

)

/app/Controller/DocsController.php (line 112)
array(
    'Doc' => array()
)

它是 PDF,所以没问题,但验证返回数组

如果没有违反规则,为什么要返回数组?

编辑 1

错误来自 saveMany,因为检查验证说“好的”...但没有更多信息 atm.....

$this->Doc->set($this->request->data);
if ($this->Doc->validates()) {
    // success
    die("Ok");
} else {
    // failed
    $errors = $this->Doc->validationErrors;
    die(print_r($errors));
}

提前致谢,对不起我的英语。

最佳答案

cake2中saveMany的保存格式有问题:)

数组应该像 0 => Doc => array ('fieldname')

关于php - saveMany 验证无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31193626/

有关php - saveMany 验证无法正常工作的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  3. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  4. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  5. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  6. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  7. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  8. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  9. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  10. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

随机推荐