简体   繁体   中英

Angular Schema Form - “required” not working select and checkbox fields

I'm new to Angular Schema Form and having some issues with select and checkbox fields required validation.

Under $scope.schema I have the select field named designation :

"designation": {
    "title": "Designation",
    "type": "select",
    "default": undefined
}

$scope.form (declaring designation and agreeTerms ):

{
    "key": "designation",
    "type": "select",
    "title": "Designation",
    "titleMap": [
        { value: undefined, name: "Select a designation" }, // first value
        { value: "Andersson", name: "Andersson" },
        { value: "Johansson", name: "Johansson" },
        { value: "other", name: "Something else..."}
    ]
},
{
    "key": "agreeTerms",
    "type": "checkbox",
    "title": "I agree to ..."
}

Both designation and agreeTerms are defined in the schema's required property.

When I submit the form, both fields however pass the required validation. What I'm expecting the UI to show are the Required messages underneath/after the fields. That is not happening.

Things I've tried:

  • assign first value of the select field to '' and null and match that with the schema default value.
  • change the select field type to object in the schema; this worked and passed the required validation but the property didn't show up in the model

Please help :)

You need to change the schema type to string as select is not a valid schema type property.

"designation": {
    "title": "Designation",
    "type": "string",
    "default": undefined
}

Then in your form tag you should have ng-submit="submitForm(ngform,modelData)" :

<form sf-schema="schema" sf-form="form" sf-model="model" ng-submit="submitForm(ngform,modelData)" sf-options="option"></form>

Then within your controller you can broadcast on submit to validate:

$scope.submitForm = function(form) {
    // First we broadcast an event so all fields validate themselves
    $scope.$broadcast('schemaFormValidate');
};

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