Angular form not processing unless all mandatory fields are completed

I am currently working on building a form using Angular and Ionic. I want to ensure that the red error class is only displayed after the user has submitted the form. To achieve this, my code is structured as follows:

<form ng-submit="submit()" name="form">
     <ion-radio ng-repeat="item in items" ng-model="data.type" name="type" ng-value="item.value" ng-class="{'error' : form.type.$invalid && formSubmitted }"
</form>

Then, in my controllers:

$scope.submit = function ()
{
   $scope.formSubmitted = true; //Letting errors know they can now be displayed 
}

Therefore, the error class will only appear when formSubmitted is set to true. However, it seems that the presence of the required attribute prevents the submit function from being called unlike other attributes like minlength.

How can I achieve the desired functionality?

Answer №1

Make sure to include the attribute novalidate in your form element.

This is important because if you don't, your browser will handle form validation instead of allowing your code to manage it.

Alternatively, you can use this approach:

<form ng-submit="submit()" name="form" novalidate>
     <ion-radio ng-repeat="item in items" ng-model="data.type" name="type" ng-value="item.value" ng-class="{'error' : form.type.$invalid && form.$submitted }"
</form>

The expression form.$submitted will evaluate to true once the form has been submitted.

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

Is it necessary to include both `import 'rxjs/Rx'` and `import { Observable } from '@rxjs/Observable'` in my code?

import { Injectable } from '@angular/core'; import { Headers, Http, Response } from '@angular/http'; import { Observable } from '@rxjs/Observable'; import 'rxjs/Rx'; import 'rxjs/add/observable/throw'; @Com ...

Imposing the situation

In my current class, I have a private property and a public method for access: Person = function () { this.Name = "asd"; var _public = new Object(); _public.Name = function (value) { if (value == undefined) { //Get return ...

How can I create an HTML select dropdown menu with a maximum height of 100% and a dynamic size?

The dropdown menu I created using an HTML select tag contains a total of 152 options. However, the large number of options causes some of them to be out of view on most monitors when the size is set to 152. I attempted to limit the number of displayed opti ...

Expand the div width to the specified measurement horizontally

Is there a way to horizontally collapse a div (container) to a specific width that I can adjust, effectively hiding its content? The collapse effect should move towards the left. <div id="container"> <button type="button" id="myButton"> ...

Creating a universal alert function in AngularJS is a useful tool that can improve the functionality

I have been exploring AngularJS and I am looking to create a universal alert function that can be used across all controllers for validation and other purposes. Here is my service: JodoModule.service("CommonSrv", function ($rootScope) { this.showAler ...

In React, I am unable to invoke a function that has been assigned to an element

import React from "react"; import shortid from "shortid"; export default class App extends React.Component { state = { items: [], text: "" }; handleTextChange = event => { this.setState({ text: event.target.value }); }; ...

Please be aware that any fabricated comments will not be displayed in the posts object

-I have made an EDIT at the bottom of my original post -For my plunker, please visit this link Currently, I am learning AngularJS by following a tutorial located here. At this moment, I am stuck on the step called 'Faking comment data'. I have ...

Building on the functionality of AngularJS, a directive scope can be established to access and modify

Can a directive accept and utilize a parameter as its scope value? For instance: angular .module('app', []) .controller('CTRL', function($scope) { $scope.some_value = { instance1: { key1: 'value11', ...

What is the best way to organize input text in Angular?

As a beginner in Angular, I am working on an input text box where users can enter sorting criteria such as "rating" and "date". Within my controller, I have defined some sample data. comments: [{ rating:5, date:"2012-10-16T17:57:28.556094Z" }, { ...

When using NextJS with next-i18next and Firebase functions, all pages are redirected to a 404 error page if the locale is included

I have implemented next-i18next with Next.js in a setup that involves SSR deployed to Firebase functions. I followed the guidelines provided in this documentation https://github.com/i18next/next-i18next During development, everything functions correctly, ...

What is the pattern for identifying numbers that are followed by an underscore and then more numbers?

I need help creating a regular expression for JavaScript validation. The pattern I'm looking to match is numbers followed by an underscore, and then more numbers. For example: 123456789_123456789 The length of the string can vary - it can have any n ...

Avoid reloading the page when submitting a form using @using Html.BeginForm in ASP.NET MVC

I have a webpage featuring a model that needs to be sent to the controller along with additional files that are not part of the model. I have been able to successfully submit everything, but since this is being done in a partial view, I would like to send ...

Ways to access configuration settings from a config.ts file during program execution

The contents of my config.ts file are shown below: import someConfig from './someConfigModel'; const config = { token: process.env.API_TOKEN, projectId: 'sample', buildId: process.env.BUILD_ID, }; export default config as someCo ...

JavaScript: Append an ellipsis to strings longer than 50 characters

Can the ternary operator be utilized to append '...' if a string surpasses 50 characters? I attempted this approach, however it did not work as expected. {post.title.substring(0, 50) + post.title.length > 50 ? '...&ap ...

The functionality of changing the checkbox to "checked" by clicking on the span is not

How can I create a toggle button with a checkbox using css and jquery? Clicking on the span representing the toggle button should change the checked property of the checkbox. Currently, the span does not change the property, even though it triggers the c ...

Getting started with Tween JS on a three-dimensional cube in Three JS

I'm venturing into the world of Tween JS and attempting to create a basic animation that moves objects to the right using Tween. Here is the code snippet from my init function (utilizing Three JS): var geometry = new THREE.CylinderGeometry(200, 200, ...

Switching FormControl based on condition in Angular

I'm currently working on creating a formGroup Validation that is dependent on a toggle value public toggle:boolean=false; ngOnInit(): void { this.formGroup = this.formBuilder.group({ formArray: this.formBuilder.array([ ...

Please enter your submission once it reaches 7 characters

I am currently working on a web application that utilizes a single input form filled through a barcode scanner. I have encountered an issue where the scanner prematurely submits when scanning barcodes with more than 6 characters, resulting in incorrect dat ...

What is Causing The Card to Not Expand?

I wanted to enhance my project by incorporating a responsive card template, so I turned to one of the templates on CodePen for help. However, after copying the code into my own platform, I encountered an issue where the card wouldn't expand when click ...

Scaling Three.js Mesh to fit renderer while maintaining aspect ratio

Currently, I am using the STLLoader plugin from the Github repository of three.js. This is a snippet of my code: this.loadSTL = function(scene, stlFile){ var loader = new THREE.STLLoader(); loader.addEventListener('load', function(g ...