PDA

View Full Version : Validate a repeater component


pulse00
04-23-2009, 01:37 PM
Hi there,

does anyone know if it's possible to use a validator to validate the contents of a repeater component ?

I'm having a repeater which consists of n checkboxes. I'd like to validate the checkboxes to a certain upper maximum value, ie when there are 10 checkboxes, the user shall only be allowed to check 5 of them.

So far i've tried to set the source of a generic validator to the repeater component and the property to currentItem, but that doesn't seem to trigger any validation (trigger is set to FlexEvent.ValueCommit).

Anyone ever tried this ?

Peter Cowling
04-23-2009, 07:47 PM
Is the problem getting validator to work at all, or trying to get this 5 out of 10 aspect to work? Either way, code would help.

pulse00
04-23-2009, 08:11 PM
i think this would be too much of a hastle to write a custom validator, so i've done my own validation:

<mx:Repeater dataProvider="{appModel.annotationModel}" id="rep">
<mx:Label text="{rep.currentItem.name}" id="lab"/>
<mx:VBox>
<mx:Repeater id="annotationRepeater" dataProvider="{rep.currentItem.annotations}">
<mx:CheckBox label="{annotationRepeater.currentItem.name}" id="box"
valueCommit="{event.currentTarget.getRepeaterItem().selected = event.currentTarget.selected; validateGroup(event)}">
</mx:CheckBox>
</mx:Repeater>
</mx:VBox>
</mx:Repeater>


The dataprovider for the outer repeater is a Value Object with an attribute name and an attribute maxCount. The value object has an an ArrayCollection of value objects, which have the attributes 'selected' and 'name'. The dataprovider items of the inner repeater can be checked or unchecked.

Here's how i do the validation:

private function validateGroup(e:FlexEvent):void {

var annotation:Annotation = e.currentTarget.getRepeaterItem() as Annotation;
var group:AnnotationGroup = annotation.group;

var max:int = group.maxCount;
var count:int = 0;

if (max == 0) {
e.currentTarget.parent.errorString = null;
return;
}

for each (var a:Annotation in group.annotations) {
if (a.selected)
count++;
}

if (count > max) {
e.currentTarget.parent.errorString = "This group can only have a maximum of " + max + " annotations!";
}

else e.currentTarget.parent.errorString = null;

}


Would have been nice though to wrap this in a custom validator, but again, i don't think it would have been worth the effort, as it would have not been very reusable.

Peter Cowling
04-24-2009, 11:22 AM
Checking: Before taking a look: Is this now working for you then?

pulse00
04-24-2009, 04:13 PM
yes, the code below works for me for validating nested repeater elements.

Peter Cowling
04-24-2009, 04:24 PM
Okay, cool :)