angularjsvalidationrequiredangular-schema-formtv4

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:

Please help :)


Solution

  • 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');
    };