Pacuraru Daniel
Pacuraru Daniel

Reputation: 1205

AngularJS get form empty data too

i am trying to save a form data with angularjs into a database using php as backend. I have this html as the form

<form ng-submit="newContactSubmit()">
        <label>FirstName<input type="text" name="contact_firstname" required ng-model="newContact.contact_firstname" /></label>
        <label>LastName<input type="text" name="contact_lastname" ng-model="newContact.contact_lastname" /></label>
        <button type="submit">Save</button>
    </form>

Now the problem is lastname is not required in my case and if i send the form without filling anything in lastname, than the newContact variable has only the contact_firstname field which makes it do an error on the php side.

$scope.newContactSubmit = function() {
    alert(JSON.stringify($scope.newContact));
    /*contactService.saveContact($scope.newContact).then(function(response) {
        alert(JSON.stringify(response));
    });*/
};

The controller doesnt send the contact_lastname field as empty. Is there a way to make it define both variables even if i dont complete them ? Thank you, Daniel!

Upvotes: 0

Views: 156

Answers (1)

Kita
Kita

Reputation: 2634

How about predefining default value for contact_lastname?

// inside your controller
$scope.newContact.contact_lastname = '';

so that newContact always has contact_lastname default value, even the field is left untouched.

Upvotes: 3

Related Questions