More than 1 validator for a textbox

Dec 4, 2008 at 6:10 PM
Can i have more than 1 validator for a textbox? for example. 

<TextBox x:Name="txtTextBox" Height="20" Margin="17,104,198,276" Width="75" d:LayoutOverrides="HorizontalAlignment">

 

 

<slv:ValidatorService.Validator>

 

 

<slv:RequiredValidator x:Name="ReqFV" ManagerName="ManagerRFV"></slv:RequiredValidator>

 

 

<slv:RangeValidator x:Name="range"></slv:RangeValidator>

 

 

</slv:ValidatorService.Validator>

 

 

 

</TextBox>



But this gives me an error 'The property validator is set more than once'

 


 

Coordinator
Dec 5, 2008 at 5:44 AM
Sandabh, 
    Unfortunately, in the current release you cannot have more than one validator attached to a single textbox.  However, looking at your example, you do not need the RequiredValidator.  The existing validators have a property 'IsRequired'.  Use this property to require and validate the input.
Dec 5, 2008 at 4:47 PM

Would i be able to assign validators from code dynamically depending on my inputs from database? would multiple validators work there? I tried single validator from code for textbox but it does not fire. the code was like this... but 'count' is always 0 and validator never fires.

 

Silverlight.Validators.

PostalCodeValidator pv = new Silverlight.Validators.PostalCodeValidator();

 

 

public Page()

 

{

InitializeComponent();

pv.ElementToValidate = reqtb;

pv.SetManagerAndControl(Group1, reqtb);

pv.IsValid =

true;

 

pv.IsRequired =

true;

 

pv.ErrorMessage =

"this is postalcode validator";

 

}

 

private void btn_Click(object sender, RoutedEventArgs e)

 

{

pv.IsValid =

true;

 

pv.ElementToValidate = reqtb;

pv.IsRequired =

true;

 

pv.Validate(

true);

 

 

List<Silverlight.Validators.ValidatorBase> valids = Group1.ValidateAll();

 

 

int count = valids.Count;

 

}

Dec 9, 2008 at 9:20 PM
sandabh,
where you able to get that working in code?

Thanks,
Robert
Dec 16, 2008 at 12:09 AM
You can assign validators dynamically. Please see this thread for an example Discussion Thread 41961. You should be able to assign more than one validator using this method.
Dec 16, 2008 at 5:37 PM
Thanks SoundScape!
Dec 16, 2008 at 5:57 PM
You really should consider making a CompositeValidator control with a list of validators, and simply validates them and returns the result... :)