2017-07-26 20 views
0

我正在使用註冊系統,並且我使用angularJ作爲前端,codeigniter作爲後端。我試圖做一個雙向驗證(客戶端和服務器),但有一些問題驗證angularJs發送到我的代碼控制器的數據。 Angular在驗證後成功地將數據發佈到數據庫,但是當我嘗試進行第二次驗證時,發送到數據庫的數據是Null。現在我的問題是,我如何在服務器上進行第二次驗證,以便出於安全原因應用程序可以同時進行驗證。如何在保存到數據庫之前通過angularJs發佈的數據對coodeigniter進行第二次驗證

ANGULAR JS手柄這裏輸入

 $scope.registration = { 
     fullName: '', 
     email: '', 
     password: '', 
     country: '', 
     gender: '', 
     town: '', 
     refEmail: '' 
    } 
    $scope.registerSuccess = ""; 
    $scope.registerFailed = ""; 

    //Function below submits registration form to the server after validations 

    $scope.submitRegisterForm = function(){ 
     $scope.dataloading = ''; 
     console.log($scope.registration); 
     $http({ 
      method: 'POST', 
      url: 'index.php/register/registerUser', 
      data: $scope.registration, 
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 

     }).then(function(data){ 
       console.log(data); 
       $scope.registerSuccess = data; 
       $scope.message = data; 
       $scope.registration = {}; 
      }) 
     .error(function(data){ 
      console.log(data); 
      $scope.registerFailed = data; 
     }) 
    } 

我的控制器

$request= json_decode(file_get_contents('php://input'), TRUE); 
    $fullName = $request->fullName; 
    $email = $request->email; 
    $password = $request->password; 
    $refEmail = $request->refEmail; 
    $gender = $request->gender; 
    $country = $request->country; 
    $town = $request->town; 
    echo $fullName; 

    $checkName = $this->form_validation->set_rules($fullName, 'Full Name', 'trim|required|min_lenght[5]|is_unique[users.full_name]'); 
    $checkName = $this->form_validation->set_rules($email, 'Email Address', 'trim|required|min_lenght[5]|valid_email|is_unique[users.email]'); 
    $checkName = $this->form_validation->set_rules($password, 'User Password', 'trim|required|min_lenght[5]'); 
    $checkName = $this->form_validation->set_rules($refEmail, 'Referrer Email ID', 'trim|required'); 
    $checkName = $this->form_validation->set_rules($gender, 'Gender', 'trim|required'); 
    $checkName = $this->form_validation->set_rules($country, 'Country', 'trim|required'); 
    $checkName = $this->form_validation->set_rules($town, 'Town', 'trim|required'); 

    if ($this->form_validation->run() == FALSE) { 
     $validate = array('error' => validation_errors()); 
     $this->session->set_flashdata($validate); 
     echo json_encode($validate); 
    }else{ 
    $this->load->model('user'); 
    $result = $this->user->saveUser($request); 

    if ($result) { 
     echo json_encode(array('success' => true, 'message'=> 'Registration successfull')); 
     // $this->load->view('users_view/email_confirmation_view_message', array('email'=>$result)); 
    }else{ 
     // this should never happen 
     echo json_encode(array('success'=>false, 'message'=> 'Registration Failed')); 
    } 
    } 

回答

0

沒有那麼多的答案 - 但贈言啓動。無法發表評論:)

我發現在API時尚中使用codeigniter有點困難。試圖這樣做時,我寫了自己的驗證。

你並不需要分配

$this->form_validation->set_rules() 

給一個變量。

您echo'ing在開始回

echo $fullName; 

,讓您的AJAX調用會收到此作爲其資料。

您不應該需要flashdata,因爲您可以從前端處理您從Ajax調用返回的數據。

旁註:如果在期待返回'echo'後還有其他條件,則退出()以停止函數流。很好寫這個IE的幫手函數叫做:

returnJson($response) 

它會在echo和json_encodes數據後退出。

我們能否看到您的數據庫輸入模型?這個問題很可能在那裏。

+0

只需重新閱讀並看到您將'true'作爲第二個參數傳遞給json_decode()。我不明白如果沒有拋出警告信息,這將如何工作。通過'true'會將'$ request'的結果轉換爲關聯數組,而不是對象。因此你不能調用'$ fullName = $ request-> fullName; $ email = $ request-> email; $ password = $ request-> password; $ refEmail = $ request-> refEmail; $ gender = $ request-> gender; $ country = $ request-> country; $ town = $ request-> town;'由於屬性不存在。這將是'$ request ['email']'等等。 – Rossidge

+0

是的,我也嘗試用戶請求['電子郵件']獲取JSON解碼數據,以便我可以繼續進行服務器驗證,但一切都到服務器爲空 – Ebune

+0

當我不應用任何服務器驗證,數據正確保存在數據庫中。 – Ebune

相關問題