Data from thymelaf form not saving properly into db - forms

I'm dealing with saving an entity from thymealaf form to database.
My current approach.
Thymeleaf form in register.html
<form action="save" th:method="post" th:action="#{save}" th:object="${customer}">
<input type ="text" th:field="*{firstName}" placeholder="First Name" /><br />
<input type ="text" th:field="*{lastName}" placeholder="Last Name" /><br />
<input type="email" th:field="*{emailAddress}" placeholder="Email" /><br />
<input type="password" th:field="*{password}" placeholder="Password" /><br />
<input style="text-align: center" type="submit" value="Register" /> </form>
Controller methods:
#RequestMapping("/home")
public String home(Model model){
model.addAttribute("customer", new Customer());
return "register";
}
#RequestMapping("save")
public String save(#ModelAttribute(value = "customer") Customer customer) {
customerRepository.save(customer);
return "saved";
}
There is an issue with data binding as you can see in the attached screenshot

Problem solved. Just needed to change dependencies from:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
to
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>

as I can see you did not mention the name in the input field thymelaf binds data using the name
so u have to add a name in the input field as follow
<form action="save" th:method="post" action="save" th:object="${customer}">
<input type ="text" name="first_name" placeholder="First Name" /><br />
<input type ="text" name="last_name" placeholder="Last Name" /><br />
<input type="email" name="email_address" placeholder="Email" /><br />
<input type="password" name="password" placeholder="Password" /><br />
<input style="text-align: center" type="submit" value="Register" /> </form>
note:- name in the input field is should match with your names from the table

Related

How prevent form submit in google apps script

I am importing a form written in GoogleApps Script into an iframe on a page built with Squarespace but for the life of me cannot prevent the form from submitting. I am using:
window.addEventListener( 'load', preventFormSubmit );
as suggested in GAS documentation but this does not seem to be triggering the preventFormSubmit function. Instead, when the submit button is clicked the form submits and goes to a blank google page. Also the alerts in the preventFormSubmit function (now commented out) never display which suggests that the form is never called.
I have spent days on this, cannot find an answer anywhere and can no longer see the woods for the trees. Any clues as to what I am doing wrong?
Squarespace is a website builder which enables one to embed code, in this case as an iframe.
My code:
js.html:
<script>
function preventFormSubmit() {
//alert( "prevent form submit triggered" );
var forms = document.querySelectorAll('form');
for (var i = 0; i < forms.length; i++) {
forms[i].addEventListener('submit', function(event) {
event.preventDefault();
});
}
//alert( "forms prevented from submitting: " = forms.length );
}
window.addEventListener( "ready", preventFormSubmit );
function handleFormSubmit(formObject) {
google.script.run
.withSuccessHandler( showSuccess )
.withFailureHandler( showError )
.processForm_1( formObject );
}
</script>
html:
<!DOCTYPE html >
<head>
<base target="_top" >
<?!= include('css'); ?>
<?!= include('js'); ?>
</head>
<body>
<div id="formDiv" class="card" >
<h2 id="title" >Alternative Booking form</h2>
<form id="alternative-booking-form-1" onsubmit="handleFormSubmit(this)" >
<fieldset>
<legend>About You</legend>
<p>Please tell us a bit about yourself.
</p>
<input type="text" id="firstName" name="firstName" form="alternative-booking-form-1"
placeholder="your first name" value="" required
/><br />
<input type="text" id="lastName" name="lastName" form="alternative-booking-form-1"
placeholder="your last name" value="" required
/><br />
<input type="text" id="title" name="title" form="alternative-booking-form-1"
placeholder="your title, eg: mr, mrs, ms etc" value="" /><br>
</fieldset>
<fieldset>
<legend>Your Contact Details</legend>
<p>We will only use your contact details in case we need to contact you with regard to
this booking, unless you consent
to further communications, as offered later in this booking process.</p>
<input type="email" id="email" name="email" form="alternative-booking-form-1"
placeholder="your email address" value=""
required /><br />
<input type="tel" id="phone" name="phone" form="alternative-booking-form-1"
placeholder="phone" value="" required /><br />
</fieldset>
<fieldset>
<input type="hidden" id="form" name="form" form="alternative-booking-form-1" value="1" />
<br />
<input type="submit" id="submit" name="submit" form="alternative-booking-form-1"
class="red" value="Next →" />
<br />
<br />
</fieldset>
</form>
</div>
<div id="output" name="output" ></div>
</body>
<!DOCTYPE html >
<head>
<base target="_top" >
<?!= include('css'); ?>
<?!= include('js'); ?>
</head>
<body>
<div id="formDiv" class="card" >
<h2 id="title" >Alternative Booking form</h2>
<form id="alternative-booking-form-1" >
<fieldset>
<legend>About You</legend>
<p>Please tell us a bit about yourself.
</p>
<input type="text" id="firstName" name="firstName" form="alternative-booking-form-1"
placeholder="your first name" value="" required
/><br />
<input type="text" id="lastName" name="lastName" form="alternative-booking-form-1"
placeholder="your last name" value="" required
/><br />
<input type="text" id="title" name="title" form="alternative-booking-form-1"
placeholder="your title, eg: mr, mrs, ms etc" value="" /><br>
</fieldset>
<fieldset>
<legend>Your Contact Details</legend>
<p>We will only use your contact details in case we need to contact you with regard to
this booking, unless you consent
to further communications, as offered later in this booking process.</p>
<input type="email" id="email" name="email" form="alternative-booking-form-1"
placeholder="your email address" value=""
required /><br />
<input type="tel" id="phone" name="phone" form="alternative-booking-form-1"
placeholder="phone" value="" required /><br />
</fieldset>
<fieldset>
<input type="hidden" id="form" name="form" form="alternative-booking-form-1" value="1" />
<br />
<input type="button" id="submit" name="submit" form="alternative-booking-form-1"
class="red" value="Next →" />
<br />
<br />
</fieldset>
</form>
</div>
<div id="output" name="output" ></div>
<script>
window.onload = function() {
document.getElementById("alternative-booking-form-1").addEventListener( "ready", handleFormSubmit );
}
function handleFormSubmit(formObject) {
google.script.run
.withSuccessHandler( showSuccess )
.withFailureHandler( showError )
.processForm_1( formObject );
}
</script>
</body>

Shopify Omnisend Custom HTML Signup Form

I am using omnisend app for Shopify subscribe list. I am using a custom form to integrate the input data.
For the ominsend form, they have own class to capture data.
Like this:
<form class="my-form omnisend-subscribe-form" action="/subscribe" method="post">
<input class="omnisend-subscribe-input-email" type="text" placeholder="Enter your email address" />
<input class="omnisend-subscribe-input-first-name" type="text" placeholder="Enter your first name" />
<input class="omnisend-subscribe-input-last-name" type="text" placeholder="Enter your last name" />
<input type="submit" value="Subscribe!">
</form>
First Name: omnisend-subscribe-input-first-name
Last Name: omnisend-subscribe-input-last-name
Email: omnisend-subscribe-input-email
But for the birthday input data, I am unable to find the class.
How can I dynamic the birthday input data by class?
<form class="my-form omnisend-subscribe-form" action="/subscribe" method="post">
<input class="omnisend-subscribe-input-email" type="text" placeholder="Enter your email address" />
<input class="omnisend-subscribe-input-first-name" type="text" placeholder="Enter your first name" />
<input class="omnisend-subscribe-input-last-name" type="text" placeholder="Enter your last name" />
<input class="omnisend-subscribe-input-?" type="text" placeholder="MM" />
<input class="omnisend-subscribe-input-?" type="text" placeholder="DD" />
<input class="omnisend-subscribe-input-?" type="text" placeholder="YYYY" />
<input type="submit" value="Subscribe!">
</form>
Actually, there is no way to collect the birthday form the omnisend form. This form is not support to collect birthday.

How to send data from input using asp-route attribute?

I'm using this code to call controller with action.
I want to send data from input tag inside asp-route-data.
<span><label><b>Korisnik :</b></label></span>
<br />
<input type="text" name="korisnik" id="korisnik" size="40" height="25" />
<br />
<br />
<span><label><b>Lozinka :</b></label></span>
<br />
<input type="password" name="lozinka" id="lozinka" size="40" height="25" />
<br />
<br />
<button type="submit" title="Login" class="btnLogin" asp-controller="Members" asp-route-username=a sp-route-password="" asp-action="LoginUser">
So, asp-route-username should be the value from input korisnik.
Is it possible?
If I understood your question correctly (of which I'm not so sure) I think the answer to your problem is it use <form> tag. Then instead of appling the asp-route attributes on <button> tag, apply them on <form> tag:
<form asp-controller="Members" asp-action="LoginUser" asp-route-username="a" asp-route-password="">
<span><label><b>Korisnik :</b></label></span>
<br />
<input type="text" name="korisnik" id="korisnik" size="40" height="25" />
<br />
<br />
<span><label><b>Lozinka :</b></label></span>
<br />
<input type="password" name="lozinka" id="lozinka" size="40" height="25" />
<br />
<br />
<button type="submit" title="Login" class="btnLogin"></button>
</form>
Be sure to check out the official ASP.NET Core documentation on how to work with forms.
If you pass username and password using asp-route-{value}, it will show these information in the url which is not recommended.
If you just want to pass the two input values from view to controller, you need to modify name attribute of input to match the action parameters.For example:
View:
<form method="post" asp-controller="Members" asp-action="LoginUser">
<span><label><b>Korisnik :</b></label></span>
<br />
<input type="text" name="username" id="korisnik" size="40" height="25" />
<br />
<br />
<span><label><b>Lozinka :</b></label></span>
<br />
<input type="password" name="password" id="lozinka" size="40" height="25" />
<br />
<br />
<button type="submit" title="Login" class="btnLogin"></button>
</form>
Action:
[HttpPost]
public async Task<IActionResult> LoginUser(string username,string password)

On CodeIgniter, how to repopulate a form only with correct values?

Imagine a form like this:
<form action="test.php" method="post">
<input type="text" name="firstname" value="<?=set_value('firstname')?>" />
<input type="text" name="lastname" value="<?=set_value('lastname')?>" />
<input type="text" name="email" value="<?=set_value('email')?>" />
<input type="submit" name="submit_form" value="OK" />
</form>
If I submit an incorrect email, the CodeIgniter function will write "the field is not valid" and populate the invalid field with the wrong value.
I would like to keep the error message but not the wrong value (I prefer having an empty value). But I also want to keep the re-populating function for correct values.
Here is what I get:
Here is what I want:
[EDIT] SOLUTION FOUND (thanks to Herr Kaleun and Matt Moore)
Example with the email field:
<input type="text" name="email" id="email" value="<?=!form_error('email')?set_value('email'):''?>" />
You can check the fields individually and have a logic like this.
If the error for a specific field is present, you can build a logic on top of that.
if ( form_error('email') != '' )
{
$data['email_value'] = '';
}
else
{
$data['email_value'] = set_value('email');
}
<form action="test.php" method="post">
<input type="text" name="firstname" value="<?=set_value('firstname')?>" />
<input type="text" name="lastname" value="<?=set_value('lastname')?>" />
<input type="text" name="email" value="<?= $email_value; ?>" />
<input type="submit" name="submit_form" value="OK" />
</form>
The Form_error is set when a form value errors out based on the validation. Judging by your use of the set_value function I assume you're using the built in form validation. You can check to see if the message for that field is set if(form_error('fieldname') !=NULL) and set the value based on that.
You may have to setup error messages for each field, which you should be doing anyway. Here is the guide that covers it: http://codeigniter.com/user_guide/libraries/form_validation.html#repopulatingform
<form action="test.php" method="post">
<input type="text" name="firstname" value="<?if(form_error('firstname') != NULL){echo set_value('firstname');}?>" />
<input type="text" name="lastname" value="<?if(form_error('lastname') != NULL){ echo set_value('lastname');}?>" />
<input type="text" name="email" value="<?if(form_error('email') !=NULL){ echo set_value('email');}?>" />
<input type="submit" name="submit_form" value="OK" />
</form>
*NOTE THIS CODE HAS NOT BEEN TESTED *

Select input value from two different forms in one page using mootools

I have two forms with different values in one page.
I want get input value of the form that user clicks on it's submit button, using mootools code.
Mootools code most work for two forms, as same:
<form class="cart_form" >
<input type="hidden" name="order_code" value="KWL-hgy" />
<input type="hidden" name="price" value="40000" />
<input type="hidden" name="name" value="modeling" />
<label>KWL-hgy: <input class="center" type="text" name="qty" value="2" size="3" ></label>
<input type="submit" name="submit" value="Add to cart" />
</form>
<form class="cart_form" >
<input type="hidden" name="order_code" value="KWL-JFE" />
<input type="hidden" name="price" value="12000" />
<input type="hidden" name="name" value="php" />
<label>KWL-JFE: <input class="center" type="text" name="qty" value="1" size="3" ></label>
<input type="submit" name="submit" value="Add to cart" />
</form>
you could do it somehow like this:
$$('form.cart_form').addEvent('submit', function(e){
this.getChildren().each(function(el){
alert(el.get('name') + ': ' + el.get('value'));
});
});
this will fire an event as soon as a form with the class chart_form is submited and loop through all the form's children.