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,
On your form you may have many text boxes and each text box may have its custom logic and custom tool tip. When you hit “Save” button you can show custom message box if you have invalid fields. To do so you can check what Validate returns in its isValid property. If it is false send whatever message box you need. You can use code like this:
private OnSave(object sender, EventArgs e)
{
Validation valid = this.ultraValidator1.Validate(true, false);
if (!valid.IsValid)
MessageBox.Show("Please fill all fields correct");
}
Please check attached solution and 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!
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.