Validation of forms in AngularJS/HTML5 that are nested within one another

Just starting out with AngularJS and experiencing issues with HTML5 nested form validation.

I currently have 2 forms; mainFrm (parent form) and stateFrm (a child form). I am struggling to validate each form within its own scope.

<form id="mainFrm" ng-submit="save()">
    
    <text-input form="mainFrm" required type="text">

    <form id="stateFrm" ng-submit="addState()">

        <input form="stateFrm" type="text">
        
        <!-- this won't add an item if input-text is empty-->
        <!-- prompts HTML5 validation-->
        <button form="stateFrm" type="submit">Add state to favorites</button>
        
        <!-- and a list of favorite states -->

    </form>
    
    <!-- (Will only validate the text-input of mainForm if empty) -->
    <button type="submit">Save</button>
</form>

The submit button of stateFrm is not working. The ng-submit="" of that form will not trigger and there is no validation prompt when the input is empty.

Check out the functional DEMO

NOTE: angular-material design was used in this example

Answer №1

Although HTML5 does not allow nested forms, you can achieve a similar layout by separating the forms. You can view a working example on Codepen at this link: http://codepen.io/anon/pen/YNrGrp

<form id="mainFrm" name="mainFrm" layout="column" ng-submit="vm.saveMain()">
  <md-input-container>
    <label for="name">Group name</label>
    <input type="text" required ng-model="group" />
  </md-input-container>
</form>

<form layout="column" name="stateFrm" layout-align="start" id="stateFrm" ng-submit="vm.addItem(state)">
  <md-input-container>
    <input required form="stateFrm" type="text" ng-model="state" aria-label="state item" placeholder="enter state"/>
  </md-input-container>
  <md-button form="stateFrm" class="md-raised" type="submit">Add state to favorite</md-button>

  <md-list>
    <md-subheader>Favorite States</md-subheader>
    <md-list-item ng-repeat="state in vm.states | orderBy">
      <span class="md-body-1">{{ state }}</span>
    </md-list-item>
  </md-list>
</form>


<md-button form="mainFrm" class="md-raised md-primary" type="submit">Save Main</md-button>

Answer №2

The W3C HTML5 Documentation states that according to the specifications, nested forms are considered invalid and not allowed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Align component within material-ui grid

Looking to center align the same cards but not just the grid component, I need the content itself to be equally distant from the borders and each other. I've searched and tried different solutions without luck. https://i.stack.imgur.com/aZj7H.png htt ...

Having trouble executing the typescript build task: Command 'C:Program' is not valid as an internal or external command

I'm currently working on converting typescript code to JavaScript and have been following the steps outlined in the documentation. To automate the compilation of .ts files, I set up a watch task triggered by pressing Ctrl+Shift+B. However, upon runni ...

What is the best way to clear a form in a Next.js 13.4 component following a server action?

Currently, I am working on a component using next.js 13.4, typescript, and resend functionality. My code is functioning properly without clearing data from inputs, as it uses the "action" attribute which is commented out. However, I started incorporating ...

Check off all checkboxes and send their values to an AJAX script using a table

I currently have a table set up with checkboxes in the first column. By checking these boxes, an AJAX script is triggered that updates a PHP session variable with the selected values. This functionality is working smoothly. However, I am now looking to enh ...

Show nested arrays in Vue.js exhibition

As a newcomer to vue, I've been navigating my way around with some success. Lately, I've been dealing with JSON data structured like this: [ { "name": "jack", "age": "twenty", "Colors&qu ...

ReactJS aligns buttons to the right

I have been attempting to align a button to the far right without success. Here is what I have tried: <Button variant="contained" style={{display: 'flex', justifyContent: 'right'}} color="primary" className="float-right" onClick={ ...

Retrieving JSON information using Angular from JSONPlaceholder

Obtaining the JSON data from http://jsonplaceholder.typicode.com/ using Angular seems quite simple, yet I am struggling to find the right method. Can anyone provide guidance on how to achieve this? I have already attempted multiple approaches with no suc ...

Error: Firebase encountered an issue (auth/invalid-api-key) while deploying on Netlify

I'm currently working on a Next.js application that utilizes Firebase Auth for client authentication and is hosted on Netlify. firebaseConfig.js import { initializeApp } from "firebase/app"; import { getAuth } from "firebase/auth" ...

Ensure that the folder name contains specific characters

When working with AngularJS, I am developing a feature to create folders. One requirement is that if a folder name contains special characters like /, :, ?, "<", or |, an error message should be displayed stating "A folder name cannot contain any of the ...

The TransferList component in Material UI does not update its state based on props when using the useState

My TransferList component in Material UI receives an array of previously selected items as props.selectedItems. The props.Items contains all available items. I expected to see props.selectedItems in the left panel of TransferList, but the left panel is em ...

Reactjs is experiencing issues with the data mapping function

Currently, I am developing with Reactjs and utilizing the nextjs framework. In my current project, I am working on fetching data from a specific URL (https://dummyjson.com/products) using the map function. However, I encountered an error message: TypeError ...

Issue with AngularJS Directive - ng-click not activating within the specified directive

I've encountered an issue with my new directive. The variables are not being updated when using ng-click. I've searched and found some solutions here, but they all involve calling a method. I simply need to change the values of 2 variables when t ...

Clickable list element with a button on top

Within my web application, there is a list displaying options for the user. Each 'li' element within this list is clickable, allowing the user to navigate to their selected option. Additionally, every 'li' element contains two buttons - ...

What is the best approach to repurpose a component when small adjustments are needed?

Can a customized slider component be created in React that can be reused with slight variations? For example, having the second one include a 13% field. View image description here ...

You must use the 'new' keyword in order to invoke the class constructor

Although similar questions have been asked before, my situation differs from the typical scenarios. I have a basic base class named CObject structured as follows: export class CObject extends BaseObject { constructor() { super(); } sta ...

Adjust the appearance of "FAQS" to show as "FAQs" in Lodash

I've integrated the lodash library - a powerful JavaScript utility tool - into my AngularJS project. <div ng-repeat="question in questions"> <label>{{question | startCase}}</label> </div> If you want to learn more about th ...

Several attributes in the JSON object being sent to the MVC controller are missing or have a null

I am facing an issue while passing a JSON object to my MVC controller action via POST. Although the controller action is being called, some elements of the object are showing up as NULL. Specifically, the 'ArticleKey' element is present but the & ...

Deciphering occurrences in highcharts

When drawing a rectangle in highcharts using the selection event, I am able to retrieve the coordinates of the box by translating the axis values of the chart like this: chart.xAxis[0].translate((event.xAxis[0]||chart.xAxis[0]).min) Now, my query is how ...

Is it possible to send data to a local .json file using the $http service in AngularJS?

Has anyone been successful in using the $http service to send data to a local json file within their app directory? I've experimented with $http.post and $http.get, but so far I have not been able to figure out how to update or add new data to my loc ...

Sort an array of strings alphabetically, including numbers (as strings), in order

After using the orderBy: toString() method, I arranged the values of the array below: Array: ["Susan","1","121","Adam","11"] However, the result turned out to be: Adam Susan 11 121 1 I was looking for this order instead: 1 11 121 Adam Susan Here is t ...