Laravel Form::select generation in blades template - laravel

This line of code:
{!! Form::select('fightingAreas', [1,2,4,8], old('fightingAreas'),['class' => 'form-control']) !!}
Generates :
<select class="form-control" id="fightingAreas" name="fightingAreas">
<option value="0" selected="selected">1</option>
<option value="1">2</option>
<option value="2">4</option>
<option value="3">8</option>
</select>
I would like to generate a value that is equals to text:
<select class="form-control" id="fightingAreas" name="fightingAreas">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
</select>
Is it posible to do it with Form::select()???

Its actually easier than you think
{!! Form::select('fightingAreas',
['1'=>1,'2'=>2,'4'=>4,'8'=>8],
null,['class' => 'form-control'])
!!}
Also using null will automatically select the old value no need to set old('fightingAreas')
For more info have a look at
http://laravel-recipes.com/categories/21
https://laravelcollective.com/docs/5.2/html
Thanks

Related

topic.blade.php cannot fetch value->id, but it works with value->name

I got the code snippet,
<div class="form-group">
<label for="category-id-field">Category</label>
<select class="form-control" name="category_id" id="category-id-field">
<option value="" hidden disabled {{ $topic->id ? '' : 'selected' }}>Choose your category</option>
#foreach ($categories as $value)
#php
$selected = $topic->category_id === $value->id ? 'selected' : '';
#endphp
<option value="{{ $value->id }}" {{ $selected }}>{{ $value->name }}</option>
#endforeach
</select>
</div>
And it shows like below, as you can see $value->name works but $value->id just shows up zero.
<div class="form-group"><label for="category-id-field">Category</label>
<select name="category_id" id="category-id-field" class="form-control">
<option value="" selected="selected">Choose your category</option>
<option value="0">statement</option>
<option value="0">life</option>
<option value="0">work</option>
<option value="0">study</option>
</select>
</div>
Versions:
laravel/framework: "^7.0"
Platform: macos catalina
mysql: 8.0.19
Some more background:
Things I did, the id in the categories table used to be integer, I changed it to VARCHAR(30) and added it as the foreign key to table topics->category_id.
If anyone happens to have some experience on this one, please point out the error. Many thanks.

Predefined redundant values in view Laravel

I have here a simple modal that edits user privileges. I want to show the current value of the selected user first but it seems my code is redundant in nature as it repeats. These values are not in a separate table by the way. Is there any simpler way to do this in this view?
<div class="form-group col-md-4">
<label for="user_type">Privilege</label>
<select name="user_type" class="form-control" id="">
<?php $user_type = $user->user_type ?>
#if ($user_type == 'Admin')
<option value="Admin" selected>Admin</option>
<option value="Facilitator">Facilitator</option>
<option value="TeamLeader">TeamLeader</option>
<option value="ScrumLeader">ScrumLeader</option>
<option value="Member">Member</option>
#elseif ($user_type == 'Facilitator')
<option value="Admin">Admin</option>
<option value="Facilitator" selected>Facilitator</option>
<option value="TeamLeader">TeamLeader</option>
<option value="ScrumLeader">ScrumLeader</option>
<option value="Member">Member</option>
#elseif ($user_type == 'TeamLeader')
<option value="Admin">Admi n</option>
<option value="Facilitator">Facilitator</option>
<option value="TeamLeader" selected>TeamLeader</option>
<option value="ScrumLeader">ScrumLeader</option>
<option value="Member">Member</option>
#elseif ($user_type == 'ScrumLeader')
<option value="Admin">Admin</option>
<option value="Facilitator">Facilitator</option>
<option value="TeamLeader">TeamLeader</option>
<option value="ScrumLeader" selected>ScrumLeader</option>
<option value="Member">Member</option>
#else
<option value="Admin">Admin</option>
<option value="Facilitator">Facilitator</option>
<option value="TeamLeader">TeamLeader</option>
<option value="ScrumLeader">ScrumLeader</option>
<option value="Member" selected>Member</option>
#endif
</select>
</div>
I would suggest using the laravelcollective/html package (here)
You will then be able to write something like:
{{Form::select('user_type', $usertypearray, $user->user_type, ['class' => 'form-control']) }}
Where $usertypearray is a value => label array. For instance:
$usertypearray = ['admin'=> 'Admin', 'facilitator'=> 'Facilitator', 'teamleader'=> 'TeamLeader', 'scrumleader'=> 'ScrumLeader','member'=>'Member'];
(I put values in lowercase, but check if it's what the server is looking for)
You can looping on select tag like this
{{ Form::select('user_type',$user_type ,old('user_type'),['class' => 'form-control']) }}
Hope this helps :)

Track file upload using vue.js

Currently building a web app that allows user to upload videos, now i have a form that post all user details and video using laravel, now i want to keep track of the file upload using a progress bar, in my finding....i discover vue.js can handle this.
This is my UploadController.php
function generateRandomString($length = 10) {
return strtoupper(substr(str_shuffle("123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"), 0, $length));
}
public function store(Request $request){
$validatedData = $request->validate([
'name'=>'required | unique:uploads',
'email'=>'required | email | unique:uploads',
'phone'=>'required',
'country'=>'required',
'title'=>'required | unique:uploads',
'agency'=>'required',
'category'=>'required',
'video'=>'required | file | mimetypes:video/mp4,video/webm | max:40960'
],[
'name.required' => 'Name is required',
'ref.required' => 'Reference is required',
]);
// Unique ID
$ref = $this->generateRandomString();
$email = $request->email;
// Get Filename with extention
$filenameWithExtension = $request->file('video')->getClientOriginalName();
//Get just the filename
$filename = pathinfo($filenameWithExtension, PATHINFO_FILENAME);
//Get just the extention
$extension = $request->file('video')->getClientOriginalExtension();
//Get new filename
$filenameToStore = $ref.'.'.$extension;
// Upload Video
$path = $request->file('video')->storeAs('public/videos', $filenameToStore);
//Create new registration
$upload = new Upload;
$upload->name = $request->input('name');
$upload->email = $request->input('email');
$upload->phone = $request->input('phone');
$upload->country = $request->input('country');
$upload->title = $request->input('title');
$upload->agency = $request->input('agency');
$upload->ref = $ref;
$upload->category = $request->input('category');
$upload->video = $filenameToStore;
$upload->save();
Mail::send('emails.welcome',[
'ref' => $ref,
'name' => $request->name,
'title'=> $request->title,
'category'=> $request->category,
'agency'=> $request->agency,
'phone'=> $request->phone,
'country'=> $request->country,
], function ($mail) use($request){
$mail->from('no-reply#percussionfed.org', 'International Percussion Federation');
$mail->to($request->email)->subject('International Percussion Federation');
});
return redirect('/register')->with('success', 'Registration Successful, Check'." ".$email." ".'for details.' );
}
View.blade.php
<form method="POST" action="/store" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Name</label>
<sup class="required" title="Required">*</sup>
<input type="text" class="file" title="Name" value="{{old('name')}}" name="name" >
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Email</label>
<sup class="required" title="Required">*</sup>
<input type="text" class="file" title="Email" value="{{old('email')}}" name="email" >
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Phone</label>
<sup class="required" title="Required">*</sup>
<input type="number" class="file" title="Phone" value="{{old('phone')}}" name="phone" >
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Country</label>
<sup class="required" title="Required">*</sup>
<select class="file" title="Country" id="country" value="{{old('country')}}" name="country" >
<option value="">Select your country</option>
<option value="United States">United States</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="Andorra">Andorra</option>
<option value="Angola">Angola</option>
<option value="Anguilla">Anguilla</option>
<option value="Antarctica">Antarctica</option>
<option value="Antigua and Barbuda">Antigua and Barbuda</option>
<option value="Argentina">Argentina</option>
<option value="Armenia">Armenia</option>
<option value="Aruba">Aruba</option>
<option value="Australia">Australia</option>
<option value="Austria">Austria</option>
<option value="Azerbaidjan">Azerbaidjan</option>
<option value="Bahamas">Bahamas</option>
<option value="Bahrain">Bahrain</option>
<option value="Bangladesh">Bangladesh</option>
<option value="Barbados">Barbados</option>
<option value="Belarus">Belarus</option>
<option value="Belgium">Belgium</option>
<option value="Belize">Belize</option>
<option value="Benin">Benin</option>
<option value="Bermuda">Bermuda</option>
<option value="Bhutan">Bhutan</option>
<option value="Bolivia">Bolivia</option>
<option value="Bosnia-Herzegovina">Bosnia-Herzegovina</option>
<option value="Botswana">Botswana</option>
<option value="Bouvet Island">Bouvet Island</option>
<option value="Brazil">Brazil</option>
<option value="British Indian Ocean Territory">British Indian Ocean Territory</option>
<option value="Brunei Darussalam">Brunei Darussalam</option>
<option value="Bulgaria">Bulgaria</option>
<option value="Burkina Faso">Burkina Faso</option>
<option value="Burundi">Burundi</option>
<option value="Cambodia">Cambodia</option>
<option value="Cameroon">Cameroon</option>
<option value="Canada">Canada</option>
<option value="Cape Verde">Cape Verde</option>
<option value="Cayman Islands">Cayman Islands</option>
<option value="Central African Republic">Central African Republic</option>
<option value="Chad">Chad</option>
<option value="Chile">Chile</option>
<option value="China">China</option>
<option value="Christmas Island">Christmas Island</option>
<option value="Cocos Islands">Cocos Islands</option>
<option value="Colombia">Colombia</option>
<option value="Comoros">Comoros</option>
<option value="Congo">Congo</option>
<option value="Congo, Democratic Republic">Congo, Democratic Republic</option>
<option value="Cook Islands">Cook Islands</option>
<option value="Costa Rica">Costa Rica</option>
<option value="Cote D'Ivoire">Cote D'Ivoire</option>
<option value="Croatia">Croatia</option>
<option value="Cuba">Cuba</option>
<option value="Curacao">Curacao</option>
<option value="Cyprus">Cyprus</option>
<option value="Czech Republic">Czech Republic</option>
<option value="Denmark">Denmark</option>
<option value="Djibuti">Djibuti</option>
<option value="Dominica">Dominica</option>
<option value="Dominican Republic">Dominican Republic</option>
<option value="Ecuador">Ecuador</option>
<option value="Egypt">Egypt</option>
<option value="El Salvador">El Salvador</option>
<option value="Equatorial Guinea">Equatorial Guinea</option>
<option value="Eritrea">Eritrea</option>
<option value="Estonia">Estonia</option>
<option value="Ethiopia">Ethiopia</option>
<option value="Falkland Islands">Falkland Islands</option>
<option value="Faroe Islands">Faroe Islands</option>
<option value="Fiji">Fiji</option>
<option value="Finland">Finland</option>
<option value="France">France</option>
<option value="French Guiana">French Guiana</option>
<option value="French Polynesia">French Polynesia</option>
<option value="French Southern Territories">French Southern Territories</option>
<option value="Gabon">Gabon</option>
<option value="Gambia">Gambia</option>
<option value="Georgia">Georgia</option>
<option value="Germany">Germany</option>
<option value="Ghana">Ghana</option>
<option value="Gibraltar">Gibraltar</option>
<option value="Greece">Greece</option>
<option value="Greenland">Greenland</option>
<option value="Grenada">Grenada</option>
<option value="Guadeloupe">Guadeloupe</option>
<option value="Guatemala">Guatemala</option>
<option value="Guinea">Guinea</option>
<option value="Guinea Bissau">Guinea Bissau</option>
<option value="Guyana">Guyana</option>
<option value="Haiti">Haiti</option>
<option value="Heard and MC Donald Islands">Heard and MC Donald Islands</option>
<option value="Honduras">Honduras</option>
<option value="Hong Kong">Hong Kong</option>
<option value="Hungary">Hungary</option>
<option value="Iceland">Iceland</option>
<option value="India">India</option>
<option value="Indonesia">Indonesia</option>
<option value="Iran">Iran</option>
<option value="Iraq">Iraq</option>
<option value="Ireland">Ireland</option>
<option value="Israel">Israel</option>
<option value="Italy">Italy</option>
<option value="Jamaica">Jamaica</option>
<option value="Japan">Japan</option>
<option value="Jordan">Jordan</option>
<option value="Kazakhstan">Kazakhstan</option>
<option value="Kenya">Kenya</option>
<option value="Kiribati">Kiribati</option>
<option value="Korea, Democratic People's Rep">Korea, Democratic People's Rep</option>
<option value="Korea, Republic of">Korea, Republic of</option>
<option value="Kosovo">Kosovo</option>
<option value="Kuwait">Kuwait</option>
<option value="Kyrgyzstan">Kyrgyzstan</option>
<option value="Laos">Laos</option>
<option value="Latvia">Latvia</option>
<option value="Lebanon">Lebanon</option>
<option value="Lesotho">Lesotho</option>
<option value="Liberia">Liberia</option>
<option value="Libya">Libya</option>
<option value="Liechtenstein">Liechtenstein</option>
<option value="Lithuania">Lithuania</option>
<option value="Luxembourg">Luxembourg</option>
<option value="Macau">Macau</option>
<option value="Macedonia">Macedonia</option>
<option value="Madagascar">Madagascar</option>
<option value="Malawi">Malawi</option>
<option value="Malaysia">Malaysia</option>
<option value="Maldives">Maldives</option>
<option value="Mali">Mali</option>
<option value="Malta">Malta</option>
<option value="Marshall Islands">Marshall Islands</option>
<option value="Martinique">Martinique</option>
<option value="Mauritania">Mauritania</option>
<option value="Mauritius">Mauritius</option>
<option value="Mayotte">Mayotte</option>
<option value="Mexico">Mexico</option>
<option value="Micronesia">Micronesia</option>
<option value="Moldava">Moldava</option>
<option value="Monaco">Monaco</option>
<option value="Mongolia">Mongolia</option>
<option value="Montenegro">Montenegro</option>
<option value="Montserrat">Montserrat</option>
<option value="Morocco">Morocco</option>
<option value="Mozambique">Mozambique</option>
<option value="Myanmar">Myanmar</option>
<option value="Namibia">Namibia</option>
<option value="Nauru">Nauru</option>
<option value="Nepal">Nepal</option>
<option value="Netherlands">Netherlands</option>
<option value="Netherlands Antilles">Netherlands Antilles</option>
<option value="New Caledonia">New Caledonia</option>
<option value="New Zealand">New Zealand</option>
<option value="Nicaragua">Nicaragua</option>
<option value="Niger">Niger</option>
<option value="Nigeria">Nigeria</option>
<option value="Niue">Niue</option>
<option value="Norfolk Island">Norfolk Island</option>
<option value="Northern Mariana Islands">Northern Mariana Islands</option>
<option value="Norway">Norway</option>
<option value="Oman">Oman</option>
<option value="Pakistan">Pakistan</option>
<option value="Palau">Palau</option>
<option value="Palestine">Palestine</option>
<option value="Panama">Panama</option>
<option value="Papua New Guinea">Papua New Guinea</option>
<option value="Paraguay">Paraguay</option>
<option value="Peru">Peru</option>
<option value="Philippines">Philippines</option>
<option value="Pitcairn">Pitcairn</option>
<option value="Poland">Poland</option>
<option value="Portugal">Portugal</option>
<option value="Qatar">Qatar</option>
<option value="Reunion">Reunion</option>
<option value="Romania">Romania</option>
<option value="Russian Federation">Russian Federation</option>
<option value="Rwanda">Rwanda</option>
<option value="S Georgia and S Sandwich Islands">S Georgia and S Sandwich Islands</option>
<option value="Samoa">Samoa</option>
<option value="San Marino">San Marino</option>
<option value="Sao Tome and Principe">Sao Tome and Principe</option>
<option value="Saudi Arabia">Saudi Arabia</option>
<option value="Senegal">Senegal</option>
<option value="Serbia">Serbia</option>
<option value="Seychelles">Seychelles</option>
<option value="Sierra Leone">Sierra Leone</option>
<option value="Singapore">Singapore</option>
<option value="Slovakia">Slovakia</option>
<option value="Slovenia">Slovenia</option>
<option value="Solomon Islands">Solomon Islands</option>
<option value="Somalia">Somalia</option>
<option value="South Africa">South Africa</option>
<option value="Spain">Spain</option>
<option value="Sri Lanka">Sri Lanka</option>
<option value="St Helena">St Helena</option>
<option value="St Kitts and Nevis">St Kitts and Nevis</option>
<option value="St Lucia">St Lucia</option>
<option value="St Pierre and Miquelon">St Pierre and Miquelon</option>
<option value="St Vincent and Grenadines">St Vincent and Grenadines</option>
<option value="Sudan">Sudan</option>
<option value="Suriname">Suriname</option>
<option value="Svalbard and Jan Mayen">Svalbard and Jan Mayen</option>
<option value="Swaziland">Swaziland</option>
<option value="Sweden">Sweden</option>
<option value="Switzerland">Switzerland</option>
<option value="Syria">Syria</option>
<option value="Taiwan">Taiwan</option>
<option value="Tajikistan">Tajikistan</option>
<option value="Tanzania">Tanzania</option>
<option value="Thailand">Thailand</option>
<option value="Timor-Leste">Timor-Leste</option>
<option value="Togo">Togo</option>
<option value="Tokelau">Tokelau</option>
<option value="Tonga">Tonga</option>
<option value="Trinidad and Tobago">Trinidad and Tobago</option>
<option value="Tunisia">Tunisia</option>
<option value="Turkey">Turkey</option>
<option value="Turkmenistan">Turkmenistan</option>
<option value="Turks and Caicos Islands">Turks and Caicos Islands</option>
<option value="Tuvalu">Tuvalu</option>
<option value="US Minor Islands">US Minor Islands</option>
<option value="Uganda">Uganda</option>
<option value="Ukraine">Ukraine</option>
<option value="United Arab Emirates">United Arab Emirates</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Uruguay">Uruguay</option>
<option value="Uzbekistan">Uzbekistan</option>
<option value="Vanuatu">Vanuatu</option>
<option value="Vatican City">Vatican City</option>
<option value="Venezuela">Venezuela</option>
<option value="Vietnam">Vietnam</option>
<option value="Virgin Islands (British)">Virgin Islands (British)</option>
<option value="Wallis and Futuna">Wallis and Futuna</option>
<option value="Western Sahara">Western Sahara</option>
<option value="Yemen">Yemen</option>
<option value="Yugoslavia">Yugoslavia</option>
<option value="Zambia">Zambia</option>
<option value="Zimbabwe">Zimbabwe</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Title of video</label>
<sup class="required" title="Required">*</sup>
<input type="text" name="title" title="Title of video" value="{{old('title')}}" class="file" name="video">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Regional Agency</label>
<sup class="required" title="Required">*</sup>
<select class="file" title="Regional Agency" id="state" {{old('agency')}} name="agency">
<option value="">Select your regional agency</option>
<option>Africa</option>
<option>Asia</option>
<option>Australia</option>
<option>Europe/United Kingdom</option>
<option>North America</option>
</select>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>Category</label>
<sup class="required" title="Required">*</sup>
<select class="file" title="Category" id="state" {{old('category')}} name="category">
<option value="">Select a category</option>
<option value="Traditional Rhythm (Small Band)">Traditional Rhythm (small band)</option>
<option value="Traditional Rhythm (Large Band)">Traditional Rhythm (large band)</option>
<option value="Voices and Percussion (Small Band)">Voices and Percussion (small band)</option>
<option value="Voices and Percussion (Large Band)">Voices and Percussion (large band)</option>
<option value="Melodic Percussion (Small Band)">Melodic Percussion (small band)</option>
<option value="Melodic Percussion (Large Band)">Melodic Percussion (large band)</option>
<option value="Drum Kit Solo">Drum Kit Solo</option>
<option value="Drum Line Face Off (Small Band)">Drum Line Face-off (small band)</option>
<option value="Drum Line Face Off (Large Band)">Drum Line Face-off (large band)</option>
<option value="Innovative and Creative Rhythm (Small Band)">Innovative and Creative Rhythm (small band)</option>
<option value="Innovative and Creative Rhythm (Large Band)">Innovative and Creative Rhythm (large band)</option>
</select>
</div>
</div>
<div class="container" style="margin-top:10px;">
<label style="margin-bottom:-10%;">VIDEO UPLOAD</label><hr>
<input type="file" title="Upload a Video" name="video" class="file">
</div>
<div class="col-md-12 col-sm-12">
<br>
<button type="submit" class="btn btn-info btn-block"><i class="fa fa-save"></i> SUBMIT APPLICATION</button>
</div>
</div>
</form>
But the problem is, i don't know how to go about this.
Any help will be deeply appreciated.
Thank you
Below is a Vue code snippet implementing Harshal's solution using axios to make the HTTP request
data () {
return {
uploadProgress: 0
};
},
computed: {
config () {
return {
headers: { 'Content-Type': 'multipart/form-data' },
onUploadProgress: progressEvent => {
this.uploadProgress = Math.floor((progressEvent.loaded * 100) / progressEvent.total);
}
};
},
},
methods: {
upload (e) {
const files = e.target.files || e.dataTransfer.files;
if (!files.length) {
return;
}
const data = new FormData();
data.append('fileNameGoesHere', files.item(0));
axios.post(this.uploadUri, data, this.config).then(response => {
// file uploaded successfully
});
},
}
You don't need to do anything special on the server side. You can do this using purely client-side code if you are willing to use Ajax to submit form. There are many small things you need to do but these will be your major steps:
Don't do traditional form submission.
Instead, create a FormData object. FormData is a new way to do form submission in JavaScript application.
When the user clicks the submit button, construct this FormData object.
For file input, you should use <input type="file" />.
Create an instance of XHR object. XHR is XMLHttpRequest.
The beauty of XHR is that you get a progress event indicating how much bytes have been sent. That is exactly what you need here.
Use onprogress event fired by XHR object to get periodic progress data.
Also remember to use multipart form type.
MDN has a very nice article depicting the overall flow. https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
Note: you may have heard of new fetch API in browsers. You can use that but unfortunately, it is Promise based and it won't return the actual progress event. So you cannot use that.

Vuejs v-for not populating the select option value in laravel blade

The expected result should return the id's value
<select class="form-control" name="uploaded_segment_id" id="uploaded_segment_id" required="">
<option value="">Choose Segment</option>
<option v-for="uploaded_segment in uploaded_segments" value="#{{ uploaded_segment.id }}"> #{{ uploaded_segment.name }}</option>
</select>
//Output
<option value="">Choose Segment</option>
<option value="{{ uploaded_segment.id }}">Brand1_April_2017_April_2017</option>
According to the documentation, this is the way to do it:
<option v-for="option in options" v-bind:value="option.value">
{{ option.text }}
</option>
So change this line:
<option v-for="uploaded_segment in uploaded_segments" value="#{{ uploaded_segment.id }}"> #{{ uploaded_segment.name }}</option>
to:
<option v-for="uploaded_segment in uploaded_segments" v-bind:value="uploaded_segment.id"> #{{ uploaded_segment.name }}</option>

th:selected a number in a select/option with Thymeleaf doesn't work

I have this code
<div th:class="form-group">
<td><label class="control-label leftMargin10 rightMargin10" scope="col" th:text="#{insertHours.hhFrom}">Attivita'</label></td>
<td><select class="form-control" th:field="*{hhFrom}">
<option th:each="i : ${#numbers.sequence(0, 23)}" th:value="${i}" th:text="${i}" th:selected="${ i==9 } ">Options</option>
</select>
</td>
</div>
When I try to add a condition in th:selected it doesn't work.
I have also replaced with this code:
th:attr="${i==9}? selected=selected: '' "
but the result is the same.
The HTML
<select class="form-control" id="hhFrom" name="hhFrom">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
</select>
Thanks in advance to answers
You cannot use th:field along with th:selected.
If you replace th:field with name=someMeaningfullName the code will work just fine.
Check out this thread on the Thymeleaf forum for more information
Another hack that, is pretty simple and works, is to close the select tag: <select ... />
Please note /> at the end of first line:
<select th:field="*{percentage}" />
<option th:each="i : ${#numbers.sequence(0, 100)}" th:value="${i}" th:text="${i}" th:selected="${i==75}"></option>
</select>
Renders as:
<select id="percentage" name="percentage" />
<option value="0">0</option>
<option value="1">1</option>
...
<option value="74">74</option>
<option value="75" selected="selected">75</option>
<option value="76">76</option>
...
<option value="100">100</option>
</select>
Both web browser and Thymeleaf will handle this fine.
I used Thymeleaf v.3.0.9.RELEASE
Also I found out that if you put <div> tag around option fields, selected="selected" will also work. e.g
<select th:field="*{name}">
<div>
<option disabled="true" selected="selected" value="">Select</option>
<option value="1">first</option>
<option value="2">second</option>
</div>
</select>

Resources