CakePHP : Validation message not displaying

前端 未结 3 1517
暗喜
暗喜 2021-02-06 17:19

I\'m new to cakePHP and I\'ve made a simple form following some tutorial. On this html form I\'ve used validation. Now the problem is that the validation is working but the mess

相关标签:
3条回答
  • 2021-02-06 17:52

    That's built-in browser validation.

    Since 2.3 the HTML5 required attribute will also be added to the input based on validation rules.

    Your title has the notEmpty rule, so Cake is outputting

    <input type="text" required="required" ..

    and your browser is triggering that message.

    Edit: to override this behaviour, you can do:

    $this->Form->input('title', array('required'=>false));
    

    or

    $this->Form->submit('Submit', array('formnovalidate' => true));
    

    When you submit the form, your model validation will fire.

    0 讨论(0)
  • 2021-02-06 17:57

    Your Form-create() options are invalid, first argument is the model-name, second is for options:

    <h2>Add New Post</h2>
    <?php
         echo $this->Form->create('Post', array('action'=>'add'));
         echo $this->Form->input('title');
         echo $this->Form->input('body');
         echo $this->Form->end('Create Post');
    ?>
    

    If the form-helper does not know which 'model' it is creating a form for, I won't check for field validation in the right place, hence, it won't output the validation errors for 'title'

    [update] solution above didn't solve the problem. OP has modified the question

    Some ideas:

    1. Be sure to enable 'debug' (App/Config/core.php set Configure::write('debug', 2); Otherwise CakePHP may be using a 'cached' version of your model.

    2. If you've named your Model incorrectly, Cake may be automatically generating a model for you, in which case your own model is never actually used, try this for debugging to see if we even 'get' to your model:

    Add this to your model;

    public function beforeValidate($options = array())
    {
         debug($this->data); exit();
    }
    
    0 讨论(0)
  • 2021-02-06 18:06

    From your code what i can see is that you havent included helpers.

    public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Session');
    

    Just add to your controllers and try..

    0 讨论(0)
提交回复
热议问题