Html, thymleaf radiobutton th:field="" fails if data is null - html

I´d like to autofill receipt data. If my receipt is not null it should pick the right gender to a radio button with thymeleaf. There are no errors if the receipt is not null, but if receipt is null the errors are thrown
<div class="form-group">
<div id="genderReceipt">
<label for="genderMaleReceipt" class="radio-inline">
<input type="radio" name="genderReceipt" id="genderMaleReceipt" value="MALE" th:field="*{receipt.gender}"/>
Herr
</label>
<label for="genderFemaleReceipt" class="radio-inline">
<input type="radio" name="genderReceipt" id="genderFemaleReceipt" value="FEMALE" th:field="*{receipt.gender}"/>
Frau
</label>
</div>
<label for="genderReceipt" class="error" style="display: none;"> Bitte gib eine Anrede an.</label>
</div>
The result should be like: if receipt == null both are unchecked... if receipt != null the right gender is checked
error log says:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'receipt' available as request attribute

My Solution for this problem is to duplicate the code and insert th:if statements like here in the example
<div th:if="${receipt} == null">
<label for="genderFemaleReceipt" class="radio-inline">
<input type="radio" name="genderReceipt" id="genderMaleReceipt" value="MALE" />
Herr
</label>
<label for="genderFemaleReceipt" class="radio-inline">
<input type="radio" name="genderReceipt" id="genderFemaleReceipt" value="FEMALE" />
Frau
</label>
</div>
<div th:if="${receipt} != null">
<label for="genderFemaleReceipt" class="radio-inline">
<input type="radio" name="genderReceipt" id="genderMaleReceipt" value="MALE" th:field="*{receipt.gender}"/>
Herr
</label>
<label for="genderFemaleReceipt" class="radio-inline">
<input type="radio" name="genderReceipt" id="genderFemaleReceipt" value="FEMALE" th:field="*{receipt.gender}"/>
Frau
</label>
</div>
I know thats not best practice ;( but i didn´t know a other way to do it.

Related

Woocommerce bookings

I'm trying to create custom steps on checkout page with Woo bookings plugin.
So I have 3 steps.
create booking product with parameters like duration, start date, end date.
Extra services
Payment
My problem is that I need to submit form details, in other words add this booking product to cart, but without submitting order.
I'm using this structure with hardcoded values, but whem I'm submitting this it acts like place order button, not like add to cart button. How do I just add to cart?
<div class="form-group">
<input type="radio" id="Terminal1withcancelation" class="form-check-input" name="add-to-cart" value="13"><label for="Terminal1withcancelation">terminal 1</label>
<input type="radio" id="Terminal1nocancelation" class="form-check-input" name="add-to-cart" value="13"><label for="Terminal1nocancelation">terminal 1</label>
<input type="radio" id="Terminal2withcancelation" class="form-check-input" name="add-to-cart" value="13"><label for="Terminal2withcancelation">terminal 2</label>
<input type="radio" id="Terminal2nocancelation" class="form-check-input" name="add-to-cart" value="13"><label for="Terminal2nocancelation">terminal 2</label>
<input type="hidden" name="wc_bookings_field_duration" value="4">
<input type="hidden" name="wc_bookings_field_start_date_month" value="07">
<input type="hidden" name="wc_bookings_field_start_date_day" value="23">
<input type="hidden" name="wc_bookings_field_start_date_year" value="22">
<input type="hidden" name="wc_bookings_field_start_date_to_month" value="07">
<input type="hidden" name="wc_bookings_field_start_date_to_day" value="26">
<input type="hidden" name="wc_bookings_field_start_date_to_year" value="2022">
</div>
<button type="submit" name="btn1" value="btn1" class="buy custom_add_btn" id="nextBtn" onclick="nextPrev(1)">NEXT</button>

Radio button text click result wrong answer [duplicate]

This question already has an answer here:
2 sets of radio buttons with same IDs
(1 answer)
Closed 10 months ago.
When I click the text of "prefer not to say" of the second and third questions, the first question's answer changes to "prefer not to say". When I click the text of 'other' of the third question, the second question's answer changes to 'other'. In both cases, the third question's 'other' and 'prefer not to say' do not check when I click text. What is wrong with this code?
It runs well when I click the radio button.
<div>
<p><strong>1. What is your age?</strong></p>
<input type="radio" id="Under18" name="h11" value="Under18">
<label for="Under18">Under 18</label><br>
<input type="radio" id="18-25" name="h11" value="18-25">
<label for="18-25">18 - 25</label><br>
<input type="radio" id="26-35" name="h11" value="26-35">
<label for="26-35">26 - 35</label><br>
<input type="radio" id="46-55" name="h11" value="46-55">
<label for="46-55">46 - 55</label><br>
<input type="radio" id="Over55" name="h11" value="Over55">
<label for="Over55">Over 55</label><br>
<input type="radio" id="Prefer not to say" name="h11" value="Prefer not to say">
<label for="Prefer not to say">Prefer not to say</label>
</div>
<div>
<p><strong>2. What is your gender?</strong></p>
<input type="radio" id="Female" name="h12" value="Female">
<label for="Female">Female</label><br>
<input type="radio" id="Male" name="h12" value="Male">
<label for="Male">Male</label><br>
<input type="radio" id="Other" name="h12" value="Other">
<label for="Other">Other</label><br>
<input type="radio" id="Prefer not to say" name="h12" value="Prefer not to say">
<label for="Prefer not to say">Prefer not to say</label>
</div>
<div>
<p><strong>3. What is your ethnicity?</strong></p>
<input type="radio" id="White/Caucasian" name="h13" value="White/Caucasian">
<label for="White/Caucasian">White/Caucasian</label><br>
<input type="radio" id="Hispanic/Latino" name="h13" value="Hispanic/Latino">
<label for="Hispanic/Latino">Hispanic/Latino</label><br>
<input type="radio" id="Black/African American" name="h13" value="Black/African American">
<label for="Black/African American">Black/African American</label><br>
<input type="radio" id="Asian/Pacific Islander" name="h13" value="Asian/Pacific Islander">
<label for="Asian/Pacific Islander">Asian/Pacific Islander</label><br>
<input type="radio" id="Native American/American Indian" name="h13" value="Native American/American Indian">
<label for="Native American/American Indian">Native American/American Indian</label><br>
<input type="radio" id="Other" name="h13" value="Other">
<label for="Other">Other</label><br>
<input type="radio" id="Prefer not to say" name="h13" value="Prefer not to say">
<label for="Prefer not to say">Prefer not to say</label>
</div>
This is because your radio inputs have the same ID.
Every element needs to have unique ID, so just change some of them and you should be fine. Don't forget to update your for attributes aswell.
IDs are global in the DOM.
Your label is targeting the control with the id "Prefer not to say", so the first control with the ID that matches the for attribute in the label is activated.
Add a 1, 2, 3, etc... to the ids and for attri

For loop - bind dynamic key to the #id

I have a for loop that displays the data into multiple radio-buttons (bootstrap):
<div class="radio" >
<label v-for="(choice, index) in choices" v-if="choice.question_id == question.id" :key="choice.id">
<input type="radio" name="optradio" id="choice.id"> [[choice.content]]
</label>
</div>
As you can see, I wanted to use the choice.id for id="..." in each button, technically it should look something like this:
<input type="radio" name="optradio" id="1"> Choice1
<input type="radio" name="optradio" id="2"> Choice2
<input type="radio" name="optradio" id="3"> Choice3
But it renders it with the actual string choice.id:
<input type="radio" name="optradio" id="choice.id"> Choice1
<input type="radio" name="optradio" id="choice.id"> Choice2
<input type="radio" name="optradio" id="choice.id"> Choice3
Forgive my naiveness. Any help/advices? Thanks a lot!
It renders with choice-id string because you add plain string as the id value, not a variable value
You can use v-bind directive or the shorthand for v-bind -> :id
<div class="radio" >
<label v-for="(choice, index) in choices" v-if="choice.question_id == question.id" :key="choice.id">
<input type="radio" name="optradio" v-bind:id="choice.id"> [[choice.content]]
</label>
</div>
using shorthand <input type="radio" name="optradio" :id="choice.id">
To answer your questions in the comments.
You can ' separate 'the radios by adding them in a ' group ' using the name attribute. Radios with the same name attribute are in one group. Changing their values won't affect other radios in other groups ( with different name attributes ). See example below.
Or you can use vue v-model to separate and get the selected options.
new Vue({
el: "#radio-app",
data: {
question1: '',
question2: ''
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://github.com/vuejs/vue-devtools"></script>
Question1:
<input type="radio" name="question1" id="q1choice1" value="choice1"/>
<input type="radio" name="question1" id="q1choice2" value="choice2"/>
Question2:
<input type="radio" name="question2" id="q2choice1" value="choice1"/>
<input type="radio" name="question2" id="q2choice2" value="choice2"/>
<hr />
<h2> Or with VUE v-model </h2>
<div id="radio-app">
Question1:
<input type="radio" id="q1choice1vue" value="choice1" v-model="question1">
<input type="radio" id="q1choice2vue" value="choice2" v-model="question1">
Question2:
<input type="radio" id="q2choice1vue" value="choice1" v-model="question2">
<input type="radio" id="q2choice2vue" value="choice2" v-model="question2">
<div>Question 1 selected answer: {{ question1 }}</div>
<div>Question 2 selected answer: {{ question2 }}</div>
</div>
Check more here
VUE template syntax
v-bind directive
Use: v-bind:id="choice.id" or the short version :id="choice.id"

selected option is not hitting to database using magento

<label class="radio-inline">
<input type="radio" name="optradio" value="1" <?php echo($data["optradio"]== 1 ?'checked="checked"':''); ?> />
Male </label>
<label class="radio-inline">
<input type="radio" name="optradio" value="2" <?php echo($data["optradio"]== 2 ? 'checked="checked"':''); ?> />
Female </label>
I want to store the selected value of gender in database. The code i have written as shown above.
Please guide me where i am going wrong.

Payment options with radio buttons in the contact form

My form is currently set up to gather all the input data to my autoresponder...however, I made the form with only one option - pay now. Users would like options, so Im thinking of giving them 2 choices, the old "pay now" COD method, and option#2 paypal. I think radio buttons are the best way for doing this. However I cant get them to work separately...when I choose option 2, option 1 remains selected. So I added the radio buttons myself after the ordernow button.
<p>mail: *</p>
<p>
<label>
<input type="text" class="wf-input wf-req wf-valid__email" name="mail" class="mj" ></input>
</label>
</p>
<p>name: *</p>
<p>
<label>
<input type="text" class="wf-input wf-req wf-valid__required" name="name" class="mj" ></input>
</label>
</p>
<p>
<input type="submit" value="ORDER NOW" class="butt">
<div class="selectpaymentradios">
<label class="radio" >select payment</label>
<input class="radio" type="radio" name="cash" value="cash" checked /> <span>Ca$h</span>
<input class="radio" type="radio" name="ppal" value="ppal" /> <span>PaypaL</span>
</div>
<input type="hidden" name="webform_id" value="12x45"/>
</p>
</form>
<script type="text/javascript" src="http://xyz.com/view_webform.js?wid=12x45&mg_param1=1"></script>
Im trying to figure out how can I make this work with my autoresponder, I think this form has to be able to tell me what kind of payment did the customer chose...but the autoresponders form creator doesnt have radio buttons at all so Im stuck, I dont know if its possible...
<input class="radio" type="radio" name="cash" value="cash" checked /> <span>Ca$h</span>
<input class="radio" type="radio" name="ppal" value="ppal" /> <span>PaypaL</span>
the problem you hit, is very simple - you have to use the same name for all radio-buttons, where only one item should be selected. like this:
<input class="radio" type="radio" name="payment" value="cash" checked /> <span>Ca$h</span>
<input class="radio" type="radio" name="payment" value="ppal" /> <span>PaypaL</span>
The name attribute should be the same for both radio buttons:
<input class="radio" type="radio" name="method" value="cash" checked="checked" /> <span>Ca$h</span>
<input class="radio" type="radio" name="method" value="ppal" /> <span>PaypaL</span>
Also, if you are closing input tags, you are probably worried about XHTML validation. So instead of just checked you should type checked="checked".