I want to custom validate in my model : but it doesn't work correctly. Any body can help me?
I have a sample in attach file for this case.
- ProductsViewModel inherit IValidatableObject
public class ProductsViewModel : IValidatableObject
{
[Required]
public int ProductID { get; set; }
[Required]
[StringLength(80)]
public string ProductName { get; set; }
[Required]
[DataType(DataType.Currency)]
[Range(1, 100)]
public decimal? UnitPrice { get; set; }
public bool Discontinued { get; set; }
[Required]
[UIHint("Category")]
[Display(Name = "Category")]
public string CategoryID { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (ProductName == "Fire")
{
yield return new ValidationResult("Can not sell fire, too dangerous!!!");
}
}
}
And I have a Editor template: ProductsViewModel.cshtml
@model ProductsViewModel
@Html.HiddenFor(p => p.ProductID)
<ul>
<li>@Html.LabelFor(p => p.ProductName)
@Html.EditorFor(p => p.ProductName) </li>
<li>@Html.LabelFor(p => p.UnitPrice)
@Html.EditorFor(p => p.UnitPrice)
</li>
<li>@Html.LabelFor(p => p.CategoryID)
@Html.EditorFor(p => p.CategoryID)
</li>
<li>@Html.LabelFor(p => p.Discontinued)
@Html.EditorFor(p => p.Discontinued)
</li>
</ul>
@Html.ValidationSummary()
10 Answers, 1 is accepted
I have looked at the provided project. It seems that the validation rules are set to the viewModel (used for grid's population), however in the update action the entity is used instead viewModel. Therefore, the custom validation is not executed as the different object is used during validation.
Regards,Rosen
the Telerik team
Thanks for your answer
You gave me "the reason" the custom validation is not executed
my problem in my project is:
I used a grid - ajax editing - popup mode
And I need check a unique field before update/insert data into database.
How I can do that? Can you give me a solution or a sample with the best?
You may use MVC remote validation feature or manually validate the entity state in the Action method before it is inserted/updated. As you may agree implementation of domain validation is not directly related to our product.
All the best,Rosen
the Telerik team
Thanks for your help,
Have a nice week.
I have already tried to use Remote validation before, it worked. But I always must click 'Submit' button twice in edit popup form to persist a valid data set into database: in the first click, if input data is valid -> nothing happens, after that with second click I can persist valid data set into database.
Until now, I still find a solution for this problem.
This issue has been already resolved. You may upgrade to latest Q2 2011 SP1 version and give it a try.
Regards,Rosen
the Telerik team
I will get latest release version and test it.
I tried Q2 2011 SP1 version
This bug "Several clicks on Update button are required, in order changes to be submit if remote validation is used " is fixed
But It's not good as I expected because Remote attribute do not work.
You can see my following sample
Could you please elaborate a bit more on what is the behavior you are expecting, as maybe I'm missing something obvious. A step by step instructions how to recreated it will be appreciated.
All the best,Rosen
the Telerik team
I had a mistake in my sample, therefore you couldn't understand me. I will re-test everything again and describes my expectation for details after that.
Facilities I also have a question look forward to you reply.
in product page: I have a Category DropDownList, when user chooses a item in dropdownlist then display all products of chosen category in the product grid (using ajax editing popup mode)
Products Grid:
Html.Telerik().Grid<ProductsViewModel>()
.Name("Products")
.DataKeys(dataKeys => dataKeys.Add(p => p.ProductID))
.ToolBar(toolBar => toolBar.Insert())
.Columns(columns =>
{
columns.Bound(p => p.ProductName);
columns.Bound(p => p.CategoryID).Width(250).Title("Category");
columns.Bound(p => p.UnitPrice).Format("{0:c}").Width(80);
columns.Bound(p => p.Discontinued).Width(80)
.ClientTemplate("<input type='checkbox' disabled='disabled' name='Discontinued' <#=Discontinued? checked='checked' : '' #> />");
columns.Command(commands =>
{
commands.Edit();
commands.Delete();
}).Width(200);
})
.DataBinding(dataBinding => dataBinding.Ajax().Select("Select", "Home")
.Update("Update", "Home")
.Insert("Insert", "Home")
.Delete("Delete", "Home")
)
.Editable(editable => editable.Mode(GridEditMode.PopUp))
.Pageable()
.Sortable()
EditorTemplate file:
@model ProductViewModel
@Html.ValidationSummary()
@Html.HiddenFor(p => p.ProductID)
@Html.HiddenFor(p => p.CategoryID)
<ul>
<li>@Html.LabelFor(p => p.DisplayName)
@Html.EditorFor(p => p.DisplayName)
</li>
</ul>
User click "Create New", CategoryID hidden field alway is '0' (I think because a "new" ProductViewModel is generated to Editor Form).
==> The question: If I in use Insert() command in ToolBar, how I can set (pass, transfer) "chosen CategoryID" of Category DropDownList to "CategoryID" hidden field of Editor form when user click "Create New" button ?
Note: Currently, I must use a Session to store "the chosen category" and uses after that, I think it not good solution.
Hope that you can understand me ^-^
Default values for insert form could be set through DefaultDataItem method of Editable. If you want to set values dynamically you may use Edit client event.
All the best,Rosen
the Telerik team