CakePHP 验证不起作用

2024-01-01

我是 cakephp 的新手,我需要验证表单。

这是代码: 控制器:

<?php
class TasksController extends AppController {
    var $name = 'Tasks';
    var $helpers = array('Html','Form','Session');
     public function index(){
     }
    function add_task()
    {
        if(!empty($this->data)) {
            //print_r($this->data);
            $this->Task->set($this->data);
            if ($this->Task->validates()) {
                // it validated logic
                //echo "ttt";
            } else {
                // didn't validate logic
                echo $errors = $this->Task->validationErrors;
            }
        }
    }
}
?>

Model:

<?php
    class Task extends AppModel
    {
        public var $name = 'Task';
        var $useDbConfig = 'travanco_erp';
        public var $useTable = 'tbl_tasks'; // This model uses a database table 'exmp'
        public var $validate = array(
                    'task_title_mm' => array(
                            'rule' => 'notEmpty',
                            'required' => true,
                            'message' => 'The title field is required'
                    ),
                    'task_description_mm' => array(
                            'rule' => 'notEmpty',
                            'required' => true,
                            'message' => 'The description field is required'
                    ),
                    'task_from_mm' => array(
                            'rule' => 'notEmpty',
                            'required' => true,
                            'message' => 'The from date field is required'
                    ),
                    'task_to_mm' => array(
                            'rule' => 'notEmpty',
                            'required' => true,
                            'message' => 'The to date field is required'
                    )
            );

    }
?>

这是视图:

<div class="employeeForm" style="width:64%; padding:10px 30%;"> 

            <?php echo $this->Form->create('test', array('class'=>'form'));?>
            <fieldset style="width:36em; padding:0px 0px;">
                    <div style="width:475px; font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#333333; font-weight:bold; margin-left:20px; margin-top:10px;">Add Task</div>
                    <br/>
                <?php
                    /*echo $this->Form->input('task_ids_mm',        array(  'div'=>'frm_filed_new',
                                                                    'error' => array(   'wrap' => 'div',
                                                                                        'class' => 'formerror'
                                                                                    ),
                                                                    'label' => 'Task ID',
                                                                ));*/


                    echo $this->Form->input('task_title_mm',        array(  'div'=>'frm_filed_new',
                                                                    'error' => array(   'wrap' => 'div',
                                                                                        'class' => 'formerror'
                                                                                    ),
                                                                    'label' => 'Title',
                                                                ));


                    echo $this->Form->input('task_description_mm',  array(  'type' => 'textarea',
                                                                        'cols'=>60,
                                                                        'rows' => 5,
                                                                        'div'=>'frm_filed_new',
                                                                        'error' => array(   'wrap' => 'div',
                                                                                            'class' => 'formerror'
                                                                                    ),
                                                                        'label' => 'Description',
                                                                ));

                    echo $this->Form->input('task_from_mm',     array(  'div'=>'frm_filed_new','id'=>'task_from_mm','value'=>'',
                                                                    'error' => array(   'wrap' => 'div',
                                                                                        'class' => 'formerror'
                                                                                    ),
                                                                    'label' => 'From',
                                                                ));
                    echo $this->Form->input('task_to_mm',   array(  'div'=>'frm_filed_new','id'=>'task_to_mm','value'=>'',
                                                                    'error' => array(   'wrap' => 'div',
                                                                                        'class' => 'formerror'
                                                                                    ),
                                                                    'label' => 'To',
                                                                ));

                ?>  
                <br/>
                <?php echo $this->Form->button('Submit', array('type'=>'submit','escape'=>true)); ?>
            </fieldset>  
            <?php echo $this->Form->end(); ?>

        </div>

验证不起作用。

我的代码有什么错误? 我该如何解决这个问题?

EDIT:

这是database.php文件配置错误的错误。现在已经更正了。print_r($errors)显示错误。但没有显示在视图页面中,我的意思是在文本框附近。

这是错误数组:Array ( [task_title_mm] => Array ( [0] => The title field is required ) [task_description_mm] => Array ( [0] => The description field is required ) [task_from_mm] => Array ( [0] => The from date field is required ) [task_to_mm] => Array ( [0] => The to date field is required ) )

我怎样才能把它放在文本框附近?


CakePHP 旨在自动验证模型并显示验证错误。自动验证在模型保存时运行。在你的情况下:

$this->Task->save($this->request->data);

上面将触发验证。无需运行: $this->Task->validates() - 如果这样做,您还必须自己负责显示验证错误。所以我认为你应该尝试:

<?php
class TasksController extends AppController {
  var $name = 'Tasks';
  var $helpers = array('Html','Form','Session');

  function add_task()
  {

    if ($this->request->is('post')) {
      // If the form data can be validated and saved...
      if ($this->Task->save($this->request->data)) {
         //saved and validated
      }
    }
  }
}
?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CakePHP 验证不起作用 的相关文章

  • 用于验证一个参数的多种类型和值的 json 架构

    请在这件事上给予我帮助 我尝试编写一个 json 模式来验证以下对象 json 对象 param value 可能的值 all 任意整数的数组 所以它是一个简单的 json 对象 其中包含一个变量 可以是字符串 all 也可以是任何整数数组
  • Cakephp + 枚举支持:无法保存或选择枚举 0 和 1

    当我保存具有两个枚举字段的数据来管理消息状态时 即用户已读或未读 我使用枚举 1 0 来管理状态 1 gt 已读和 0 gt 未读 以下代码将保存消息 但在状态栏中保存空文件 data array message gt test messa
  • 将默认消息“验证错误:需要值”更改为“需要值”

    我可以修改这个默认值吗required true 验证消息仅显示 需要值 formId inputId 验证错误 值是必需的 Either使用输入组件的requiredMessage属性
  • Fluent 验证和库

    前几天在寻找完全不同的东西时 我偶然发现了两个用于在 NET 中进行流畅验证的库 这个概念似乎很有趣 因为到目前为止我正在使用通常的条件和分支语句 if else case 等 进行验证 特别是 它使得链接某些条件相对容易 这些条件在某些情
  • 使用注释和 IValidatableObject 进行递归验证

    我正在尝试使用注释和一些自定义代码来验证嵌套对象 不是 MVC 中的模型 我发现以下帖子很有用 手动使用数据注释验证和对象图 https stackoverflow com questions 6938877 using data anno
  • 如何在 WTForms 中使字段有条件可选?

    我的表单验证工作已接近完成 我只有两种情况 我不知道具体如何解决 1 密码字段当然应该是必需的 但我还提供了通过 OAuth 使用 google 或 facebook 帐户登录的可能性 然后名称被预先填充 但我从表单中完全删除密码字段是否有
  • 如何使用自定义 ValidationAttribute 来确保两个属性匹配?

    我们使用 xVal 和标准DataAnnotationsValidationRunner描述here http blog stevensanderson com 2009 01 10 xval a validation framework
  • 使用 Minitest 测试自定义验证器

    我有多个带有电子邮件验证的模型 因此 我将验证提取到自定义验证器中 我按照以下教程做到了这一点导轨指南 http guides rubyonrails org active record validations html custom va
  • MVC 模式中的验证层

    验证模型将使用的数据的最佳位置在哪里 例如 考虑登记表 我们有一些来自注册表的数据 那么验证这些数据的最佳位置在哪里 我们应该通过 if 语句或特殊的验证器类来检查每个数据 这意味着大量的编码 所以我想了解在哪里可以做到这一点 在控制器中
  • 禁用 notInArray 验证器 Zend Framework 2

    有没有办法在 Zend Framework 2 中禁用 notInArray Validator 互联网上的所有信息都显示如何在 Zend Framework 1 中禁用 notInArray Validator 例如以这种方式 如果您根本
  • yup.js 验证数字字段大于同级字段,或者可以为空

    我正在使用 Yup js 来验证一些表单字段 我有两个整数字段 Year Built Year Renovated Year Built是必填字段 Year Renovated is not 不过 装修年份可以留空如果有一个值它应该大于建造
  • CakePHP保存三模型关系关联

    我有以下输出 需要将其插入数据库中 Array Test gt Array Question gt Array 0 gt Array category id gt 3 answer style id gt 2 Answer gt Array
  • jquery验证-等待远程检查完成

    当我打电话时 form valid 我连接了远程验证检查 一切正常 但是如果所有其他字段都有效 则表单会通过验证 因为远程检查没有 足够快 返回响应 有没有办法强制 jquery 验证等待任何远程检查完成或挂钩远程检查调用的完成事件 我目前
  • 蛋糕控制台 2.2.1:烘焙错误

    运行 MAMP 的 OSX 机器 CakePHP 2 2 1 已正确安装和配置 这意味着当我浏览到 Index php 文件时 所有绿色条都显示出来 我已经完成了博客教程 并且正在开发我的第二个应用程序 其中脚手架已启动并运行 现在我第一次
  • Mac 应用程序商店 - 尝试让加密发挥作用。 。 。我缺少什么?

    我正在尝试使用 Alan Quartermain 的解决方案 如该问题所链接 Mac App Store 收据验证码 https stackoverflow com questions 4261348 mac app store recei
  • Spring:自定义验证器未被调用

    我正在查看有关 Spring 自定义验证器的其他问题 但不幸的是我无法用建议的答案解决我的问题 我的问题如下 我有一个实体 帐户 并且创建了一个自定义验证器 AccountValidator 我在控制器 RegisterController
  • cakephp 3 中的 SUM 查询不起作用

    我正在尝试添加同一字段的数据并希望返回我使用以下查询的结果 total this gt Details gt find all array fields gt array sum Details total downtime Details
  • ngModel.$parsers 忽略 ng-model 值末尾的空格

    我有这样的指令 directive noWhitespace parse function parse return restrict A require ngModel link function scope element attrs
  • 提交前验证表单(比检查空字段更复杂)

    我有一个包含时间输入的表单 具体来说 开放时间和结束时间 当按下提交按钮时 它会转到一个 php 页面 其中这些输入将添加到数据库中 在允许提交表单之前我想检查一些事情 例如 我想确保开始时间早于 小于 结束时间 这是表格 Opens
  • 使用 jquery 和 php 测试表单输入是否为 1 或 2 位整数

    我有一个表单 其中有五个字段全部设置为 maxlength 2 基本上 我希望唯一可以输入的值是一位或两位整数 因为在将值存储在数据库中之前对这些字段执行计算 是否有任何 jquery 不允许用户输入不是整数的值 另外 用 jquery 和

随机推荐