Hello,
I have to validate a control against multiple different conditions. I have created a Custom Condition class and put my conditions in the Matches subroutine.
What I'd like to do is to customize also the Notification settings text that appears when one of the conditions is not matched. I can do this by passing the Control object and Validator object in the constructor of my class. The problem is that the Matches subroutine is not raised when the control value is empty, and so I can't customize the message in the "isRequired" condition is True.
To be more specific:
If the condition 1 is false, notification text has to be "Condition 1 not satisfied"
If the condition 2 is false, notification text has to be "Condition 2 not satisfied"
If the value is empty (and isRequired is true), notification text has to be "Value mandatory"
I'm not able to do the last one because Matches event is not raised when leaving the control with value empty, so notification text remains the older one (for example "Condition 2 not satisfied").
Hello Claudio,
You are on the right way. After implementing the ICondition interface you need set different rules in Matches method. In this method you should implements your conditions, based on which validation is considered as valid or not. For each condition which doesn’t matches you can add error message in a combined error message. Finally you can set this error message overriding the ToString method of your Condition class.
In your Form Load you have to implement your custom Condition class in new ValidationSettings and apply it to your control. Here you may set IsRequired property to true and custom NotificationSettings text.
Please find attached a sample solution implementing above mentioned logic.
Please let me know if this is what you are looking for or if I am missing something.
Sincerely,
Milko
Developer Support Engineer
Infragistics, Inc.
www.infragistics.com/support
Hello Milko,
Thank you for your answer, it puts me on the right track.
I still have a problem: with your solution I can customize the NotificationSettings text when I am editing my controls; usually on my windows forms I have a "Save" button and clicking on it I launch a global validation using the
ultravalidator1.Validate(true, false)
function. I use the value returned by this function to customize the message shown to the user (with a code similar the one of ultraValidator1_ValidationError in your example). Unfortunately, when validation goes wrong because of a condition failure, the notification text message of the control returns to the default one (i.e.: "This field is required!!" in your example) and not the customized one.
To be more specific using your example:
I enter the ultraTextEditor1 control and type "text". When i leave the control the validation goes wrong and the tooltip on the error icon says ("String must be longer than 5. String must starts with 'SP'"). This is correct. Then I click on a "Save" button on the form launching the ultravalidator1.Validate(true, false) command. After that, the tooltip on the error icon returns to "This field is required!!". The only update I made to your code is this one
private void ultraValidator1_ValidationError(object sender, ValidationErrorEventArgs e) { if (e.Control != null) { for (int i = 0; i < e.Validation.Errors.Count; i++) { ValidationResult error = e.Validation.Errors[i]; if (error.ValidationSettings.Condition is Condition && error.Status == ValidationStatus.ConditionFailure) { e.NotificationSettings.Text = e.Validation.Errors[0].ValidationSettings.Condition.ToString(); } } } }
because e.Control is null when lanching the Validate function.
Is it possible to maintain the correct notificationsettings tooltip text after lauching the Validate method?
Thank you!
Hi,
Thank you for contacting Infragistics Developer Support.
I am glad that you have resolved your issue. Let me know if you have any additional questions.
Sorry for the late reply.
With your last solution the problem of retaining the correct tooltip message after calling Validate method on validator component is solved, but there is a drawback: after the validation, if you enter a textbox and delete the text, the validation message doesn't change to "This field is required!!" but remains the custom one. That's because the NotificationSettings.Text property doesn't contain the generic error message anymore but the custom one.
The only way I've found to manage this has been to use this code on ValidationError event (vb.net)
Public Sub ValidatorGenerico_ValidationError(ByVal sender As Object, ByVal e As Infragistics.Win.Misc.ValidationErrorEventArgs) For i As Integer = 0 To e.Validation.Errors.Count - 1 Dim errore As Infragistics.Win.Misc.ValidationResult = e.Validation.Errors(i) If TypeOf errore.ValidationSettings.Condition Is MyCustomCondition Then If errore.Status = Infragistics.Win.Misc.ValidationStatus.ConditionFailure Then errore.ValidationSettings.NotificationSettings.Text = errore.ValidationSettings.Condition.ToString() If Not IsNothing(e.Control) Then e.NotificationSettings.Text = errore.ValidationSettings.NotificationSettings.Text Else errore.ValidationSettings.NotificationSettings.Text = errore.ValidationSettings.NotificationSettings.Tag If Not IsNothing(e.Control) Then e.NotificationSettings.Text = errore.ValidationSettings.NotificationSettings.Text End If End If Next End Sub
(ValidatorGenerico is my ultravalidator component)
I store the generic error message in NotificationSettings.Tag property of each control and so i change then NotificationSettings.Text property on the basis of the Status error value choosing from the custom one or the generic one.
Maybe it's not the best workaround but it's working fine for me right now.
Regards,
Claudio
I am just checking about the progress of this issue. Let me know if you need my further assistance on it.
Thank you for using Infragistics Components.
Thank you for your feedback.
To have custom messages appear you need to call ultraValidator1_ValidationError event handler from button1_Click. This is possible if you send to Validate method each control you need to validate. As you may have many controls on the form it is better to use a workaround. Instead of calling each control you need to validate, you can run one more time Validate method. This will fix the error messages issue.
Please check attached solution and let me know if this is what you are looking for or if I am missing something.
Hello Milka,
Your example reflects what I'm doing right now in my form. The only problem, and you can notice that in your last solution project, is that after you click the "Validate" button and the MessageBox "Please fill all fields correct" appears, the notification tooltip message on the 2 textboxes is reset to "The field is required!" (you can see it by passing the mouse on the error icon) while before was "String must be longer than 5...".If you enter and leave the 2 textboxes again the message returns the custom one.
My question is if there is a way to not reset the tooltip message to the default value (the one of the empty condition) when you Validate the form.
Thanks!
Claudio Di Flumeri