sergioramosiker
sergioramosiker

Reputation: 375

AngularJS Full Stack form Submit Issue

I have included on extra input field to the default login.html page available in yeoman+express+angularjs full stack application

 <form class="form" name="form" ng-submit="login(form)" novalidate>
`<input type="text" name="pname" class="form-control" ng-model="user.pname" >` 
 <input type="text" name="email" class="form-control" ng-model="user.email">
<input type="password" name="password" class="form-control" ng-model="user.password">
<p class="help-block" ng-show="form.pname.$error.required && form.email.$error.required &&              form.password.$error.required  && submitted"><button type="submit" lass="btn btn-info">Sign in</button></div>          

but pname is not getting validated for blank and im not able to get the value of pname in my controller also

angular.module('ratefastApp')
  .controller('LoginCtrl', function ($scope, Auth, $location) {
    $scope.user = {};
    $scope.errors = {};

    $scope.login = function(form) {
      $scope.submitted = true;

      if(form.$valid) {
        Auth.login({
          email: $scope.user.email,
          password: $scope.user.password,
          practicename : $scope.user.pname
        })
        .then( function(err) {
          $location.path('/');
        })
        .catch( function(err) {          
          err = err.data;
          $scope.errors.other = err.message;
        });
      }
    };
  });

pname is going undefined.. in express controller , my express controller code is as follow

exports.login = function (req, res, next) { var

practiceName = String(req.body.pname); var userName = String(req.body.email); var userPass = String(req.body.password); };

------------- route.js app.post('/api/session', session.login); ------- html is same as above

in console im getting practiceName undefined

Upvotes: 0

Views: 307

Answers (1)

Khanh TO
Khanh TO

Reputation: 48982

Try putting required on the fields you need:

<input type="text" name="pname" class="form-control" ng-model="user.pname" required>

DEMO

The reason you get undefined for your pname in your express controller is because you're passing practicename, not pname:

Auth.login({
      email: $scope.user.email,
      password: $scope.user.password,
      practicename : $scope.user.pname //you use practicename here.
 })

Try replacing:

practiceName = String(req.body.pname); 

With:

practiceName = String(req.body.practicename); 

Upvotes: 0

Related Questions