Zeev Katz Zeev Katz - 1 month ago 17
AngularJS Question

Required fieldset html [Angular]

There is a way to required form fieldset and don't every input?

My point: the user can choose what to fill but will not have to fill all of the fieldset inputs

I have this two:

1:

<fieldset>
<legend> legend </legend>
<label for="id"> label </label>
<input type="text" id="id" name="name">
<label for="id2"> label </label>
<input type="text" id="id2" name="name2">
</fieldset>


2:

<fieldset>
<legend> legend </legend>
<label for="id"> label </label>
<input type="radio" id="id" name="name">
<label for="id2"> label </label>
<input type="radio" id="id2" name="name">
</fieldset>


I use Angular if it can be simplest.

Answer

If you only have 2 options, the easiest thing to do would be:

<fieldset>
  <legend> legend </legend>
  <label for="id"> label </label>
  <input type="text" id="id" name="name" ng-required="!name2">
  <label for="id2"> label </label>
  <input type="text" id="id2" name="name2" ng-required="!name">
</fieldset>

Edit: Since they are dynamic, you would probably need a model to help you out on each fieldset.

<fieldset>
  <legend> legend </legend>
  <label for="id"> label </label>
  <input type="text" id="id" name="name" ng-model="fieldSetHasValue" ng-change="fieldSetHasValue = true">
  <label for="id2"> label </label>
  <input type="text" id="id2" name="name2" ng-model="fieldSetHasValue" ng-change="fieldSetHasValue = true">
</fieldset>

And then before you submit, check if fieldSetHasValue is true. This is just one of many ways to accomplish this and there will no doubt be some kinks to work through if you implement this. This is just a quick off the top of my head solution without seeing the rest of your code.

Comments