Thymeleaf form not submitting to Spring boot controller - forms

I am trying to submit a form to Spring boot controller
This is the thymeleaf part:
<form th:action="#{/change_password}" method="post">
<div class="row">
<div class="col-md-9 register-right">
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<h3 class="register-heading">Change password</h3>
<div class="row register-form">
<div class="col-md-6">
<div class="form-group">
<input type="email" th:name="email" id="email" class="form-control" placeholder="Email *" >
<span th:if="${notPresent}" class="alert alert-info" style="color:red; width: 100% !important; border: none; background-color: transparent !important;">This email does not exist!</span>
</div>
<div class="form-group">
<input type="password" th:name="password" id="password" class="form-control" placeholder="Password *" >
</div>
<div class="form-group">
<input type="password" th:name="confirmPassword" id="confirmPassword" class="form-control" placeholder="Confirm *" >
</div>
</div>
<div class="col-md-6">
<input type="submit" class="btnRegister" style="background-color: #ffa600 !important;" value="Change"/>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
This is the Spring boot controller method:
#PostMapping("/change_password")
public String changeUserPassword(HttpServletRequest request, Model model) {
String path = "";
User u = userService.findByEmail(request.getParameter("email"));
if(u == null || u.getActive() == 0) {
model.addAttribute("notPresent", true);
path = "redirect:/forgot_password";
} else {
u.setPassword(bCryptPasswordEncoder.encode(request.getParameter("password")));
userService.updateUser(u);
sendEmail(u.getEmail(), u.getFirstname());
path = "redirect:/login";
}
return path;
}
I don't get any errors so I am not sure what's wrong.

To submit a form you can use an object userPassword define in a Class like this:
package ....
imports ....
#Data #AllArgsConstructor #NoArgsConstructor #ToString
public class UserPassword {
private Integer id;
private String password;
... other fields you need
}
You add this object to the controller displaying the page with :
model.addAttribute("UserPassword", userPassword);
Then you add this object to your form:
<form th:action="#{/change_password}" th:object="${userPassword}" method="post">
You need to bind data to this object in your html page (here I bind password from userPassword to the input tag):
<input type="password" th:field="*{password}" class="form-control" placeholder="Password *" >
And Finnaly I retrieve the object in the controller:
#PostMapping("/change_password")
public String changeUserPassword(Model model, #ModelAttribute UserPassword userPassword) {
.....
}
You can find a complete tutorial on part 7 from https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html

Related

Bootstrap 5 form validation - required & disabled

I currently have a disabled input box. I use the box to display the sum of two range sliders, where I update the value of the box via JavaScript.
I currently require that the value of the box equals 100 before allowing the form to be submitted. Is there a work-around where I can still disable the box, where it will still adopt the same Bootstrap style formatting (change color from red to green, etc) as a non-disabled box with the 'require' option?
Following a suggestion here, I've updated the code snippet below to almost be what I want. The only thing that I'd like to change, is to make 'rSum', the box that displays the sum, disabled (while still keeping all the validation formatting features). Ideally, I want this sum to adopt the validation feedback rather than the sliders, or other mutable input objects.
function checkSum() {
let currSum = parseInt(document.getElementById('rSum').value);
if (currSum != 100) {
document.getElementById('rSum').setCustomValidity('Must sum to 100%');
return false
} else {
document.getElementById('rSum').setCustomValidity('');
return true
}
}
function updateBoxes() {
const s1 = document.getElementById('range1');
const s2 = document.getElementById('range2');
let currSum = parseInt(s1.value) + parseInt(s2.value);
document.getElementById('rangeValue1').value = (s1.value)+"%";
document.getElementById('rangeValue2').value = (s2.value)+"%";
document.getElementById('rSum').value = (currSum)+"%";
}
// This last function is the original bootstrap validation example, modified to call 'checkSum()' instead
(function () {
'use strict'
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.querySelectorAll('.custom-validation')
// Loop over them and prevent submission
Array.prototype.slice.call(forms)
.forEach(function (form) {
form.addEventListener('submit', function (event) {
if (!checkSum()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
}, false)
})
})()
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
<form class="custom-validation" novalidate>
<div class="mb-3 row">
<div class="form-group col-md-1">
<input class="form-control" type="text" id="rangeValue1" disabled>
</div>
<div class="form-group col-md-4 col-form-label">
<input type="range" class="form-range" min="0" max="100" step="5" id="range1" value="0" onchange="updateBoxes()" >
</div>
</div>
<div class="mb-3 row">
<div class="form-group col-md-1">
<input class="form-control" type="text" id="rangeValue2" disabled>
</div>
<div class="form-group col-md-4 col-form-label">
<input type="range" class="form-range" min="0" max="100" step="5" id="range2" value="0" onchange="updateBoxes()" >
</div>
</div>
<div class="mb-3 row">
<div class="form-group">
<input type="text" class="form-control text-center" id="rSum" placeholder="100%" required>
<div class="valid-feedback">
Looks good!
</div>
<div class="invalid-feedback">
Probabilities must add up to 100%
</div>
</div>
</div>
<div class="mb-3 row">
<div class="col-sm-6 offset-md-1">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
A couple of ways to do this. Here I use an example form with some inputs that can be used in the slider validation (one is a range slider but could be a simple number input also).
I set an event handler for those elements, associate them using data attributes and then; very verbosely and a bit ugly for clarity, use them in a function.
function checkSum(event) {
const fearRequird = this.fearNeed;
const myFear = event.target;
const associated = document.querySelector(myFear.dataset.related);
const result = this.sumEl;
const thisValue = parseInt(myFear.value);
const associatedValue = parseInt(associated.value);
let currSum = thisValue + associatedValue;
result.value = currSum;
let isValidSlider = currSum >= fearRequird;
result.classList.toggle("is-valid", isValidSlider);
result.classList.toggle("is-invalid", !isValidSlider);
let t = isValidSlider ? '' : `You Fear ${thisValue} and ${associatedValue} total ${currSum}. You need total ${fearRequird} fear`;
result.setCustomValidity(t);
result.closest('.form-group').querySelector('.invalid-feedback').textContent = t;
return isValidSlider;
}
(function() {
'use strict'
// Fetch all the forms we want to apply custom Bootstrap validation styles to
let forms = document.querySelectorAll('.needs-validation');
// Loop over them and prevent submission
Array.prototype.slice.call(forms)
.forEach(function(form) {
form.addEventListener('submit', function(event) {
if (!form.checkValidity()) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false)
});
const fearNeed = 100;
const myFears = document.querySelectorAll('.fear-factor');
const needs = {
fearNeed: fearNeed,
sumEl: document.querySelector('#rSum')
};
myFears.forEach((fearElement) => {
fearElement.addEventListener('change', checkSum.bind(needs), false);
});
})();
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
<form class="row g-3 needs-validation mx-1 my-2" novalidate>
<div class="form-group">
<input type="text" class="form-control text-center" id="rSum" placeholder="100%" required>
<div class="valid-feedback">
Looks good!
</div>
<div class="invalid-feedback">
Fears must add up to 100%
</div>
</div>
<div class="col-md-4">
<label for="validationCustomUsername" class="form-label">Fish Name</label>
<div class="input-group has-validation">
<span class="input-group-text" id="inputGroupPrepend">Fish</span>
<input type="text" class="form-control" id="validationCustomFishname" aria-describedby="inputGroupPrepend" required>
<div class="invalid-feedback">
Must catch a fish.
</div>
<div class="valid-feedback">
Looks like an good catch!
</div>
</div>
</div>
<div class="col-md-3">
<label for="validationCustomFearMet" class="form-label">Scary cats fear</label>
<div class="input-group has-validation">
<input type="number" class="form-control fear-factor" min="0" max="100" step="1" value="13" id="validationCustomFearMet" data-related="#customRange3" required />
<span class="input-group-text" id="inputGroupPrepend">%</span>
<div class="invalid-feedback">
Please provide scary cats fear as a percent 0-100.
</div>
<div class="invalid-feedback">
Please provide scary cats fear as a percent 0-100.
</div>
</div>
</div>
<div class="mx-1">
<label for="customRange3" class="form-label">Fear range</label>
<input type="range" class="form-range fear-factor" min="0" max="100" step="1" value="0" data-related="#validationCustomFearMet" id="customRange3">
</div>
<div class="col-12">
<button class="btn btn-primary" type="submit">Submit form</button>
</div>
</form>

Nuxt and Netlify Form File Upload Problem

in a Nuxt site I have a form with a field of type "file", and a Name, Email and Message fields.
Name, Email and Message are working flawlessy everytime, instead the "file" field ONLY WORKS when the first page visited is the page where the form is (or it's reloaded). If a user, let's say, land on the homepage and only then reach the form page, the message sent from the form will not contain the file that was attached.
What can cause this? I've already tried to reset the form on the mounted hook, or reset only the "file" field but with no luck.
This is the form component code "TheFormJob.vue":
<template>
<div>
<form name="job" netlify-honeypot="bot-field" method="post" action="/success" data-netlify="true">
<input type="hidden" name="form-name" value="job" >
<p class="hidden">
<label>Don’t fill this out: <input name="bot-field"></label>
</p>
<div class="form">
<div class="form__name">
<input class="form__field" name="name" id="name" :placeholder="$t('contatti.nomeForm')" required >
</div>
<div class="form__email">
<input class="form__field" type="email" name="email" id="email" placeholder="Email*" required >
</div>
<div class="form__upload">
<input class="form__field" type="file" name="fileToUpload" id="fileToUpload" accept=".doc, .docx, .pdf" required>
</div>
<div class="form__textarea">
<textarea class="form__field" name="message" id="message" :placeholder="$t('contatti.messaggioForm')" required></textarea>
</div>
<div class="form__privacy extra-mt">
<label><input type="checkbox" name="privacy" required> {{$t('contatti.privacy')}}</label>
</div>
<div class="form__send extra-mt">
<button class="default-button" type="submit" value="Invia" >{{$t('contatti.invia')}} </button>
</div>
</div>
</form>
</div>
</template>
<script>
export default {
mounted(){
const dis = this
const uploadField = document.getElementById("fileToUpload");
uploadField.onchange = function() {
if(this.files[0].size > 1048576){ // 1MB
alert(dis.$t('lavora.fileAlert'));
}
}
}
}
</script>
In the page "index.vue":
<template>
<div>
<div class="container">
<div class="first row extra-mt5-md is-center">
<div class="col-12 col-4-md text-center" style="padding: 2rem 0">
<h1>{{$t('lavora.title')}}</h1>
</div>
</div>
<div class="second row">
<div class="col-12 col-4-md image" />
<div class="col-12 col-8-md content">
<p>{{$t('lavora.p1')}}</p>
<TheFormJob class="extra-mt5" />
</div>
</div>
<div class="third row">
<div class="col-12">
<nuxt-link class="default-button-full" :to="localePath('contatti')">{{$t('contatti.title')}} → </nuxt-link>
</div>
</div>
</div>
</div>
</template>

I have a nested group Angular form. I want to take the group's controls values and match the name of the controls to my JSON object

I have a small contact form that I have built using Angular. I want to validate the form and change the form data to JSON object.
Here's my Form:
<form [formGroup]="addContactForm" (ngSubmit)="onSubmit()" novalidate >
<div [hidden]="addcontactForm.submitted">
<div class="modal-body" style="overflow: auto">
<!-- create contact -->
<div style="padding: 0 0px 0px 25px;margin-top:30px;">
<div class="form-horizontal">
<span *ngIf="ACname.invalid && (ACname.dirty || ACname.touched)" class="has-error">
<span *ngIf="ACname.errors.required">
Last Name is required.
</span>
</span>
<!-- name -->
<div FormGroupName="ACname">
<div class="form-group" style="text-align:right" [ngClass]="{ 'has-error': ACname.addContactFirstName.invalid && (ACname.addContactFirstName.dirty || ACname.addContactFirstName.touched) }">
<label class="col-sm-3" for="addContactFirstName">First Name</label>
<div class="col-sm-7">
<input id="addFirstName"
formControlName="addContactFirstName"
class="form-control"
placeholder="Enter First Name" />
</div>
</div>
<div class="form-group" style="text-align:right" [ngClass]="{ 'has-error': ACname.addContactLastName.invalid && (ACname.addContactLastName.dirty || ACname.addContactLastName.touched) }">
<label class="col-sm-3" for="addContactLastName">Last Name</label>
<div class="col-sm-7">
<input id="addLastName"
class="form-control"
formControlName="addContactLastName"
placeholder="Enter Last Name" />
</div>
</div>
</div>
<div FormGroupName="ACcontactMethod">
<!-- office phone -->
<div class="form-group" style="text-align:right" [ngClass]="{ 'has-error': ACcontactMethod.addcontactForm.submitted && !ACcontactMethod.addContactOfficePhone.valid }">
<label class="col-sm-3" for="addContactOfficePhone">Office Phone</label>
<div class="col-sm-7">
<input id="addofcPhone"
type="text"
class="form-control"
formControlName="addContactOfficePhone"
placeholder="Enter Office Number" />
<span *ngIf="addContactOfficePhone.invalid && (addContactOfficePhone.dirty || addContactOfficePhone.touched)" class="has-error">
<span *ngIf="addContactLastName.errors.required">
Name is required.
</span>
</span>
</div>
</div>
<!-- mobile phone -->
<div class="form-group" style="text-align:right" [ngClass]="{ 'has-error': addcontactForm.submitted && !addContactMobilePhone.valid }">
<label class="col-sm-3" for="addContactMobilePhone">Mobile Phone</label>
<div class="col-sm-7">
<input id="addmobPhone"
type="text"
class="form-control"
formControlName="addContactMobilePhone"
placeholder="Enter Mobile Number" />
<span *ngIf="addContactMobilePhone.invalid && (addContactMobilePhone.dirty || addContactMobilePhone.touched)" class="has-error">
<span *ngIf="addContactMobilePhone.errors.required">
Name is required.
</span>
</span>
</div>
</div>
<!-- home phone -->
<div class="form-group" style="text-align:right" [ngClass]="{ 'has-error': addcontactForm.submitted && !addContactHomePhone.valid }">
<label class="col-sm-3" for="addContactHomePhone">Home Phone</label>
<div class="col-sm-7">
<input id="addhomPhone"
type="text"
class="form-control"
formControlName="addContactHomePhone"
placeholder="Enter Home Number" />
<span *ngIf="addContactHomePhone.invalid && (addContactHomePhone.dirty || addContactHomePhone.touched)" class="has-error">
<span *ngIf="addContactHomePhone.errors.required">
Name is required.
</span>
</span>
</div>
</div>
<!-- email -->
<div class="form-group" style="text-align:right" [ngClass]="{ 'has-error': addcontactForm.submitted && !addContactEmail.valid }">
<label class="col-sm-3" for="addContactEmail">Email</label>
<div class="col-sm-7">
<input id="addEmail"
type="email"
class="form-control"
formControlName="addContactEmail"
placeholder="Enter Email" />
<span *ngIf="addContactEmail.invalid && (addContactEmail.dirty || addContactEmail.touched)" class="has-error">
<span *ngIf="addContactEmail.errors.required">
Name is required.
</span>
</span>
</div>
</div>
<!-- chat id -->
<div class="form-group" style="text-align:right" [ngClass]="{ 'has-error': addcontactForm.submitted && !addContactChatId.valid }">
<label class="col-sm-3" for="addContactChatId">Chat ID</label>
<div class="col-sm-7">
<input id="addChatID"
type="text"
class="form-control"
formControlName="addContactChatId"
placeholder="Enter Chat ID" />
<span *ngIf="addContactChatId.invalid && (addContactChatId.dirty || addContactChatId.touched)" class="has-error">
<span *ngIf="addContactChatId.errors.required">
Name is required.
</span>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-left" (click)="addcontactForm.reset()" data-dismiss="modal">Close</button>
<button type="submit"
class="btn btn-primary"
[disabled]="!addcontactForm.valid"
(click)="addContact(model);">
Add Contact
</button>
</div>
</div>
<div class="submitted-message" *ngIf="addcontactForm.submitted">
<p>You've submitted your contact, {{ addcontactForm.value.addContactFirstName }} {{ addcontactForm.value.addContactLastName }}!</p>
<button type="button" class="btn btn-default pull-left" (click)="addcontactForm.reset()" data-dismiss="modal">Close</button>
<button (click)="addcontactForm.resetForm({})">Add new Contact </button>
</div>
</form>
Here's my ts:
import { Component} from '#angular/core';
import { AppComponent } from '../app.component';
import { FormBuilder, Validators, FormGroup, FormControl } from '#angular/forms';
#Component({
selector: 'addcontactmodal',
templateUrl: 'addcontact.component.html'
})
export class AddContactModalComponent {
id: any;
addContactForm: FormGroup;
constructor(private _appComponent: AppComponent, private fb: FormBuilder) {
this.id = localStorage.getItem('Id');
this.addContactForm = this.fb.group({
ACname: new FormGroup({
addContactFirstName: new FormControl('', Validators.minLength(40)),
addContactLastName: new FormControl('', Validators.minLength(40)),
}),
ACcontactMethod: new FormGroup({
addContactOfficePhone: new FormControl('', Validators.minLength(20)),
addContactMobilePhone: new FormControl('', Validators.minLength(20)),
addContactHomePhone: new FormControl('', Validators.minLength(20)),
addContactEmail: new FormControl('', Validators.minLength(127)),
addContactChatId: new FormControl('', Validators.minLength(127))
})
});
}
// private method(s)
private addContact() {
let data = {
ChatId: this.fb.group('addContactChatId').value,
Email: addContactEmail,
FirstName: addContactFirstName,
HomePhone: addContactHomePhone,
MobilePhone: addContactMobilePhone,
LastName: addContactLastName,
OfficePhone: this.model.addContactOfficePhone
}
this._appComponent.signalRService.setAgentContact(this.id, data);
}
}
I want to:
Validate the form
Have the data output to JSON
I do not get any of the validation the form promises. It doesn't submit.
Errors:
nhandled Promise rejection: Cannot read property 'invalid' of undefined ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Cannot read property 'invalid' of undefined
at Object.View_AddContactModalComponent_0.co [as updateDirectives]
You are not using complete property paths for your validation messages. Here's a simplified template of yours:
<form [formGroup]="addContactForm" (ngSubmit)="onSubmit()" novalidate >
<!-- formGroupName - mark all form controls belonging to this group inside tag -->
<div formGroupName="ACname">
<input formControlName="addContactFirstName" />
<!-- use complete property path or do like follows! -->
<span *ngIf="addContactForm.hasError('minlength', 'ACname.addContactFirstName')">
Minlength 40
</span>
</div>
</form>
StackBlitz

How to access to RESTController through a controller?

I am using Spring.
The method in my RESTController is this,
http://localhost:8080/delivery-api/training/submit. It is able to save hard code values into database.
This is part of my html form, it can be open up using this URL: http://localhost:8080/delivery-web/training/apply.
<form th:object="${applyForm}" class="form-horizontal" role="form" method="post" action="http://localhost:8080/delivery-api/training/submit" >
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1" > Country </label>
<div class="col-sm-9">
<input type="text" id="form-field-1" placeholder="Country"
class="col-xs-10 col-sm-7" th:field="*{country}" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1"> Fee </label>
<div class="col-sm-9">
<input type="text" id="form-field-1" th:field="*{Fee}" class="col-xs-10 col-sm-7" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1"> Start Date </label>
<div class="col-sm-9">
<input type="date" id="form-field-1" th:field="*{startDate}"
class="col-xs-10 col-sm-7" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1">End Date </label>
<div class="col-sm-9">
<input type="date" id="form-field-1" th:field="*{endDate}"
class="col-xs-10 col-sm-7" />
</div>
</div>
<br> <br>
<button class="btn btn-info" type="submit">
<i class="ace-icon fa fa-check bigger-110"></i> Submit
</button>
</div>
</div>
</form>
When I click on submit, it can be directed to http://localhost:8080/delivery-api/training/submit and insert the hard coded values into database, but it did not direct to my application-form.html page after that.
My controller
#PostMapping(value = "/apply")
public String apply(#Valid #ModelAttribute("applyForm") DeliveryApplicationForm applyForm, BindingResult errors, Model model) {
model.addAttribute("applyForm", applyForm);
return VIEW_PATH + "application-form";
}
I would like to render html page and also pass data from my form to RESTController method through a controller after I click on the submit button in my html page.
May I know how I can do it?

MVC 4 Input Submit Button not firing Action

I cannot get the my HttpPost action to fire.
In HomeController.cs I have the following:
public ActionResult TestForm()
{
return View();
}
[HttpPost]
public ActionResult TestForm(TestForm testForm)
{
int x = 1;
return View();
}
In my View code I build my form with
#using (Html.BeginForm())
{
....
}
The rendered HTML is
<form action="/Home/TestForm" method="post"> <div style="display: table">
<div style="display: table-row">
<div style="display: table-cell"><label for="Cmd">CMD String</label></div>
<div style="display: table-cell">
<input class="CmdForm" id="Cmd" name="Cmd" type="text" value="" />
</div>
</div>
<div style="display: table-row">
<div style="display: table-cell"><label class="MacAddress" for="MacAddress">MAC Address</label></div>
<div style="display: table-cell">
<input id="MacAddress" name="MacAddress" type="text" value="" />
</div>
</div>
<div style="display: table-row">
<div style="display: table-cell"></div>
<div style="display: table-cell">
<input type="button" value="Submit"/>
</div>
</div>
</div>
I've put a break point at int x=1; just to see if it's hitting the function on button click but it does not hit it. Can someone give me some tips on what I might be missing?
You put a button for submission which is not of the right type. You should use submit as a type, not button.
Change the following code from
<div style="display: table-cell">
<input type="button" value="Submit"/>
</div>
to
<div style="display: table-cell">
<input type="submit" value="Submit"/>
</div>