0

AddClass.cshtml File has the form and ng-validate uses the validation option. When I click the Add Class button in cshtml file without entering any value in Textboxes, it should show error messages but i get form.validation() is not a function error. All the js files are already present such as angular-validate.min and jquery.validate.min. Any idea what might be the problem??

<form name="addclassform" id="addclassform" ng-submit="addClassForm(addclassform)" ng-validate="classValidationOptions">
    <div class="modal-body">
        <div class="form-group">
            <label for="ClassName" class="col-md-3">Class Name</label>
            <input name="ClassName" id="ClassName" class="form-control" ng-model="addClassFormData.ClassName" />
        </div>
        <div class="form-group">
            <label for="Location" class="col-md-3">Location</label>
            <input name="Location" id="Location" class="form-control" ng-model="addClassFormData.Location" />
        </div>
        <div class="form-group">
            <label for="TeacherName" class="col-md-3">Teacher Name</label>
            <input name="TeacherName" id="TeacherName" class="form-control" ng-model="addClassFormData.TeacherName" />
        </div>
    </div>
    <div class="modal-footer">
        <button type="submit" class="btn btn-success">Add Class</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
    </div>
</form>

This is the AngularJS file. The classValidationOptions has all the rules and form.validation() throws an Error in $scope.addClassForm

//Validate Class
$scope.classValidationOptions = {
    rules: {
        ClassName: { required: true },
        Location: { required: true },
        TeacherName: { required: true }
    },
    messages: {
        ClassName: { required: "Please enter Class Name." },
        Location: { required: "Please enter the Location." },
        TeacherName: { required: "The field is required." }
    }
};

$scope.addClassFormData = {};

//Adds a Class
$scope.addClassForm = function (form) {
    if (form.validate()) {
        $http({
            method: "POST",
            url: "/Home/AddClass",
            data: $.param($scope.addClassFormData),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).then(function Success(response) {
            var data = angular.fromJson(response.data);
            if (data.success) {
                $scope.loadClasses();
                $('#addClass').modal('hide');
                $scope.resetClassForm("add");
                //toastr.success(data.msg, 'Success');
            }
            else {
                //toastr.error(data.msg, 'Error');
            }
        });
    }
};

enter image description here

1 Answer 1

0

I found the Error. The problem was I was not writing ngValidate in the module

Incorrect Code

var app = angular.module('myApp', []);

Correct Code

var app = angular.module('myApp', ['ngValidate']);
Sign up to request clarification or add additional context in comments.

Comments

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.