I just begin with webmatrix. I have been able to extract data from my database and to filter it within a date range (with datepicker). It works when I fill the dates, but it crash when the datepicker is empty. I would like to validate the fields and show
a message if they are empty. I have tried the Validation.RequiredField, but something doesn't work. When I submit I receive an error message that indicate a String (the "from" and probably the "to") where dateTime can't be recognized (line shown in bold).
Could you help me, here is my code:
@{
Layout = "~/_SiteLayout.cshtml";
Page.Title = "Run";
var db = Database.Open("StarterSite");
var from = "";
var to = "";
var entrainements = db.Query("SELECT * FROM course WHERE type='Entraînement' ORDER BY date DESC");
Validation.RequireField("from", "Select a date");
Validation.RequireField("to", "Select a date");
if(IsPost && Validation.IsValid()){
from = Request.Form["from"];
to = Request.Form["to"];
entrainements = db.Query("SELECT * FROM course WHERE date > @0 AND date < @1 AND type='Entraînement' ORDER BY date DESC", from, to); }
}
<script>
$(function() {$("#from").datepicker ({
defaultDate: "+1w",
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
yearRange: "-100:nn",
monthNamesShort: [ "Janv", "Fév", "Mars", "Avr", "Mai", "Juin", "Juil", "Août", "Sept", "Oct", "Nov", "Déc" ],
monthNames: [ "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre" ],
dayNamesMin: [ "Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa" ],
dateFormat: "d MM yy",
onClose: function(selectedDate){$( "#to" ).datepicker("option","minDate",selectedDate ); }
});
$("#to").datepicker({
defaultDate: "+1w",
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
yearRange: "-100:nn",
monthNamesShort: [ "Janv", "Fév", "Mars", "Avr", "Mai", "Juin", "Juil", "Août", "Sept", "Oct", "Nov", "Déc" ],
monthNames: [ "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre" ],
dayNamesMin: [ "Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa" ],
dateFormat: "d MM yy",
maxDate: "0",
onClose: function(selectedDate){$("#from").datepicker("option","maxDate",selectedDate ); }
});
});
</script>
@Html.ValidationSummary()
<form id="courseEntrainementsSorties" action="Page.cshtml" method="post">
<div>
<label for="from">Du</label><input type="text" id="from" name="from" value="from" />@Html.ValidationMessage("from")
<label for="to">au</label><input type="text" id="to" name="to" value="to"/>@Html.ValidationMessage("to")
<input type="submit" value="Go" />
<table class="sortiesTable" summary="La table montre les entraînements faits en ordre décroissant de date">
<caption>Liste des entraînements effectués</caption>
<thead>
<tr>
<th id="c1" class="w125" scope="col">Date</th>
<th id="c2" class="w125" scope="col">Type</th>
<th id="c3" class="w125" scope="col">Distance (km)</th>
<th id="c4" scope="col">Commentaires</th>
</tr>
</thead>
<tbody>
@{
if(IsPost){
int courseEntrainementsRN = 1;
foreach(var sorties in entrainements)
{
string rowId = "cr" + courseEntrainementsRN.ToString();
string headersId1 = "c1,cr" + courseEntrainementsRN.ToString();
string headersId2 = "c2,cr" + courseEntrainementsRN.ToString();
string headersId3 = "c3,cr" + courseEntrainementsRN.ToString();
<tr>
<th id="@rowId"
scope="row">@sorties.date.ToString("d MMM yyyy")</th>
<td headers="@headersId1"
class="tableCellAlignLeft">@sorties.type</td>
<td headers="@headersId2"
class="tableCellAlignLeft">@sorties.distance</td>
<td headers="@headersId3"
class="tableCellAlignLeft">@sorties.description</td>
</tr>
courseEntrainementsRN += 1;
}
}
}
</tbody>
</table>
</div>
</form>
prog4fun
Member
5 Points
33 Posts
Problem with Required Field Validation
Feb 19, 2013 04:01 AM|LINK
Hi!
I just begin with webmatrix. I have been able to extract data from my database and to filter it within a date range (with datepicker). It works when I fill the dates, but it crash when the datepicker is empty. I would like to validate the fields and show a message if they are empty. I have tried the Validation.RequiredField, but something doesn't work. When I submit I receive an error message that indicate a String (the "from" and probably the "to") where dateTime can't be recognized (line shown in bold). Could you help me, here is my code:
@{ Layout = "~/_SiteLayout.cshtml"; Page.Title = "Run"; var db = Database.Open("StarterSite"); var from = ""; var to = ""; var entrainements = db.Query("SELECT * FROM course WHERE type='Entraînement' ORDER BY date DESC"); Validation.RequireField("from", "Select a date"); Validation.RequireField("to", "Select a date"); if(IsPost && Validation.IsValid()){ from = Request.Form["from"]; to = Request.Form["to"]; entrainements = db.Query("SELECT * FROM course WHERE date > @0 AND date < @1 AND type='Entraînement' ORDER BY date DESC", from, to); } } <script> $(function() {$("#from").datepicker ({ defaultDate: "+1w", changeMonth: true, changeYear: true, numberOfMonths: 1, yearRange: "-100:nn", monthNamesShort: [ "Janv", "Fév", "Mars", "Avr", "Mai", "Juin", "Juil", "Août", "Sept", "Oct", "Nov", "Déc" ], monthNames: [ "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre" ], dayNamesMin: [ "Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa" ], dateFormat: "d MM yy", onClose: function(selectedDate){$( "#to" ).datepicker("option","minDate",selectedDate ); } }); $("#to").datepicker({ defaultDate: "+1w", changeMonth: true, changeYear: true, numberOfMonths: 1, yearRange: "-100:nn", monthNamesShort: [ "Janv", "Fév", "Mars", "Avr", "Mai", "Juin", "Juil", "Août", "Sept", "Oct", "Nov", "Déc" ], monthNames: [ "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre" ], dayNamesMin: [ "Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa" ], dateFormat: "d MM yy", maxDate: "0", onClose: function(selectedDate){$("#from").datepicker("option","maxDate",selectedDate ); } }); }); </script> @Html.ValidationSummary() <form id="courseEntrainementsSorties" action="Page.cshtml" method="post"> <div> <label for="from">Du</label><input type="text" id="from" name="from" value="from" />@Html.ValidationMessage("from") <label for="to">au</label><input type="text" id="to" name="to" value="to"/>@Html.ValidationMessage("to") <input type="submit" value="Go" /> <table class="sortiesTable" summary="La table montre les entraînements faits en ordre décroissant de date"> <caption>Liste des entraînements effectués</caption> <thead> <tr> <th id="c1" class="w125" scope="col">Date</th> <th id="c2" class="w125" scope="col">Type</th> <th id="c3" class="w125" scope="col">Distance (km)</th> <th id="c4" scope="col">Commentaires</th> </tr> </thead> <tbody> @{ if(IsPost){ int courseEntrainementsRN = 1; foreach(var sorties in entrainements) { string rowId = "cr" + courseEntrainementsRN.ToString(); string headersId1 = "c1,cr" + courseEntrainementsRN.ToString(); string headersId2 = "c2,cr" + courseEntrainementsRN.ToString(); string headersId3 = "c3,cr" + courseEntrainementsRN.ToString(); <tr> <th id="@rowId" scope="row">@sorties.date.ToString("d MMM yyyy")</th> <td headers="@headersId1" class="tableCellAlignLeft">@sorties.type</td> <td headers="@headersId2" class="tableCellAlignLeft">@sorties.distance</td> <td headers="@headersId3" class="tableCellAlignLeft">@sorties.description</td> </tr> courseEntrainementsRN += 1; } } } </tbody> </table> </div> </form>Pengzhen Son...
Star
8637 Points
884 Posts
Microsoft
Re: Problem with Required Field Validation
Feb 20, 2013 01:45 AM|LINK
Hi,
Maybe you should try convert the "from" and "to" from string to date.
Feedback to us
Develop and promote your apps in Windows Store
prog4fun
Member
5 Points
33 Posts
Re: Problem with Required Field Validation
Feb 21, 2013 11:14 PM|LINK
Thanks a lot! I was focusing on the required field validation, but the problem was effectively on the datetime format.