Html and angular js - html

I have some fields inside a form tag and submit button outside a form tag. If any of the fields inside a form is empty my form should not be submitted. For this I used the required attribute but it was not working since submit button was outside of the <form> and also used novalidate attribute. Nothing seems to work.
Below is my code:
<section id="profile" class="content-header">
<div class="thumbnail">
<button type="submit" class="btn btn-block btn-success" ng-if="!viewProfile" ng-click="updateProfile();">SUBMIT</button>
</div>
<div class="row">
<form name="profileForm" role="form">
<div class="form-group col-md-6">
<label for="firstName"> First Name </label>
<input type="text" class="form-control" id="firstName" ng- model="profileDetails.firstName" ng-disabled="viewProfile" required/>
</div>
<div class="form-group col-md-6" ng-if="userDetails.role==0">
<label for="firstName"> Middle Name </label>
<input type="text" class="form-control" id="middleName" ng- model="profileDetails.middleName" ng-disabled="viewProfile" />
</div>
</form>
</div>
</section>
like FirstName and MiddleName there are many other fields without filling any of those fields my form should not be submitted, can anyone suggest me the best approach for this. My angularController is:
(function() {
function profileController($scope, profileFactory, $loading) {
$scope.updateProfile = function() {
$loading.start("main");
profileFactory
.updateProfile($scope.profileDetails)
.then(function(response) {
var updatedUserDetails = response.user;
if (updatedUserDetails.imagePath !== null) {
$scope.profileDetails.imagePath = updatedUserDetails.imagePath;
$scope.profileDetails.profieImageBytes = updatedUserDetails.profieImageBytes;
}
angular.extend($scope.userDetails, $scope.profileDetails);
$scope.editProfile();
})
.catch(function() {
$loading.finish("main");
});
};
$loading.finish("main");
}
profileController.$inject = ["$scope", "profileFactory", "$loading"];
angular
.module("vResume.profile")
.controller("profileController", profileController);
})();

You typically use the .$invalid field on the form with ng-disabled for this.
<button type="submit" class="btn btn-block btn-success" ng-disabled="profileForm.$invalid" ng-if="!viewProfile" ng-click="updateProfile();">SUBMIT</button>
See the angularjs docs.

Related

Form doesn't work when submit button is in the modal window

I have HTML form with submit button inside. Inside the form there are piece of HTML code which popups in modal window when filling up the form. Inside this modal are one more input and submit button. And it does not work, the form doesn't send to email. But when I move entire button HTML code right before </form> then submit button works. Here is a code:
<form name="autoForm" id="contact-form" class="row" method="post" action="php/send.php">
<div class="form-group col-md-6">
<label id="address-label" for="place">* Markė | modelis | metai | TA | miestas</label>
<input id="form_place" type="text" name="place" class="form-control" data-error="Laukas privalomas" >
<div class="help-block with-errors"></div>
</div>
<div class="form-group col-md-12">
<label for="message">Automobilio būklė</label>
<textarea id="form_message" name="message" class="form-control h-auto" rows="4" data-error="Prašau, parašykite daugiau apie NT"
placeholder="Čia galite parašyti daugiau informacijos apie automobilio būklę"></textarea>
<div class="help-block with-errors"></div>
</div>
<div class="submit-container col mt-5">
<a class="btn btn-primary" id="to-modal-form"><font size="4">Sužinoti kainą</font></a>
</div>
<div id="modal-background" class="modal-background container-hidden">
<div id="modal-form" class="modal-form">
<a id="modal-close"><i class="icon-close" data-v-27b8b94e=""></i></a>
<div class="form-header">
<h2>Gaukite kainos pasiūlymus tiesiai į savo telefoną.</h2>
<p>Įveskite telefono numerį ir gaukite pasiūlymo nuorodą <b>SMS</b></p>
</div>
<div class="form-body">
<div class="form-group col-md-6">
<label class="phone" for="phone">* Jūsų telefono numeris</label>
<input id="form_phone" type="phone" name="phone" class="form-control" required="required" data-error="Laukas privalomas">
<div class="help-block with-errors"></div>
</div>
<input type="hidden" name="code" value="" id="code" />
<button class="btn btn-primary" type="submit" id="form_submit"><font size="4">Tęsti</font></button>
<span class="privacy">Sutinku su privatumo politika</span>
</div>
</div>
</form>
Filling up first inputs there are first button <a class="btn btn-primary" id="to-modal-form"><font size="4">Sužinoti kainą</font></a> which is opening the modal windows. Inside after filling up additional input I press <button class="btn btn-primary" type="submit" id="form_submit"><font size="4">Tęsti</font></button> which doesn't work.
But form work when button is moved before </form>. Form sends all values - inside and out of modal window.
Can't get what is wrong.
If you have button outside </form> it would not work the solution is using form="" parameter to the button it basically tell this button belong to this specific form element
<button class="btn btn-primary" form="contact-form" type="submit" id="form_submit"><font size="4">Tęsti</font></button>
Your submit button will confused which form that it should submitted if you place it outside the form tag.
You could use javascript to submit the form.
https://www.javascript-coder.com/javascript-form/javascript-form-submit/
Put a button inside your modal with onclick="submitForm('contact-form');", then add below js code
function submitForm(id) {
document.getElementById(id).submit();
}
Solved this by adding submit button before with attribute hidden:
<form>
<!-- the form content -->
<button class="btn btn-primary" type="submit" id="form_submit" hidden></button>
</form>
And that button inside of modal window made as trigger with attribute onclick="submitForm();" like this:
<button class="btn btn-primary" onclick="submitForm();"><font size="4">Tęsti</font></button>
In JS it triggers submit button using .click() method:
const submitBtn = document.getElementById('form_submit');
function submitForm() {
submitBtn.click();
}

How to pass value from ejs form to nodejs

The below code works fine, while fetching.. but i don't want the subflag div occupying the space which affects the submit button in displaying in correct position. Is there any other way possible to pass the values to app.js
app.post("/activitieslist/:id/edit", function(req. res) {
var astname = req.body.astname;
var subflag = req.body.subflag;
console.log(astname, subflag);
});
<form action="/activitieslist/<%= activitylist._id %>/edit" method="POST">
<div class="form-group">
<label>Asset Name :
</label>
<input class="form-control" type="text" name="astname" value="<%= activitylist.astname %>">
</div>
<div class="form-group invisible">
<input type="text" name="subflag" value="N">
</div>
<button class ="btn btn-info"><i class="fas fa-edit"></i>Save</button>
</form>

Trigger action in a particular element of a collection inside an ng-repeat

I have an array of objects that is displayed using ng-repeat. In each of these objects there's a button that triggers an API request and toggles a flag in order to show a hidden div. Here's the HTML:
<div ng-repeat="tarjeta in tarjetas">
<label class="radio-inline">
<input type="radio" name="card" checked> {{tarjeta.brand}} ************{{tarjeta.digits}}
<button class="btn btn-dropdown" ng-click="openEditCard(); getEditableInfoCard(tarjeta.id)" ng-show="!editCard"></button>
<button class="btn btn-up" ng-click="openEditCard()" ng-show="editCard"></button>
</label>
<div ng-show="editCard">
<input type="text" name="nombre" placeholder="Nombre en la tarjeta:" class="form-control" ng-model="editableInfo.name" required>
<p ng-show="validaUpdateName">Dato incorrecto</p>
<div style="float: left; margin-right: 15px;">
<input type="text" name="expM" placeholder="Expiración (MM):" maxlength="2" class="form-control" ng-model="editableInfo.exp_month" required>
<p ng-show="validaUpdateMM">Dato incorrecto</p>
</div>
<label class="dash">
/
</label>
<div style="float: left;">
<input type="text" name="expY" placeholder="Expiración (YYYY):" maxlength="2" class="form-control" ng-model="editableInfo.exp_year" required>
<p ng-show="validaUpdateYY">Dato incorrecto</p>
</div>
<input type="text" name="direccion" placeholder="Dirección de la tarjeta:" class="form-control" required>
<br>
<br>
<br>
<div style="float: right;">
<button class="btn btn-primary" ng-click="updateCard(tarjeta)">Guardar cambios</button>
<button class="btn btn-trash"></button>
</div>
</div>
The problem is that when I click the button on a single element, the action is triggered for every element of the array. I think this is quite simple to solve but I'm a beginner using Angular and can't figure out what the problem is. I already tried tracking by $index. This is driving me crazy any help is welcome.
It really hasn't much to do with angular. Only with logic.
You're using single boolean editCard, and based on that boolean, you're supposed to know which cards should be opened, and which ones should not be. That can't possibly work. A single boolean can't store that amount of information. Only true or false.
Instead, you need, for each card to know if it is opened or not. So each card should have its own boolean flag:
<div ng-repeat="tarjeta in tarjetas">
<button ng-click="tarjeta.edited = true" ...></button>
<button ng-click="tarjeta.edited = false" ...></button>
<div ng-if="tarjeta.edited">
...
</div>
</div>
And if only one card is supposed to be opened at a time, thenwhat you need to know is which of the card is opened. Again, a boolean can't store that information. So you'd need
<div ng-repeat="tarjeta in tarjetas">
<button ng-click="edit(tarjeta)" ...></button>
<button ng-click="edit(null)" ...></button>
<div ng-if="tarjeta === editedTarjeta">
...
</div>
</div>
and in the controller:
$scope.editedTarjeta = null;
$scope.edit = function(tarjeta) {
$scope.editedTarjeta = tarjeta;
};

Validation of angular form without submit button

I am using anuglar.js form validation.
I want to check validation without form submit button.
This is my html.
<form name="userForm">
<input type="hidden" ng-model="StandardID" />
<div class="form-group">
<label for="email">Name:</label>
<input type="text" class="form-control" ng-model="Name" placeholder="Name" maxlength="50" required>
<span ng-show="userForm.Name.$dirty && !userForm.Name.$valid">Name is required.</span>
</div>
<div class="form-group">
<label for="pwd">Alias:</label>
<input type="text" class="form-control" ng-model="Alias" placeholder="Alias" maxlength="50" required>
</div>
<button type="submit" class="btn btn-info" ng-click="update()">Submit</button>
<button type="submit" class="btn btn-info" data-dismiss="modal">Cancel</button>
</form>
My controller js is.
$scope.update= function () {
alert($scope.userForm.Name.$valid);
if ($scope.userForm.Name.$valid) {
alert("Entry added");
}
}
It shows small dialog that says this field is required. I don't want this popup . Why span section does not show?
To do it, the key is use the name attribute in your <form> and in all of your inputs.
Following this way, angularjs will create a form instance where you can access all fields like myForm.myInput.$invalid
For example:
<form name="myForm" novalidate>
<label>My Input:</label>
<input type="text" name="myInput" ng-model="myInput" required>
<span ng-show="myForm.myInput.$dirty && myForm.myInput.$invalid">My Input is invalid.</span>
</form>
Check angular docs
HTML
<div ng-app="myApp">
<form name="cutome_form" ng-submit="submitForm()" novalidate>
<input type="text" name="name" class="form-control" ng-model="Name" placeholder="Name" maxlength="50" required>
<div ng-show="cutome_form.name.$dirty && cutome_form.name.$invalid">
<span class="error" ng-show="cutome_form.name.$error.required">The field is required.</span>
</div>
<div class="form-group">
<label for="pwd">Alias:</label>
<input type="text" name="alias" class="form-control" ng-model="Alias" placeholder="Alias" maxlength="50" required>
</div>
<div ng-show="cutome_form.alias.$dirty && cutome_form.alias.$invalid">
<span class="error" ng-show="cutome_form.alias.$error.required">The field is required.</span>
</div>
<button type="submit" ng-disabled="cutome_form.$invalid">Submit All</button>
</form>
</div>
directive
.directive('ngModel', function() {
return {
require: 'ngModel',
link: function(scope, elem, attr, ngModel) {
elem.on('blur', function() {
ngModel.$dirty = true;
scope.$apply();
});
ngModel.$viewChangeListeners.push(function() {
ngModel.$dirty = false;
});
scope.$on('$destroy', function() {
elem.off('blur');
});
}
}
});

Form with predefined values AngularJs

I am using AngularJs and have inputs with predefined value from ng-repeat. I want resend those data value via ng-click function to treat data.
The problem the data is not submitted
<label>Price</label>
<input type="text" class="form-group form-control" ng-value=s.prix required="required" ng-model="prix" >
</div>
<div class="modal-footer">
<button type="submit" id='submit' ng-click="edit()" class="btn btn-primary" data-dismiss="modal">Save</button>
s.prix is a value of column in the table prix (ng-repeat="s in produit") if you know what i mean.
Use ng-repeat instead as model
Like this
<input type="text" class="form-group form-control" required="required" ng-model="s.prix" >
Then pass object in edit.
Try like this
ng-click="edit(s)"
JS
$scope.edit=function(s){
console.log(s);
}
html:
<body ng-controller="myCtrl">
<label>Price</label>
<div ng-repeat="s in produit">
<input type="text" class="form-group form-control" required="required" ng-model="s.prix" >
<button type="submit" id='submit' ng-click="edit(s)" class="btn btn-primary" data-dismiss="modal">Save</button>
</div>
</body>
controller:
myApp.controller('produitCtrl', ['$scope',function($scope){
$scope.produit = [{prix:'hello'}];
$scope.edit=function(s){
console.log(s);
}
}]);