cakephp 2验证错误消息不显示

时间:2016-06-10 21:37:03

标签: cakephp-2.5

这是我的模特 Contacts.php

<?php

App::uses('AppModel', 'Model');
App::uses('Validation', 'Utility');

/**
 * Base Application model
 *
 * @package  Croogo
 * @link     http://www.croogo.org
 */
//$validate = new Validator();
//             'alphaNumeric' => array(
//             'required' => true,
//             'allowEmpty' => false,


class Contacts extends AppModel {
    var $name = 'Contacts';
    var $useTable = false;

    var $validate = array(
        'ContactsAddress' => array(
             'rule' => 'notEmpty',
             'required' => true,
             'message' => 'The address is required'),
        'ContactsEmail' => array(
                  'rule' => array('email', true),
                  'required' => true,
                  'message' => 'A valid email is required'
              ),
        'ContactsPhone' => array(
                 'rule' => array('phone', null, 'us'),
                 'message' => 'A valid phone numbe is required'
             ) 
    );
    //}
}

ContactsController.php

App::uses('AppController', 'Controller');
App::uses('Validation', 'Utility');

class ContactsController extends AppController {
  public $helpers = array('Form', 'Html', 'Session', 'Js', 'Time');
  public $uses = array('Contacts');
/**
 * This controller does not use a model
 *
 * @var array
 */
    //public $uses = array();
//public $uses = array('Contact');
//use  Cake\Validation\Validator;
/**
 * Displays a view
 *
 * @return void
 * @throws NotFoundException When the view file could not be found
 *  or MissingViewException in debug mode.
 */
  public function index() {
    $this->Contacts->set($this->request->data);
    if($this->request->is('post')) {
       //if ($this->Contact->validation()) {
      if ($this->Contacts->validates()) {
      //    return 
          $this->Contacts->save();
          $this->redirect('/contacts/confirm');
      }  else {
CakeLog::write('debug', 'ErrorCheck');
 //      $errors = $this->Contacts->validationErrors;

        //$this->Session->setflash($this->Contacts->validationErrors);
        //$this->redirect('/contacts');
//CakeLog::write('debug', $errors['ContactsAddress'][0]);
// Debugger::dump($errors);
         }
      }
    }

视图文件 index.ctp

<!--Navigation Background Part Starts -->
<div id="navigation-bg">
    <!--Navigation Part Starts -->
    <div id="navigation">
        <ul class="mainMenu">
            <li><a href="/"  title="Home">Home</a></li>
            <li><a href="about" title="About">About</a></li>
            <li class="noBg"><a href="contact" class="selectMenu" title="Contact">Contact</a></li>
        </ul>
        <a href="contact" class="signup" title="APPOINTMENT"></a>
        <br class="spacer" />
    </div>
    <!--Navigation Part Ends -->
</div>
<div id="ourCompany-bg">
<div class="requestForm">
<p class="formHeader">Meeting Location</p>
<?php echo $this->Form->create(false); ?>
<!--, array('url' => false)--> 
<!-- array('action' => 'confirm'))); ?> -->
<?php //echo $this->Form->create(false, array('url' => array('action' => 'index'))); ?>
<?php $today = date('d')+1; ?>
<?php $formmonth = date('m'); ?>
<?php echo $this->Form->input('name', array( 
'label' => array('text' => 'Name:  '))); ?>
<span class="errorMessage"> <?php //echo $this->validationErrors['Contacts']['ContactsAddress'][0];?></span>
<?php echo $this->Form->input('address', array(
'label' =>  array('text' => 'Address of meeting:  '))); ?>
<?php CakeLog::write('debug', $this->validationErrors['Contacts']['ContactsAddress'][0]); ?> 

你可以看到我一直在尝试很多不同的事情。我一直试图弄清楚这几周没有进展。

我很感激有人可以解决我的问题

1 个答案:

答案 0 :(得分:0)

显然,最重要的因素之一是将模型名称作为Form-&gt; create中的第一个属性。由于我不想发布到数据库,我使用了false,我错误地认为这意味着没有SQL更新。一旦我将模型名称作为第一个属性,它就像记录的那样工作。没有数据库验证的方法是在模型中放置$ userTable = false。

希望这有助于某人。