1

I have a form

<form name="formName">
 <table>
    <tr>
       <td><input type="text" ng-model="validate" required/></td>
    </tr>
 </table>
</form>
<button type="button" class="btn btn-default" ng-click="validate()">save</button>

as you can see, the button is outside the form.. I want to validate the form when I click the button into $scope.validate();

the question is, how can I achieve that using angularjs ?

5
  • are you getting any errors ? Commented Jun 23, 2014 at 10:22
  • 1
    Take a look at the awesome ngMessages directive introduced in 1.3 for form validations: yearofmoo.com/2014/05/how-to-use-ngmessages-in-angularjs.html Commented Jun 23, 2014 at 10:26
  • nope but angularjs is ignoring my validation actually... could you help me? @thomasbabuj Commented Jun 23, 2014 at 10:26
  • First I would rather delegate validations into directives (the controller should be used only to expose data to bind and call the services to retrieve data), if you want more info about this I can send you some links an sample (you play with the property form isValid in order to enable/disable a submit button). About the validate not being called, could you please post a plunkr or jsfiddle so we can pinpoint where is the issue? Commented Jun 23, 2014 at 10:39
  • I'm sorry but I think I don't have them.. all I have is the mock up above.. what I want is just validation when I clicked the button and send it to the validate() function @Braulio Commented Jun 23, 2014 at 10:47

1 Answer 1

1

It does not matter if button is in or outside the form, the validation is performed always when any of the proprties of the model changes. So the only problem that remains is when to display error message.

btw required is from HTML5 ng-required is the attribute you should use.

The code below displays error message when any of the properties changes

<div ng-app="mod">
<div ng-controller='MCtrl'>
    <form name="formName" novalidate>
        <table>
            <tr>
                <td>
                    <input type="text" name="prop" ng-model="model.property" ng-required="true" />
                    <span ng-show="formName.prop.$error.required">Required</span>
                </td>
            </tr>
        </table>
    </form>
    <button type="button" class="btn btn-default" ng-click="validate(formName)">save</button>
</div>

mod.controller('MCtrl', function ($scope) {
    $scope.model = { property : ''};

    $scope.validate = function(form){
        alert(form.$valid);
    }

});

If youd like to display the errors only when the form is submitted you add one variable to track it like it the code below (note additional variable in ng-show="formName.prop.$error.required && submitted"

<div ng-app="mod">
<div ng-controller='MCtrl'>
    <form name="formName" novalidate>
        <table>
            <tr>
                <td>
                    <input type="text" name="prop" ng-model="model.property" ng-required="true" />
                    <span ng-show="formName.prop.$error.required && submitted">Required</span>
                </td>
            </tr>
        </table>
    </form>
    <button type="button" class="btn btn-default" ng-click="validate(formName)">save</button>
</div>

mod.controller('MCtrl', function ($scope) {
    $scope.model = { property : ''};
    $scope.submitted = false;

    $scope.validate = function(form){
        $scope.submitted = true;
        alert(form.$valid);
    }

});
Sign up to request clarification or add additional context in comments.

1 Comment

what you explained to me is very clear.. thank you so much.. it works like magic ! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.