CakePHP validation not working

后端 未结 3 783
挽巷
挽巷 2021-01-22 08:13

Iam new in cakephp ,I need to validate a form.

This is the code: Controller:



        
相关标签:
3条回答
  • 2021-01-22 08:27

    Try this:

    if ($this->Task->validates()) {
                // it validated logic
                //echo "ttt";
    } else {
    
         $this->validateErrors($this->Task);
         $this->render();
    }
    
    0 讨论(0)
  • 2021-01-22 08:34

    one thing i noticed in your code that you are writing in your model

    public var $validate=array();
    

    instead try

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

    Validation should work after words. Thanks :)

    0 讨论(0)
  • 2021-01-22 08:44

    CakePHP is designed to automatically validate model and display validation errors. Auto validation runs on model save. In your case:

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

    above will trigger validation. There is no need to run: $this->Task->validates() - If you do so, you also have to take care of displaying validation error by your own. So I think you simply should try:

    <?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
          }
        }
      }
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题