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

Saving MongoDB query results to a file using the built-in Node.js driver

I have been attempting to save the output of a MongoDB query to a file using the native Node.js driver. Below is my code (which I found on this post: Writing files in Node.js): var query = require('./queries.js'); var fs = require('fs' ...

Preventing jQuery slideToggle functionality from toggling multiple elements at once

I am currently working on a project where I have a series of images with captions that appear underneath each one. To show or hide the caption for each image, I am using slideToggle when the image is clicked. $('.imageholder').click(function() ...

Sending data from jQuery modal to the final input field

In my latest project, I have developed a modal window that features a table with rows of input boxes and buttons: <table class="datatable tablesort selectable paginate full" width="100%"> <tbody> ...

What is the best way to retrieve the page slug within the layout file in Next.js, specifically when using the app folder structure?

In my latest project, I have implemented a nested layout using the new app folder. Within this layout, I have included a header that appears across all pages in the path www.mysite.com/some-slug. One issue I am facing is with the signup button located in t ...

A guide to resolving cross-origin resource sharing issues using a reverse proxy

After creating a JavaScript web application for processing documents, I am now looking to integrate with web services like NLTK-server, TIKA-server, and SOLR for further analysis. While I can successfully access the REST endpoints of these services using c ...

Listening on TCP port for HTML5 Websocket communications

I have a desktop application that is communicating with my asp.net mvc app. The desktop application publishes data on port:10000 which I need to be able to listen to in the browser. Below is the code snippet: <html> <head> <s ...

Ensuring Stringency in JQuery's '$' Selector

I have a specific data attribute within the div element that is displayed in the HTML. <div my-custom-attrib="1".../> <div my-custom-sttrib="2"...> Now, in JQuery, I am attempting to filter between the div elements based on the value of the c ...

Double dragenter events triggered before dragleave in HTML5 drag and drop functionality

Currently, I'm researching HTML5 Drag and Drop functionality by using this resource. However, I've encountered an issue with the dragenter event that seems to fire twice for child elements before the dragleave event. Because of this, the dashed b ...

Encountering an "undefined is not a function" error across all libraries

While working on ASP.Net MVC4, I have encountered an issue where I consistently receive the error message "undefined is not a function" when using jQuery functions with different libraries. Despite ensuring that every ID is correct and everything has bee ...

A guide to accessing items imported from a glb file within the Babylon JS game engine

When I use the BABYLON.SceneLoader.ImportMesh() method to load a .glb file created in Blender, one of the objects is named Cube.003. Surprisingly, after calling the method, Cube.003 is not included in the scene.meshes array. It does show up in the scene ...

How can nested json be sorted effectively based on two specific fields?

Example Data: [{ 'ID': objID(abc123), 'Department': 'IT', 'Employees': [ { 'ID': 3, 'StartDate': '24-12-2022T08:30', 'active': true }, { ...

Transferring data to ng-model within ng-repeat loop

I am facing an issue with a form that is supposed to pass its inputs to ng-model before saving them into the database. One of the inputs is a dynamic value, specifically a pre-generated code retrieved from a database table. <div class="form-group" ng-r ...

Combining Strings with HTML in Angular

There is a "template string" in my code that goes like this: var templateString = "Hey there, I am {{name}}"; The name I want to insert is stored in a variable. Here's how I proceeded: var miniScope = { name: "Alice" }; var phrase = $interpolate( ...

I am encountering difficulties with importing Node modules into my Vue.js project

Having some trouble importing a node module via NPM in a Vue.js single file component. No matter which module I try to install, it always throws an error saying These dependencies were not found. I'm following the installation instructions correctly ( ...

Update the CSS for InputLabel

I have a drop-down list that I want to customize. The issue is illustrated below: I'm looking to center the text "choose format" within the field and adjust the font size. return ( <FormControl sx={{ m: 1, minWidth: 150 }} size="sm ...

Is there a way to make the submit button navigate to the next tab, updating both the URL and the tab's content as well?

I am encountering an issue with my tabs for Step1 and Step2. After pressing the submit button in Step1, the URL updates but the component remains on tab1. How can I resolve this so that the user is directed to the Step2 tab once they click the submit butto ...

What is the reason for node executing both of these handlers on the server?

My node app is set up with two handlers, one for requests to the root URL and another for the slug. However, when I visit the root or slug URLs, the app crashes and I receive this error: url is pointing to root url has a slug throw err; // Rethrow non-MySQ ...

Error: Client was unable to process JSON data

My server setup looks like this: var http = require('http'); //Defining the port to listen to const PORT=8092; //Function that handles requests and sends responses function handleRequest(request, response){ response.statusCode = 200; ...

Pressing a specific button triggers a broadcast signal, prompting a modal window to pop up and display relevant information

Within my website's HTML page named rollup.html, there exists a button that triggers the opening of a modal. <button id="myBtn" ng-click="printDivModal('rollup-tab')">ModalTest</button> In the accompanying JavaScript file, roll ...

What is the best way to outline the specifications for a component?

I am currently working on a TypeScript component. component @customElement("my-component") export class MyComponent extends LitElement { @property({type: String}) myProperty = "" render() { return html`<p>my-component& ...