Track file upload using vue.js - laravel

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.

Related

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 :)

retrieving the max value of all the selects in a page with Xpath

I want to retrieve max value for each combobox present in a web page, with xpath.
Here is a sample HTML containing several combobox with choices:
<label> <span class="invisible_spoken">Some choice</span>
<select class="select_class">
<option value="0">0</option>
<option value="1" >
1
</option>
<option value="2" >
2
</option>
</select>
</label>
<label> <span class="invisible_spoken">Some choice</span>
<select class="select_class">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</label>
<label> <span class="invisible_spoken">Some choice</span>
<select class="select_class">
<option value="0">0</option>
<option value="1">1</option>
</select>
</label
What I would want to retrieve is the maximum value of each combobox, sample output :
2, 3, 1
I've tried with an expression like :
//table[#id="rooms"]//select[contains(#class, "b_selectbox")]/option[not(//table[#id="rooms"]//select[contains(#class, "b_selectbox")]/option/#value > #value)]/#value
But it is returning just combobox max values of the maximum combo value, in the sample case just 3 that is the highest, in the case there will be two combobox with maximum values equals to 3, then it will return those 2 ...
Xpath Selector for Options with highest value.
//label/select[#class="select_class123"]/option[not(../option/#value > #value)]
o/p: Option elements with highest values.
(//label/select[#class="select_class"]/option[not(../option/#value > #value)])/#value
o/p: 2, 3, 1
Test XML XPath online:
<select class="select_class">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select class="select_class">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select class="select_class">
<option value="0">0</option>
<option value="1">1</option>
</select>

how to validate year of completion < current year means (year of completion < 2014) by using jquery

how to validate year of completion < current year means (year of completion < 2014)
by using jquery
<tr><td><label><span><font color="red">*</font></span>Year Of Completion</label>
</td><td><span><select class="textbox4" name="UGYearOfCompletion" id="UGYearOfCompletion">
<option selected="selected" value="">-select-</option> <option value="2017">2017</option> <option value="2016" >2016</option> <option value="2015" >2015</option> <option value="2014" >2014</option> <option value="2013" >2013</option> <option value="2012" >2012</option> <option value="2011" >2011</option> <option value="2010">2010</option> <option value="2009" >2009</option> <option value="2008" >2008</option> <option value="2007" >2007</option> <option value="2006" >2006</option> <option value="2005" >2005</option> <option value="2004" >2004</option> <option value="2003" >2003</option> <option value="2002" >2002</option> <option value="2001" >2001</option> <option value="2000" >2000</option> <option value="1999" >1999</option> <option value="1998" >1998</option> <option value="1997" >1997</option> <option value="1996" >1996</option> <option value="1995" >1995</option> <option value="1994" >1994</option> <option value="1993" >1993</option> <option value="1992" >1992</option> <option value="1991" >1991</option> <option value="1990" >1990</option> <option value="1989" >1989</option> <option value="1988" >1988</option> <option value="1987" >1987</option> <option value="1986" >1986</option> <option value="1985" >1985</option> <option value="1984" >1984</option> <option value="1983" >1983</option> <option value="1982" >1982</option> <option value="1981" >1981</option> <option value="1980" >1980</option> <option value="1979" >1979</option> <option value="1978" >1978</option> <option value="1977" >1977</option> <option value="1976" >1976</option> <option value="1975" >1975</option> <option value="1974" >1974</option> <option value="1973" >1973</option> <option value="1972" >1972</option> <option value="1971" >1971</option> <option value="1970" >1970</option> <option value="1969" >1969</option> <option value="1968" >1968</option> <option value="1967" >1967</option> <option value="1966" >1966</option> <option value="1965" >1965</option> <option value="1964" >1964</option> <option value="1963" >1963</option> <option value="1962" >1962</option> <option value="1961" >1961</option> <option value="1960" >1960</option> <option value="1959" >1959</option> <option value="1958" >1958</option> <option value="1957" >1957</option> <option value="1956" >1956</option> <option value="1955" >1955</option> <option value="1954" >1954</option> <option value="1953" >1953</option> <option value="1952" >1952</option> <option value="1951" >1951</option> <option value="1950" >1950</option>
</select></span></td>

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>

Dropdown Date Menus

I am clueless when it comes to JavaScript and Ajax. Could y'all point me to a simple tutorial site for both?
The algorithm for what I'm trying to accomplish is fairly simple. I have three drop-down menus to enter a date. The user selects a year, then a month, then, based on the two previous selections, chooses a date. So, if a user selects Feb, 2012, the date list will show twenty-nine days. If he wants to look at Feb. 2013, the date list would show 28 days. Or for another month, show 30 or 31 days. What sort of function would I need to accomplish this?
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form name="myform">
<fieldset><legend>Selection Date</legend><label for="year">Year: </label>
<select name="year" id="year" size="1">
<option value=" " selected="selected"></option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
</select>
<label for="month">Month: </label>
<select name="month" id="month" size="1">
<option value=" " selected="selected"></option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<label for="day">Day: </label>
<select name="day" id="day" size="1">
<option value=" " selected="selected"></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>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
</fieldset>
</form>
<script type="text/javascript">
function daysInMonth(month, year) {
var days = new Date(year, month, 0);
return days.getDate();
}
function setDayDrop(dyear, dmonth, dday) {
var year = dyear.options[dyear.selectedIndex].value;
var month = dmonth.options[dmonth.selectedIndex].value;
var day = dday.options[dday.selectedIndex].value;
if (day == ' ') {
var days = (year == ' ' || month == ' ') ? 31 : daysInMonth(month, year);
dday.options.length = 0;
dday.options[dday.options.length] = new Option(' ', ' ');
for (var i = 1; i <= days; i++)
dday.options[dday.options.length] = new Option(i, i);
}
}
function setDay() {
var year = document.getElementById('year');
var month = document.getElementById('month');
var day = document.getElementById('day');
setDayDrop(year, month, day);
}
document.getElementById('year').onchange = setDay;
document.getElementById('month').onchange = setDay;
</script>
</body>
Thanks for the speedy response. I found this on the www that puts a calendar into the form:
Any Time

Resources