简体   繁体   中英

How to implement form validations for Dynamically generated Form fields

I have a page in my Angular application where all form fields are created dynamically based on data coming from backend.

                <div class="col-md-12">
                    <form class="form-inline" name="reportsForm">
                        <div class="form-group form-group-grid" ng-repeat="fields in selectedTabData.requiredField" ng-switch="fields.paramType">
                            <label>{{fields.paramName}}<span class="asterisk" ng-if="fields.mandatory==true">*</span></label>
                            <input type="number" class="form-control" ng-switch-when="Number" ng-model="fields.fieldValue" ng-required="fields.mandatory">
                            <input type="date" data-date-format="mm/DD/YYYY" class="form-control" ng-switch-when="DatePicker" ng-model="fields.fieldValue" ng-required="fields.mandatory">
                            <input type="text" class="form-control" ng-switch-when="Text" ng-model="fields.fieldValue" ng-required="fields.mandatory">
                            <select type="date" class="form-control" ng-switch-when="DropDown" ng-options="field.paramKey as field.paramValue for field in fields.paramData" ng-model="fields.fieldValue" ng-required="fields.mandatory">
                            </select>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary form-inline-grid-button" ng-click="getReport()">Run Report</button>
                        </div>
                    </form>
                    <span  style="color:red">Please enter all the required fields marked with (*)</span>
                </div>

I need the validation error message to be shown if anyone of the required field in the form is left empty.

The form fields data coming from backend is assigned in $scope.selectedTabData.requiredField

$scope.selectedTabData.requiredField.forEach(function(item)
        {
            if(item.paramType == "DatePicker")
            {
                var date = new Date(item.fieldValue);
                var formattedDate = (date.getMonth() + 1) + '/' + date.getDate() + '/' +  date.getFullYear();
                paramValue.push(formattedDate);
                paramName.push(item.paramName);
            }
            else
            {
                if(item.mandatory == true && item.fieldValue == undefined){
                    //need to set validation as failed
                }else{
                    //need to set validation as passed
                }
                paramValue.push(item.fieldValue);
                paramName.push(item.paramName);
            }
        })

This is the condition I need to check to validate the form :

if(item.mandatory == true && item.fieldValue == undefined){
                    //need to set validation as failed
                }else{
                    //need to set validation as passed
                }

This is the first time I am working with dynamic fields, can anyone help me with implementation of validation in this case?

Thanks.

So to fix this, I had to deal with this the basic way.

I created a scope variable and set it as a flag.

in HTML :

<div>
<span ng-if="formInvalid" style="color:red">Please fill in all the required fields(*)</span>
</div>

In Controller :

$scope.formInvalid = false; 

if(item.mandatory == true && item.fieldValue==null)
            {
                $scope.formInvalid = true;
            }

This worked out for me. Please add more answers if you have an alternate solution to handle validations in dynamic elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM