简体   繁体   English

AngularJS正式表格-如何使单选按钮成为必需

[英]AngularJS Formly Forms - How to Make Radio Buttons Required

I've been looking through the formly forms documentation, as well as through SO, but I cannot determine if the below is possible. 我一直在浏览正式的表单文档以及SO,但是我无法确定以下情况是否可能。 Has anyone encountered a solution? 有没有人遇到解决方案?

Issue: How does one configure Formly Form radio button questions to be required? 问题:如何配置所需的Formly Form单选按钮问题?

Background: I have an API that is returning formly form questions to the front end. 背景:我有一个API,可以将形式形式的问题返回到前端。 For example, I might get the below back: 例如,我可能会得到以下答复: 在此处输入图片说明

Both of those fields are 'required' in the DB. 这两个字段在数据库中都是“必需”的。 If I don't key in an answer to 'What's your blog URL?', I get a little 'Field is required!' 如果我没有输入“您的博客URL是什么?”的答案,我会得到一点“必填字段!”。 message. 信息。 Awesome. 真棒。

But if I don't fill out that radio button, I get no such message. 但是,如果我不填写该单选按钮,则不会收到这样的消息。 The form just hangs when I click 'Confirm'. 当我单击“确认”时,表格只是挂起。

Any idea how get the 'This form is required!' 知道如何获得“需要此表格!” message with the radio question? 广播问题消息? Again, these questions are not hard-coded, so it's not as simple as adding an option to the HTML. 同样,这些问题不是硬编码的,因此它不像在HTML中添加选项那样简单。


Yes, I have tried using required: true. 是的,我尝试使用required:是。 Works great with select, but does not seem to work with radio buttons.Trying this: 可以与select一起很好地工作,但似乎不能与单选按钮一起工作。

formlyConfigProvider.setType([{
name: 'radio',
templateUrl: "radioTemplate.html",
wrapper: ['simpleLabel', 'errorMessage'],
defaultOptions: {
  noFormControl: false
},
apiCheck: function apiCheck(check) {
  return {
    templateOptions: {
      required: true,
      options: check.arrayOf(check.object),
      labelProp: check.string.optional,
      valueProp: check.string.optional
    }
  };
},
controller: ['$scope', 'HelpersService', function($scope, HelpersService) {
  if (['Internet Explorer', 'MSIE', 'Unknown', 'Edge'].indexOf(HelpersService.getBrowserName().name) > -1) {
    $scope.ieClass = 'ie';
  }
}]

}]); }]);

I get the following error: 我收到以下错误:

在此处输入图片说明

This led me to believe this is not an option with radio buttons. 这使我相信这不是单选按钮的选项。 However, if I'm interpreting this error incorrectly, please let me know! 但是,如果我错误地解释了此错误,请告诉我! :) :)

Someone requested a code sample. 有人要求提供代码示例。 A lot of code in the codebase is proprietary; 代码库中的许多代码都是专有的。 however, I have included below the template I use for the questions that come back from the DB. 但是,我在模板下面包含了我用来回答数据库返回问题的模板。 What I'm really curious about is if anyone has encountered this issue before, and if there's a documented config option to get around it, or a very straightforward hack. 我真正好奇的是,是否有人以前曾遇到过此问题,是否有记录在案的config选项可以解决此问题,或者是非常简单的hack。 Otherwise, I'll look into detecting this with a function in the controller. 否则,我将研究使用控制器中的功能检测到这一点。

****Not all questions that come back from the DB will be required!!! ****并非所有来自数据库的问题都是必需的!!!

 <div ng-if="ctrl.questions"> <form name="ctrl.form" ng-submit="ctrl.submit(ctrl.questions.model)" novalidate> <formly-form form="ctrl.form" class="input" fields="ctrl.questions" model="ctrl.questions.model" > <input type="submit" class="btn" id="submit" value="Confirm" /> </formly-form> </form> </div> 

Add the required in the radio button: 在单选按钮中添加required

<input type="radio" name="myRadio" required ng-model="myModel" value="myValue"/>

Then you can use the ng-disabled to check if the radio is selected: 然后,您可以使用ng-disabled检查是否选择了无线电:

<input type="button" ng-click="action()" value="Next" ng-disabled="myform.myRadio.$invalid" />

use ng-required to validate the checkbox like this 使用ng-required来验证此复选框

<div ng-app="app" ng-controller="ctrl">
     <form name="myForm" ng-submit="submit()"  >
       <input type="radio" ng-model="roommate" value="roommate" ng-required="!roommate">  roommate <br/>
        <button type="submit">Submit</button>
      </form>
</div>

Demo 演示

easily...you add required: true in the templateOptions. 轻松地...您在templateOptions中添加required:true。 There is also an option for disabled as well. 还有一个禁用选项。 Did you even bother looking at any of the examples on the site? 您是否还烦恼查看网站上的任何示例? This would have been easily found by doing so 这样做很容易发现

{
  key: 'radiobutton1',
  templateOptions: {
      required: true,
      label: 'Please make a selection',
      options: [{value: 'Steak', id: 'steak'}, {value: 'Lobster', id: 'lobster'}]
  }
}

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

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