简体   繁体   English

Angular Schema Form-“必需”不起作用的选择和复选框字段

[英]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. 我是Angular Schema Form的新手, selectcheckbox字段存在一些问题, required验证。

Under $scope.schema I have the select field named designation : $scope.schema我有选择字段命名designation

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

$scope.form (declaring designation and agreeTerms ): $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 ..."
}

Both designation and agreeTerms are defined in the schema's required property. 在模式的required属性中定义了designationagreeTerms

When I submit the form, both fields however pass the required validation. 当我提交表单时,两个字段均通过了required验证。 What I'm expecting the UI to show are the Required messages underneath/after the fields. 我希望用户界面显示的是字段下方/之后的Required消息。 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. 将选择字段的第一个值分配为''并为null并将其与架构默认值匹配。
  • change the select field type to object in the schema; select字段类型更改为架构中的object 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. 您需要将架构类型更改为string因为select不是有效的架构type属性。

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

Then in your form tag you should have ng-submit="submitForm(ngform,modelData)" : 然后,在表单标签中,您应该具有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');
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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