I work on a asp.net mvc project with the durandal template + breeze.
I would like to define validation logic on my view for adding/editing operations.
So far, it works for texts, dropdown populated from database: when the element didn't contains any data, then the element is marked in red thanks to knockout validation.
But it doesn't work for dropdown populated from simple list.
The elements which populates the list comes from an enum:
public class Transport
{
[Key]
int id { get; set; }
...
[Required]
public EnumCategory Category { get; set; }
}
public enum EnumCategory
{
Cat1,
Cat2,
Cat3
}
First problem: I don't know if it is possible to retrieve the enum from my model (server side) to use it client side? For now, I created an array client side for populating my dropdown:
var categories = [
{ id: 1, description: "Cat1" },
{ id: 2, description: "Cat2" },
{ id: 3, description: "Cat3" }];
Second problem: when displaying my view, if the category dropdown already contains some data, the validation works (I mean the field is marked in red if the user clear the dropdown). But if the category dropdown didn' contains any data when view is displayed, then the dropdown is not marked in red.
What I do when validation failed is changing the background color of the element if not valid (thanks to css 'input-validation-error' >> red color).
<select data-bind="options: $root.categories,
optionsText: 'description',
optionsValue: 'id',
optionsCaption: 'Choose...',
value: category,
validationOptions: { errorElementClass: 'input-validation-error' },
valueUpdate: 'afterkeydown'">
</select>
Does anybody know why the validation on my dropdown didin't work?
Thanks.