Angular Schema Form - " required"不工作选择和复选框字段

时间:2018-05-12 04:49:03

标签: angularjs validation required angular-schema-form tv4

我是Angular Schema Form的新手,并且在selectcheckbox字段required验证方面遇到了一些问题。

$scope.schema下,我有名为designation的选择字段:

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

$scope.form(声明designationagreeTerms):

{
    "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 ..."
}

designationagreeTerms都在架构的required属性中定义。

当我提交表单时,两个字段都会通过required验证。我希望用户界面显示的是字段下方/之后的Required条消息。那种情况没有发生。

我尝试的事情:

  • 将选择字段的第一个值分配给''null,并将其与架构默认值相匹配。
  • 将架构中的select字段类型更改为object;这工作并通过了所需的验证,但财产没有出现在模型中

请帮助:)

1 个答案:

答案 0 :(得分:0)

您需要将架构类型更改为string,因为select不是有效的架构type属性。

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

然后在表单标签中,您应该具有 ng-submit =“ submitForm(ngform,modelData)”

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

然后在您的控制器内,您可以在提交时广播以进行验证:

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