在cakephp中的用户模型中,自定义表单中未显示验证错误

时间:2013-12-27 06:38:33

标签: php cakephp cakephp-2.0

we have created sign up form in html, but validation from model are not showing in form
but if we create form in simple php as given in documentation validation are working.
so how should we add custom validation from users model in signup form.

我已尝试过stackoverflow的几个解决方案但没有一个正常工作。    我想在输入框

下面的表单中显示模型类中写的错误消息

这是我的代码

 <div id="content">
  <!DOCTYPE html>
  <body>
   <?php echo $this->Session->flash('auth'); ?>
     <div class="container">
        <section>   
            <div id="container_demo" >       
              <div id="wrapper">
                 <div id="login" class="animate form">
             <form  action="/googleAtIntgeration/users/add" autocomplete="on" 
             method="post"> 
          <h1> Sign up </h1> 
         <div class ="flashmsg" ><?php echo $this->Session->flash(); ?></div>
         <p> 
      <label for="usernamesignup" class="uname" data-icon="u">Username<span 
        class="required">*</span></label>
       <input id="usernamesignup" name="data[User][username]" required="required" 
          type="text" placeholder="mysuperusername690" />
          </p> 
        <p <?php echo $this->Form->error('username') ?></p>
           <p> 
         <label for="password" class="youpasswd" data-icon="p">Password<span 
        class="required">*</span> </label>
        <input id="password" name="data[User][password]" required="required" 
         type="password" placeholder="eg. X8df!90EO" /> 
          </p>
        <p> <?php echo $this->Form->error('password') ?></p>
        <p> 
      <label for="passwordsignup_confirm" class="youpasswd" data-icon="p">Confirm 
       Password<span class="required">*</span> </label>
        <input id="passwordsignup_confirm" name="data[User][confirm_password]" 
       required="required" type="password" placeholder="eg. X8df!90EO"/>
        </p>
       <p><?php echo $this->Form->error('confirm_password') ?></p>
        <p> 
        <label for="emailsignup" class="youmail" data-icon="e" >Email<span 
        class="required">*</span></label>
      <input id="emailsignup" name="data[User][email]" required="required" type="email" 
      placeholder="mysupermail@mail.com"/> 
       </p>
      <p> <?php echo $this->Form->error('email') ?></p>
        <p>
       <div class="input select"><label for="country">Country</label>
     <select name="data[User][country]" class="inputbox" id="country"><option 
      value="">Select Country</option>
      <?php echo $this->User->countryDropDown() ?>
     </select>
     </div>
      </p>
      <p class="signin button"> 
       <input type="submit" value="Sign up"/></p>
       <p class="change_link"> 
        Already a member ?
<a href="/googleAtIntgeration/users/login" class="to_register"> Go and log in </a>
        </p>
<input name="activity_code" type="hidden" value="REG"/>
                        </form>
                    </div>                  
                </div>
            </div>  
        </section>
        </div>
      </body>
      </html>   
     </div>

所以请提出一些解决方案。      $ errors = $ this-&gt; User-&gt; validationErrors;添加到控制器中。

1 个答案:

答案 0 :(得分:1)

您需要遍历视图文件中的错误。

尝试在控制器中添加以下内容:

<?php
    $errors = $this->User->validationErrors;
    $this->set('errors', $errors);
?>

尝试在View文件中的正文标记之后添加以下内容,该文件以.ctp结尾:

<?php debug($errors); ?>

这可以让你了解它的工作原理。

另外,请参考以下问题/答案:

Use Cakephp Validation with custom form / without formhelper

我强烈建议你停止操作CakePHP,并以“CakePHP方式”使用表单助手,这就是他们在那里的原因。起初他们使用起来很苛刻,没有意义,但是一旦你学会了如何使用它们,你就会发现它们节省了时间......