2016-04-23 127 views
0

如果驗證錯誤和錯誤驗證消息也未顯示在前端,則用戶數據已丟失。cakephp驗證後保留數據錯誤

這裏是我的控制器

public function add() { 
     $this->theme = 'Front'; 
     if (!empty($this->logged_in)) { 
      return $this->redirect(Router::url('/', true) . $this->logged_in['Role']['alias']); 
     } 
     $this->set('title_for_layout', __('Create An Account')); 

// hanlde post data 
     if ($this->request->is("post")) { 
      if (!empty($this->request->data)) { 
       $this->User->set($this->request->data); 
       if (!$this->User->validates()) { 
        $this->User->validationErrors; 
        $this->Session->setFlash(__('The Information could not be saved. Please, try again.'), 'message', array('class' => 'danger')); 
        $this->redirect(Router::url('/', true) . 'sign-up/'); 
       } 
      } 

//     die('sdsddsaf'); 
      // if (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 2) { 
      unset($this->request->data['User']['specialization_id']); 
      $randomSt = $this->Custom->randomString(28); 
      $this->request->data['User']['activation_key'] = $randomSt; 
      // } 


      if ($this->User->save($this->request->data)) { 
       $user = $this->request->data; 
       if (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 3) { 
        $this->Session->setFlash(__('Thank you for registering with us. Please check your email inbox/junk.'), 'message', array('class' => 'successs')); 
// set mail variable 
        $to = $this->request->data['User']['email']; 
        $fname = $this->request->data['User']['first_name']; 
        $lname = $this->request->data['User']['last_name']; 
//$activelink = SITEURL . 'activate?code=' . $randomSt; 

        $siteurl = Configure::read('Site.title'); 

        $replace = array($fname, $lname, $siteurl); 
// Send mail on Registration 
        $this->sendMail($to, 'RegisterDoctor', $replace); 
       } else { 
        $this->Session->setFlash(__('Thank you for registering with us. Please check your e-mail inbox/junk as your e-mail confirmation has just been sent.'), 'message', array('class' => 'successs')); 
        $activelink = Router::url('/', true) . 'activate/' . $this->User->id . '/' . $randomSt; 
// set mail variable 
        $to = $this->request->data['User']['email']; 
        $fname = $this->request->data['User']['first_name']; 
        $lname = $this->request->data['User']['last_name']; 
//$activelink = SITEURL . 'activate?code=' . $randomSt; 

        $siteurl = Configure::read('Site.title'); 

        $replace = array($fname, $lname, $activelink, $siteurl); 
// Send mail on Registration 
        $this->sendMail($to, 'Register', $replace); 
       } 


       $this->redirect(array('controller' => 'users', 'action' => 'login')); 
      } 
     } 

     $reponse = array(); 

     $roles = $this->User->rolesSignup(); 
     $genders = $this->User->genders(); 
     $specializations = $this->User->specializations(); 
     $this->set(compact('roles', 'genders', 'specializations')); 
    } 

而且下面是我的看法

<?php 
       echo $this->Form->create('User', array(
        'inputDefaults' => array('div' => false, 'label' => false, 'fieldset' => false), 
        'class' => 'ac p10', 
        // 'data-togglesd' => "validator", 
        'role' => 'form', 
        'type' => 'file', 
        'onSubmit' => 'return checksubmitPass()' 
         ) 
       ); 
       ?> 
       <div class="ph10"> 
        <div class="btn-group radio_group w100" data-toggle="buttons"> 
         <label class="btn btn-primary col-md-6 <?php echo (empty($this->request->data['User']['role_id']) || @$this->request->data['User']['role_id'] == 2) ? 'active' : ""; ?> btn_roles"> 
          <input type="radio" name="data[User][role_id]" value="2" class="role_id" <?php echo (empty($this->request->data['User']['role_id']) || @$this->request->data['User']['role_id'] == 2) ? 'checked="checked"' : ""; ?>> User 
         </label> 
         <label class="btn btn-primary col-md-6 <?php echo (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 3) ? 'active' : ""; ?> btn_roles"> 
          <input type="radio" name="data[User][role_id]" value="3" class="role_id" <?php echo (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 3) ? 'checked="checked"' : ""; ?>> Doctor 
         </label> 

        </div> 
       </div> 
       <div class="form-group p10 cr"> 
        <div class="cm upload_img round"><img src="<?php echo $this->webroot; ?>img/user_avtar(upload).png" class="user_pic_upload"> 
         <?php echo $this->Form->file('image', array('class' => 'user_img', 'data-fv-file' => 'true', 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-field' => 'data[User][image]')); ?> 

        </div> 
        <small style="display: none;" class="help-block" data-fv-validator="notEmpty" data-fv-for="data[User][image]" data-fv-result="VALID">This field cannot be left blank.</small> 
       </div> 
       <div class="form-group mt10 cr"> 
        <div class="clearfix"> 
         <div class="col-lg-6 first_name"><?php echo $this->Form->input('first_name', array('class' => 'form-control', 'placeholder' => 'First Name', 'data-stripe' => "alphabet", 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-regexp' => 'true', 'data-fv-regexp-regexp' => "^[a-zA-Z\s]+$", 'data-fv-regexp-message' => "The first name can consist of alphabetical characters and spaces only")); ?></div> 
         <div class="col-lg-6 last_name"><?php echo $this->Form->input('last_name', array('class' => 'form-control', 'placeholder' => 'Last Name', 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-regexp' => 'true', 'data-fv-regexp-regexp' => "^[a-zA-Z\s]+$", 'data-fv-regexp-message' => "The last name can consist of alphabetical characters and spaces only", 'required')); ?></div> 
        </div> 
       </div> 
       <div class="form-group ph10 mt10"> 
        <?php echo $this->Form->input('email', array('class' => "form-control", 'placeholder' => 'Email Address', 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-emailaddress-message' => __('validemail'))); ?> 
       </div> 
       <div class="form-group mt10 ph10 row_org"> 
        <div id="dropdown-menu"> 
         <?php echo $this->Form->input('specialization_id', array('empty' => 'Select Speciallization', 'class' => 'selectpicker form-control', 'data-fv-notempty-message' => __('notEmpty'), 'required')); ?>         
        </div> 
       </div> 
       <div class="form-group ph10 mt10"> 
        <?php echo $this->Form->input('password', array('class' => "form-control", 'placeholder' => 'Password', 'type' => 'password', 'data-fv-notempty-message' => __('notEmpty'))); ?> 
       </div> 
       <span id="result"></span> 
       <div class="form-group ph10 mt10"> 
        <?php 
        echo $this->Form->input('verify_password', array(
         'class' => "form-control", 
         'type' => 'password', 
         'placeholder' => 'Confirm Password', 
         'data-fv-notempty-message' => __('notEmpty'), 
         'data-fv-identical' => "true", 
         'data-fv-identical-field' => "data[User][password]", 
         'data-fv-identical-message' => __('passwordNotMatch') 
          ) 
        ); 
        ?> 
       </div> 
       <div class="form-group mt10"> 
        <div class="clearfix"> 
         <div class="col-lg-6 mobile-custom"> 
          <?php echo $this->Form->input('mobile', array('class' => 'form-control phonenumber', 'placeholder' => 'Mobile number', 'maxlength' => '14', 'minlength' => '10', 'data-fv-stringlength-message' => 'The mobile number must be 10 to 14 characters long', 'data-fv-numeric-message' => __('Please enter valid mobile numbers'), 'data-fv-numeric' => "true")); ?> 

         </div> 
         <div class="col-lg-6" id="dropdown-menu"> 
          <?php //echo $this->Form->input('gender', array('class' => 'form-control selectpicker', 'data-fv-notempty-message' => __('notEmpty'),'empty' => 'I Am','error' => false, 'required')); ?> 
          <select id="UserGender" title="I Am" required="required" data-fv-notempty-message="This field cannot be left blank." class="form-control" name="data[User][gender]" style="display: none;" data-fv-field="data[User][gender]"> 

           <option value="Male">Male</option> 
           <option value="Female">Female</option> 
           <option value="Other">Other</option> 
          </select> 
         </div> 
        </div> 
       </div> 
       <div class="form-group ph10 mt10"><button type="submit" class="btn btn-info w100">Sign Up</button></div> 
       <p class="mt10"><small>By Signing up you are agree with <br><a href="/terms">Terms &amp; Conditions</a> and <a href="/privacy-policy">Privacy Policy</a></small></p> 
       <p class="mt10 signup-text"><strong>Already on HealthDrop?</strong></p> 
       <div class="ph10"> 
        <?php echo $this->Html->link('Sign In', Router::url('/', true) . 'login', array('class' => 'btn deep btn-primary w100')); ?> 
       </div> 
       <?php echo $this->Form->end(); ?> 

它BU工作之前突然停下來示值誤差,並開始丟失數據。 我檢查this問題,但它指出在輸入字段名稱,我有正確的。 任何幫助欣賞。

+0

越來越任何錯誤? – urfusion

+0

是的,如果我發佈了錯誤的數據,有幾個驗證錯誤。但對於正確的數據,它工作正常。 –

+0

我正在密切關注驗證,發現你正在通過'!''驗證if(!$ this-> User-> validates()){'如果它沒有錯字,那麼嘗試刪除'!'。 – urfusion

回答

0

你丟失了你的數據,因爲有重定向到sign-up url。

$this->redirect(Router::url('/', true) . 'sign-up/'); 

和重定向後

$this->request->data 

因爲執行新的要求是空的,有錯誤的

+0

好吧,那麼如何保存數據,我需要發送用戶註冊頁面,以便他可以修復數據並重新提交。 –

+0

你的User :: add方法應該使用註冊表單來呈現一個視圖。如果會出現一些錯誤,表單會再次顯示您的數據和錯誤。正確的URL可以在路由表中設置。註冊失敗後重定向不是必需的 – Bart

+0

你有'add'方法。我很新蛋糕 –

0

根據你的代碼有兩個錯誤,我發現

  1. 同你正在使用!驗證驗證
  2. 保存方法本身驗證數據,因此不需要再次驗證。

所以,你的代碼應該看起來像

if ($this->User->save($this->request->data)) { 
    //your code 
}else{ 
    $this->Session->setFlash(__('The Information could not be saved. Please, try again.'), 'message', array('class' => 'danger')); 
    $this->redirect(Router::url('/', true) . 'sign-up/'); 
} 

和驗證錯誤消息被自動分配到查看,沒有必要擔心他們

+0

感謝您的建議。我正在實施這種方法。 –