mercredi 3 juin 2020

Custom Validation Function not invoking

I have made and AngularJs app and made Form in HTML/CSS. The form takes 2 Dates. StartDate and endDate. For validations, I used the AngularJs Ng-messages. I created a custom validation function to check that endDate should not be before startDate. But the function is not invoking. Things i have checked.

  1. Include Ng-messages file in start.
  2. Add the function to Scope.
  3. add the customer function name in input Tag of HTML.
  4. Checked the spelling issue.

But nothing has worked for me. So, here I am to show it to you guys.

    Layout = null;
}

<form name="newTenatForm" class="flex-fill" ng-submit="newTenatForm.$valid && onAddEditTenantSubmitForm(newTenatForm)" novalidate>
    <div class="row">
        <div class="col-lg-6 offset-lg-3">
            <div class="card mb-0">
                <div class="card-body">
                    <div class="text-center mb-3">
                        <i class="icon-stack icon-2x text-success border-success border-3 rounded-round p-3 mb-3 mt-1"></i>
                        <h5 class="mb-0">@ViewBag.title</h5>
                        <span class="d-block text-muted">Fields with * are required</span>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group form-group-feedback form-group-feedback-right">
                                <input type="text" class="form-control" placeholder="Name *" name="tenantName" ng-minlength="5" ng-model="state.tenantModel.name" required>                                       
                                <ng-messages class="form-text text-danger-400"
                                             ng-if="newTenatForm.$submitted || newTenatForm.tenantName.$touched"
                                             for="newTenatForm.tenantName.$error">
                                    <div ng-message="minlength">Tenant name is too short.</div>
                                    <div ng-message="required">Tenant name is required</div>
                                </ng-messages>
                                <div class="form-control-feedback">
                                    <i class="icon-user-check text-muted"></i>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group form-group-feedback form-group-feedback-right">
                                <input type="text" class="form-control" placeholder="Identifier *" name="tenantIdentifier" ng-minlength="5" ng-model="state.tenantModel.identifier" required>
                                <ng-messages class="form-text text-danger-400"
                                             ng-if="newTenatForm.$submitted || newTenatForm.tenantIdentifier.$touched"
                                             for="newTenatForm.tenantIdentifier.$error">
                                    <div ng-message="minlength">Tenant Identifier is too short.</div>
                                    <div ng-message="required">Tenant identifier is required</div>
                                </ng-messages>
                                <div class="form-control-feedback">
                                    <i class="icon-user-check text-muted"></i>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-group form-group-feedback form-group-feedback-right">
                        <input type="text" class="form-control" placeholder="Connection String" ng-model="state.tenantModel.connectionString">
                        <div class="form-control-feedback">
                            <i class="icon-user-plus text-muted"></i>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group form-group-feedback form-group-feedback-right">
                                <input type="date" class="form-control" placeholder="Start Date" id="anytime-date" name="tenantStartDate" ng-model="state.tenantModel.startFrom" required>
                                <ng-messages class="form-text text-danger-400"
                                             ng-if="newTenatForm.$submitted || newTenatForm.tenantStartDate.$touched"
                                             for="newTenatForm.tenantStartDate.$error">
                                    <div ng-message="required">Tenant start date is required</div>                                    
                                </ng-messages>
                                <div class="form-control-feedback">
                                    <i class="icon-user-lock text-muted"></i>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group form-group-feedback form-group-feedback-right">
                                <input type="date" class="form-control" placeholder="End Date" id="tenantEndDate" name="tenantEndDate" ng-model="state.tenantModel.endOn" required compareWithStartDate>
                                <ng-messages class="form-text text-danger-400"
                                             ng-if="newTenatForm.$submitted || newTenatForm.tenantEndDate.$touched"
                                             for="newTenatForm.tenantEndDate.$error">
                                    <div ng-message="required">Tenant end date is required</div>                                    
                                    <div ng-message="checkEndDate">End date cannot be before start date.</div>                                    
                                </ng-messages>
                                <div class="form-control-feedback">
                                    <i class="icon-user-lock text-muted"></i>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-group form-group-feedback form-group-feedback-right">
                        <div class="form-group row">
                            <label class="col-form-label col-lg-2">Tenant Type *</label>
                            <div class="col-lg-10">
                                <select class="form-control" id="tenantType" name="tenantType" ng-model="state.tenantModel.tenantType" required>
                                    <option value="1">Trial</option>
                                    <option value="2">Standard</option>
                                </select>
                                <ng-messages class="form-text text-danger-400"
                                             ng-if="newTenatForm.$submitted || newTenatForm.tenantType.$touched"
                                             for="newTenatForm.tenantType.$error">
                                    <div ng-message="required">Pleas select tenant type.</div>
                                </ng-messages>
                            </div>
                        </div>
                    </div>

                    <button type="submit" value="Submit" class="btn bg-teal-400 btn-labeled btn-labeled-right"><b><i class="icon-plus3"></i></b> @if (ViewBag.title == "Update Tenant")
                    {<span>Update Acccount</span> }
                    else
                    { <span>Create account </span>} </button>
                </div>
            </div>
        </div>
    </div>
</form>```

and this is Js code. 
```(function () {
    var app = angular.module('app');
    app.controller('MultiTenantController', ['$scope', '$window', '$filter', 'TenantServices', function ($scope, $window, $filter, TenantServices) {

        var objectA = {}

        var state = {
            tenantModel: {
                addedOn: '',
                connectionString: '',
                createdBy: '',
                endOn: '',
                id: '',
                identifier: '',
                isDisabled: '',
                items: [{
                    isDisabled: '',
                    tenantType: '',
                    endOn: '',
                    startFrom: '',
                }],
                name: '',
                startFrom: '',
                tenantType: '',
                userId: '',
            },            
        };


        var init = function (id) {

            if (id) { tenantEdit(id); };

            $(document).ready(function () {
                tenantTable = $('#table_Tenants').DataTable({
                    ajax: {
                        url: 'API/MultiTenant/Pagged',
                        method: 'POST'
                    },
                    columns: [
                        { data: 'name' },
                        { data: 'identifier' },
                        { data: 'connectionString' },
                        {
                            data: 'startFrom',
                            render: function (startFrom) {
                                return $filter('date')(startFrom, 'medium');
                            }
                        },
                        {
                            data: 'addedOn',
                            render: function (addedOn) {
                                return $filter('date')(addedOn, 'medium');
                            }
                        },
                        {
                            data: 'endOn',
                            render: function (endOn) {
                                return $filter('date')(endOn, 'medium');
                            }
                        },
                        {
                            data: 'isDisabled',
                            render: function (isDisabled) {
                                return !isDisabled ?'<span class="badge badge-success">Active</span>':'<span class="badge badge-danger">Inactive</span>';
                            }
                        },
                        {
                            data: function (data) {
                                return data;
                            },
                            orderable: false,
                            render: function (data) {
                                return `<div class="list-icons">
                                    <div class="dropdown">
                                        <a href="#" class="list-icons-item" data-toggle="dropdown">
                                            <i class="icon-menu9"></i>
                                        </a>
                                        <div class="dropdown-menu dropdown-menu-right">
                                            <a href="javascript:void(0)" class="dropdown-item createTenantDatabase"><i class="icon-database-upload"></i>Create Database</a>
                                            <a href="/MultiTenant/Edit/${data.id}" class="dropdown-item editTenant"><i class="icon-database-edit2"></i>Edit</a>
                                            <a href="javascript:void(0)" class="dropdown-item deleteTenant"><i class="icon-database-remove"></i>Delete</a>                                                                                                                                                                                                                                                                                                                   
                                        </div>
                                    </div>
                                </div>`;
                            }
                        },
                    ]
                });

                tenantTable.on('click', '.createTenantDatabase', function () {
                    const $row = $(this).closest('tr');
                    const rowData = tenantTable.row($row).data();
                    addTenantDatabase(rowData);
                });                

                tenantTable.on('click', '.deleteTenant', function () {
                    const $row = $(this).closest('tr');
                    const rowData = tenantTable.row($row).data();
                    deleteTenant(rowData);
                });
            })
        };

        const onAddEditTenantSubmitForm = function (newTenatForm) {
            if (!state.tenantModel.userId) {
                addTenant(newTenatForm);
            }
            else {
                updateTenant(newTenatForm);
            }
        };

        var addTenant = function (newTenatForm) {
            state.isLoading = true;
            TenantServices.addTenant(state.tenantModel).then(
                function () {
                    swalInit.fire({
                        title: 'Success',
                        text: 'Tenant has been created',
                        type: 'success'
                    });                                        
                },
                function (errorResponse) {
                    if (errorResponse.status === 400) {
                        swalInit.fire({
                            title: 'Server Error',
                            text: 'An server error occurred while adding tenant.',
                            type: 'error 400'
                        });
                    }
                    else if (errorResponse.status === 500) {
                        swalInit.fire({
                            title: 'Server Error',
                            text: 'An server error occurred while adding tenant.',
                            type: 'error 500'
                        });
                    }
                }
            )
                .finally(() => state.isLoading = false);
        }; 

        var addTenantDatabase = function (rowObj) {
            state.isLoading = true;
            TenantServices.createTenantDatabase(rowObj.i).then(
                function () {
                    swalInit.fire({
                        title: 'Success',
                        text: 'Tenant Database has been created',
                        type: 'success'
                    });
                    table_Tenants.ajax.reload();                    
                },
                function (errorResponse) {
                    if (errorResponse.status === 400) {
                        swalInit.fire({
                            title: 'Server Error',
                            text: 'An server error occurred while creating database tenant.',
                            type: 'error 400'
                        });
                    }
                    else if (errorResponse.status === 500) {
                        swalInit.fire({
                            title: 'Server Error',
                            text: 'An server error occurred while creating database tenant.',
                            type: 'error 500'
                        });
                    }
                }
            )
                .finally(() => state.isLoading = false);
        };

        var updateTenant = function (rowObj) {
            state.isLoading = true;
            TenantServices.updateTenant(state.tenantModel).then(
                function () {
                    swalInit.fire({
                        title: 'Success',
                        text: 'Tenant Updated!',
                        type: 'success'
                    });
                    tenantTable.ajax.reload();
                },
                function (errorResponse) {
                    if (errorResponse.status === 500) {
                        swalInit.fire({
                            title: 'Server Error',
                            text: 'An server error occurred while Updating Tenant.',
                            type: 'error'
                        });
                    }
                    else if (errorResponse.status === 404) {
                        swalInit.fire({
                            title: 'Server Error',
                            text: 'Tenant not found on Server.',
                            type: 'error'
                        });
                    }
                }
            )
                .finally(() => state.isLoading = false);
        };  

        var deleteTenant = function (rowObj) {
            state.isLoading = true;
            TenantServices.deleteTenant(rowObj.id).then(
                function () {
                    swalInit.fire({
                        title: 'Success',
                        text: 'Tenant Deleted!',
                        type: 'success'
                    });
                    tenantTable.ajax.reload();                    
                },
                function (errorResponse) {
                    if (errorResponse.status === 500) {
                        swalInit.fire({
                            title: 'Server Error',
                            text: 'An server error occurred while Deleting Tenant.',
                            type: 'error'
                        });
                    }
                    else if (errorResponse.status === 404) {
                        swalInit.fire({
                            title: 'Server Error',
                            text: 'Tenant not found on Server.',
                            type: 'error'
                        });
                    }
                }
            )
                .finally(() => state.isLoading = false);
        };  

        var tenantEdit = function (id) {            
            TenantServices.getTenantById(id).then(function (data) {
                var startDate = new Date(data.found.startFrom + "Z");
                var endDate = new Date(data.found.endOn + "Z");
                state.tenantModel = data.found;
                state.tenantModel.startFrom = startDate;
                state.tenantModel.endOn = endDate;

                if(state.tenantModel.tenantType == 1)
                {
                    $(function () {                        
                        $('#tenantType').val('1');
                    });   
                } else if (state.tenantModel.tenantType == 2)
                {
                    $(function () {                         
                        $('#tenantType').val('2');
                    });
                }
            });

        };

        var compareWithStartDate = function () {
            if (state.tenantModel.startFrom == state.tenantModel.endOn) {
                newTenatForm.tenantEndDate.$setValidity('checkEndDate', true);
            }
            else {
                newTenatForm.tenantEndDate.$setValidity('checkEndDate', false);
            }
        };

        $scope.onAddEditTenantSubmitForm = onAddEditTenantSubmitForm;
        $scope.tenantEdit = tenantEdit;
        $scope.compareWithStartDate = compareWithStartDate;

        $scope.state = state;
        $scope.init = init;                       

    }]);
})();```





Aucun commentaire:

Enregistrer un commentaire