This project is read-only.

CheckBoxList Validation

Jun 28, 2011 at 4:50 AM

Hi,

How would I go about making the checkboxlist a required field?

The selected values are returned into the following List:

[Required]

public List<string> ActionIds { get; set; }

 Thanks

 

Coordinator
Jun 28, 2011 at 10:03 AM

Hi @spajic,

The Required attribute doesn't work because it verifies if the property is null. In that case if no checkbox is checked the list is an empty list not a null list. 

One nneeds to implement a custom attribute that is pecific for ICollection and verify if the a collectionb (in your case a list is empty). However, this will work just on the server side.

On the client side, just simple properties (numbers, dates, etch) can be controlled by the Mvc engine.

I will add a NotEmptyAttribute in the RTW version.

Thanks for the feedback.

Coordinator
Jun 28, 2011 at 3:08 PM

Hi @spjic,

I just modified the RequiredFieldsAttribute, to check also for empty enumerables if the named parameter RejectEmptyIEnumerables is provided and set to true. This attribute is put on a whole class and check all the required filelds of that class.

Pls download the last release of the sources here. In the sample code you will find an example of use the RequiredFieldAttribute where I check some string field and also a checkbox list:

[RequiredFields("{0} cannot be empty", RejectEmptyIEnumerables=true, Fields = "PersonalData.Name, PersonalData.SurName, Roles1")]
public class RegisterModel

.............................

.............................