2

我是 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;
            }
        }
    }
}
?>

模型:

<?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>

验证不起作用。

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

编辑:

这是databse.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 ) )

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

4

3 回答 3

2

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
      }
    }
  }
}
?>
于 2012-09-12T07:45:02.310 回答
1

我在您的代码中注意到您在模型中编写的一件事

public var $validate=array();

而是尝试

public $validate= array() or var $validate=array();

验证应该在单词之后起作用。谢谢 :)

于 2013-02-18T04:52:16.150 回答
0

试试这个:

if ($this->Task->validates()) {
            // it validated logic
            //echo "ttt";
} else {

     $this->validateErrors($this->Task);
     $this->render();
}
于 2012-09-12T08:16:41.697 回答