vendredi 4 mars 2016

How to validate and submit form without page refresh laravel and AngularJS

I've a single page website, on the #fifthPage, I've a contact form. In the contact form submit, I'm trying to show the validation error if it fails and show the success message on success. But it is giving me a page refresh.

--view.blade.php

<body ng-app="addressBookApp">
<div ng-controller="addressBookController">
  <form name="addressbook" id="addressbook" ng-submit="doSomeAction()>
    <input type="text" ng-model="name" id="name" name="name" />
      @{{ name }}
  </form>
  <ul>
    <li></li>
  </ul>
</div>

controller ----

public function store(Request $request)
{
    $rules = $this->rules();
    $values = $request->all();
    $messages = [
        'name' => 'Username is required',
    ];

    $validator = \Validator::make($values, $rules);
    if ($validator->fails()) 
    {
        return \Redirect::to('/#fifthPage')->withInput()->withErrors($validator);
    }
    else {
        $contact = new ContactUs;
        $contact->name = $values['name'];
        $contact->save();
        return \Redirect::to('/#fifthPage');
    }
}

app.js---

var app = angular.module('addressBookApp', []);
  app.controller('addressBookController', ['$scope', '$http', function($scope, $http) {
    $scope.doSomeAction = function() {
      $http({
          method: 'POST',
          url: 'addressbook/add',
          data: {'name' : $scope.name, 'email': $scope.email, 'phone': $scope.phone, 'dob': $scope.dob},
      }).success(function(response) {
          console.log(response);
      }).error(function(response) {
          alert(response);
          alert('This is embarassing. An error has occured. Please check the log for details');
      });
    }

Nor validation no database insert is happening, What am I doing wrong...



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1QR81yk
via IFTTT

Aucun commentaire:

Enregistrer un commentaire