Thymeleaf: Conditional th:value - thymeleaf

Problem:
${map} can be null.
<input type="text" th:value="${map.name}" />
What I need:
If name is not null then th:value=name otherwise th:value=""
<input type="text" th:value="${map.name != null ? map.name : ''}" />
But my above code is not valid

Solution:
<input th:value="${map !=null}? ${map.name} : ''" />
Or Better (with Elvis Operator):
<input type="text" th:value="${map?.name}"

Related

Missing string after space erb

I have used html.erb template in Rails to passing some value for HTML input and my source code looks like this:
<% all = "All value" %>
<input type="text" name="name" id="name" value=<%= all %>>
But when the view is rendered, it seems like a string after space was missing and I just got:
<input type="text" name="name" id="name" value="All">
But my expection is:
<input type="text" name="name" id="name" value="All value">
I appreciate any help!
Quote your all variable like this:
<input type="text" name="name" id="name" value="<%= all %>">

Get value with $_POST from multiple brackets

<input type="text" class="text" id="attachments-1-vid_id" name="attachments[1][vid_id]" value="1qaq0F-SXpo8">
<input type="text" class="text" id="attachments-2-vid_id" name="attachments[2][vid_id]" value="2qaq0F-SXpo8">
<input type="text" class="text" id="attachments-3-vid_id" name="attachments[3][vid_id]" value="3qaq0F-SXpo8">
<input type="text" class="text" id="attachments-4-vid_id" name="attachments[4][vid_id]" value="4qaq0F-SXpo8">
This is my input. I want to check it with only [vid_id]. Is it possible?
If any input posted
if( isset($_POST['vid_id']) ) {
//do it...
}
You are accessing the vid_id index which isn’t really what is passed in the $_POST superglobal.
The solution is based on two assumptions.
Only that input exist.
This is pretty straight forward:
<?php
if(isset($_POST[‘attachments’][231][‘vid_id’]){}
OR
<?php
if(array_key_exists(“vid_id”, $_POST[‘attachments’][231])){}
Similar inputs also exist and you want to perform for multiple inputs.
By this, I mean..
<input type=text name=attachments[][vid_id] />
.....
<input type=text name=attachments[][vid_id] />
<input type=text name=attachments[][vid_id] />
For this, you have to insert it in a loop.
<?php
foreach($_POST[‘attachments’] as $arr){
if(array_key_exists(“vid_id”, $arr){
// do stuff
}
}

Prevent user to type in a textbox [duplicate]

what is the code to disable an INPUT text box for HTML?
Thanks
<input type="text" disabled="disabled" />
See the W3C HTML Specification on the input tag for more information.
<input type="text" required="true" value="" readonly="true">
This will make a text box in readonly mode, might be helpful in generating passwords and datepickers.
The syntax to disable an HTML input is as follows:
<input type="text" id="input_id" DISABLED />
You can Use both disabled or readonly attribute of input . Using disable attribute will omit that value at form submit, so if you want that values at submit event make them readonly instead of disable.
<input type="text" readonly>
or
<input type="text" disabled>
<input type="text" required="true" value="" readonly>
Not the.
<input type="text" required="true" value="" readonly="true">

How parse checkbox values in server side(rails)?

I am beginner in ruby + rails.
I have problem to get values in controler of checkbox with params[] of rails.
The Case:
I have simple form that that have checkbox and i want to sent reqular request to controller action with user checked values.
The problem that params[:rating] can have few values.
My case:
html user side:
<form id="ratings-form">
<input name="rating" type="checkbox" value="G">G
<input name="rating" type="checkbox" value="PG">PG
<input name="rating" type="checkbox" value="PG-13">PG-13
<input name="rating" type="checkbox" value="R">R
<input type="submit" value="refresh">
</form>
controller action code to parse checked values: (get error 1. params[:rating] == nil or params[:rating] == string)
params[:rating].each do |rat|
p rat;
end
What should i change in the code to make it work?
Thanks
Try this HTML
<input name="rating[]" type="checkbox" value="G">G
<input name="rating[]" type="checkbox" value="PG">PG
<input name="rating[]" type="checkbox" value="PG-13">PG-13
<input name="rating[]" type="checkbox" value="R">R
Then you should have an array in params[:rating].

MVC Razor String Concat

I am using Razor to generate a form. I want to create HTML elements based on some value from it's model property.
for example, if a model contains Id property, and I want to generate html tags as follows
<input type="hidden" name="1_chk" />
<input type="hidden" name="2_chk" />
<input type="hidden" name="3_chk" />
So I used the following syntax, and it failed. Can anyone help me out with this?
<input type="checkbox" name="#Id_chk" />
Thanks
I think this should work for you:
<input type="checkbox" name="#(Id)_chk" />
another option:
<input type="checkbox" name="#(Id + "_chk")" />

Resources