thymeleaf: th:value is ignored when using th:field - thymeleaf

I've got a form where I want to edit some user data.
So the already stored data is put as th:value and after sending I validate with spring validation and want to give back the form on wrong input. I want the input field to have the value of the input by the user but it always gives me the stored input instead.
That's how an input field looks like
<input type="text" th:value="${product.name}" th:field="*{name}" th:errorclass="fieldError"/>
If the form is loaded the first time the input fields should have the value of the already stored data.
If it's loaded after submit and with an validation error, the input fields should have the value of the user's input.
Is there a way to to that?
Thanks!

Attribute th:field will replace attributes value, id and name in your input tag.
Instead, use plain th:id, th:value and th:name without using th:field. Then you will get what you wanted.
Then it will look like:
<input type="text" th:value="${product.name}" th:name="name" th:id="name" th:errorclass="fieldError"/>
Similar answer is here: How to set thymeleaf th:field value from other variable

Because Attribute th:field is Combination of both th:name and th:value
So, either use this:
<input type="text" th:value="${product.name}" th:name="name" th:errorclass="fieldError"/>
Or this:
<input type="text" th:field="*{name}" "th:errorclass="fieldError"/>

Using th:field or value, id and name is ok. if you user th:field, you can write this:
<input type="text" th:field="${product.name}" th:value="${product.name}" "th:errorclass="fieldError"/>

You have error in your controller (you set wrong value to *{name}), if the input value is wrong after validation error.

Related

adding an id attribute to q-input

Say I have the following q-input:
<q-input
v-model="form.email"
inverted-light
color="white"
stack-label="Email:"
type="email"
#blur="$v.form.email.$touch"
:error="$v.form.email.$error"/>
I'd like to be able to make it so that if the domain of the email is mydomain.com that the form action will change to another website (without csrf protection) and the POST will be made to that website instead of the main one.
To do this I was thinking I could use jQuery. eg. $('#email').val().replace(/^.+#/, '') == 'mydomain.com' then change the form action and submit.
The only problem is: I don't know how to set an id attribute on q-input.
Any ideas?
As of early Quasar 1.4.2 (November of this year) you can specify the id value on the resulting html generated by q-input by using the "for" property (see the end of the behavior properties: https://quasar.dev/vue-components/input#QInput-API).
So, for example, you could add for="myInputId":
<q-input
v-model="form.email"
inverted-light
color="white"
stack-label="Email:"
type="email"
#blur="$v.form.email.$touch"
:error="$v.form.email.$error"
for="myInputId"
/>
The id attribute with value "myInputField" will end up on the resulting <input> element in your HTML.
Not using the "for" in the elements gave me a lot of headaches because the Jest snapshot generated random IDs

Browser ignores query string

I have a hardware device with an admin console accessed via a web interface. I want to pass a query string to the URL so that the username and password fields are pre-populated. I am doing so as follows but the browser ignores the query string:
http://192.168.5.50?username=abc?password=def
I have checked the page source and the username and password input fields are called "username" and "password".
EDIT:
I see that I have incorrectly used the character ? instead of & to separate the key/value pairs. Correcting this as follows does not change the outcome. The query string is still ignored.
http://192.168.5.50?username=abc&password=def
There is a form in the HTML with this definition:
<form name="myform" method="post" action="read" autocomplete="off">
Is the POST method incompatible with query strings? If so, is there another method of auto-populating fields?
You can try to create a form dynamically and submit it. You url should be something like this:
javascript:document.write('<form name="myform" method="post" action="read" autocomplete="off"><<input type="hidden" name="username" value="abc"><input type="hidden" name="password" value="def"></form><scr'+'ipt type="text/javascript">document.getElementsByTagName("form")[0].submit();</scr'+'ipt>');

What is the equivalent of HTML'S input tag in Rails?

I want to be able to generate this
<input id="address" type="textarea" value="Write your Address here.">
Using some ruby form helpers.
Any hints ?
I assume you meant textarea since there is no such thing as textbox.
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/text_area_tag
if it's not tied to a field in your model
http://apidock.com/rails/ActionView/Helpers/FormHelper/text_area
if it is.

MVC #Html.Display

I have something like:
<input type="text" name="TerrMng" id="TerrMng"/>
in HTML. What is the equivalent of the above using #Html.Display?
I tried using:
#Html.Display("TerrMng", TerrMng)
but was not successful. Note that I like to use #Html.Display but not sure how to translate the ID value so that it shows up.
This should do the trick for you. Adding the TerrMng as a 2nd parameter sets the value of the created html but the variable must come from your Model on load.
#Html.Display("TerrMng");

Html.Hidden builds wrong value data in MVC 2 app

I am using an id value that I pass in a hidden field. When the user submits the form I need the hidden field for my update. After the update, a new value is placed in the hidden field in the model and sent back to the view. What seems so strange is the helper always uses the first value, never updates. For example, look at the following from the View:
<%: Html.Hidden("MyId",Model.MyId) %>
<%: Model.MyId %>
First time in a look at the source in the browser yields:
<input type="hidden" id="MyId" name="MyId" value="1" />
1
** submit back to controller and model updates the MyId property to 2.
Back at the browser I now find:
<input type="hidden" id="MyId" name="MyId" value="1" />
2
The very same model property has different values! The helper method is somehow grabbing it from a prior model instance or something?
Any help greatly appreciated on what I am not understanding. BTW..get the same behavior with Html.TextBox and Html.TextBoxFor.
Thanks.
That's how HTML helpers work and it's by design. When binding they will first look at the value in the GET/POST request to see if the value is present and after that in the model. If a value is found in the request they will simply ignore the value you set in the model.
Normally you are not supposed to modify the data sent in the request inside your controller action. But if anyhow you decide to do it you will need to either roll your own helper or simply:
<input type="hidden" name="MyId" value="<%= Model.MyId %>" />

Resources