7 Answers, 1 is accepted
Hello, Alessio,
Please refer to the following help article which demonstrates how to add different validation rules via code specifying the control, the property name, operator and value to compare with: https://docs.telerik.com/devtools/winforms/controls/validation-provider/validation-rules
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Our thoughts here at Progress are with those affected by the outbreak.
Hi Dess, thanks for your help.
I've read the article but it doesn't explain how to check the text length of a control.
Can you please give me an example of a validation rule that checks if the text in a text box is less than 20 characters ?
RadValidationRule allows you to define a rule for a certain control by specifying the PropertyName that has to be validated. However, RadTextBox doesn't offer a property that specifies how many symbols are entered in the editor. This can be checked by the RadTextBox.Text.Length property.
The possible solution that I can suggest is to add a RadValidationRule for empty Text and in addition to this handle the ControlValidation event which occurs before a RadEditorControl is being validated. The RadValidationEventArgs gives very useful information about the tool tip error indication, validation rule, etc. The IsValid argument allows you to override the default result indicating whether the validation fails and change the error message accordingly:
public RadForm1()
{
InitializeComponent();
RadValidationRule radValidationRule1 = new RadValidationRule();
radValidationRule1.AutoToolTip = true;
radValidationRule1.AddControl(this.radTextBox1);
radValidationRule1.Operator = Telerik.WinControls.Data.FilterOperator.IsNotLike;
radValidationRule1.PropertyName = "Text";
radValidationRule1.ToolTipText = "Text is empty!";
radValidationRule1.Value = "";
radValidationProvider1.ValidationRules.Add(radValidationRule1);
this.radValidationProvider1.ControlValidation += radValidationProvider1_ControlValidation;
}
private void radValidationProvider1_ControlValidation(object sender, RadValidationEventArgs e)
{
if (e.Control == this.radTextBox1 && this.radTextBox1.Text != string.Empty &&
this.radTextBox1.TextBoxElement.TextBoxItem.TextLength < 20)
{
e.ErrorText = "Text is less than 20 characters !";
e.IsValid = false;
}
}
Should you have further questions please let me know.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Our thoughts here at Progress are with those affected by the outbreak.
Hi Dess, thanks for your help.
I will take this solution for the moment. Hope in the future validation rules will be expanded allowing more complex rules with custom validation formulas.
It seems to be a reasonable request to support nested properties in RadValidationProvider, e.g. rule's PropertyName = Text.Length.
I have logged it in our feedback portal by creating a public thread on your behalf. You can track its progress, subscribe for status changes and add your comments on the following link - feedback item.
I have also updated your Telerik points.
Should you have further questions please let me know.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Our thoughts here at Progress are with those affected by the outbreak.
Hi Dess,
I tried to implement my own validation rules using the RadValidationProvider, but apprently the last rule override it all.
So I tried to set the main RadValidationRule as not null ( so always true ) and I've personalized the main rule as below:
I need then when click on "Save" button return whether the validation rules are respected or not ?
public
RadForm1()
{
InitializeComponent();
RadValidationRule FirstRule = ValdationRule_NotNull(TB_FirstField);
radValidationProvider1.ValidationRules.Add(FirstRule);
RadValidationRule SecondRule = ValdationRule_NotNull(TB_SecondField);
radValidationProvider1.ValidationRules.Add(SecondRule);
this
.radValidationProvider1.ControlValidation += radValidationProvider_ControlValidation;
}
private
void
radValidationProvider_ControlValidation(
object
sender, RadValidationEventArgs e)
{
ValidationProvider_Personalized(e, TB_FirstField, FieldTypes.String,
false
);
ValidationProvider_Personalized(e, TB_SecondField, FieldTypes.Integer,
true
);
}
public
enum
FieldTypes
{
Integer,
Float,
String,
DropdownList
};
private
RadValidationRule ValdationRule_NotNull(RadTextBox control)
{
RadValidationRule radValidationRule =
new
RadValidationRule();
radValidationRule.AutoToolTip =
true
;
radValidationRule.AddControl(control);
radValidationRule.Operator = Telerik.WinControls.Data.FilterOperator.IsNotNull;
radValidationRule.PropertyName =
"Text"
;
radValidationRule.ToolTipText =
"The field "
+ control.AccessibleDescription +
" is null"
;
radValidationRule.ToolTipTitle =
""
;
radValidationRule.Value =
""
;
return
radValidationRule;
}
private
void
ValidationProvider_Personalized(RadValidationEventArgs e, Control FieldToValidateControl, FieldTypes FieldType,
bool
IsRequired =
false
)
{
RadDropDownList DropdownToValidate =
new
RadDropDownList(); ;
RadTextBox TextBoxToValidate =
new
RadTextBox() ;
if
(FieldType != FieldTypes.DropdownList)
{
TextBoxToValidate = (RadTextBox)FieldToValidateControl;
FieldToValidateControl = TextBoxToValidate;
}
else
{
DropdownToValidate = (RadDropDownList)FieldToValidateControl;
FieldToValidateControl = DropdownToValidate;
}
if
(FieldType == FieldTypes.DropdownList)
{
if
(DropdownToValidate.SelectedIndex != -1)
//Valid selection
{
}
else
{
e.ErrorText =
"Le champ "
+ FieldToValidateControl.AccessibleDescription +
" est obligatoire"
;
e.ErrorTitle =
""
;
e.IsValid =
false
;
}
}
else
if
(FieldToValidateControl.Text.Length == 0 && IsRequired)
//this automatically works for String Validation
{
e.ErrorText =
"Le champ "
+ FieldToValidateControl.AccessibleDescription +
" est obligatoire"
;
e.ErrorTitle =
""
;
e.IsValid =
false
;
}
else
if
(FieldToValidateControl.Text.Length == 0 && !IsRequired)
{
}
else
{
switch
(FieldType.ToString())
{
case
"Integer"
:
{
int
numberInt;
if
(!
int
.TryParse(FieldToValidateControl.Text,
out
numberInt))
{
e.ErrorText =
"Le champ "
+ FieldToValidateControl.AccessibleDescription +
" ne doit contenir que des entiers"
;
e.ErrorTitle =
""
;
e.IsValid =
false
;
}
else
{
if
(
int
.Parse(FieldToValidateControl.Text) < 0)
{
e.ErrorText =
"Le champ "
+ FieldToValidateControl.AccessibleDescription +
" doit être un entier positif"
;
e.ErrorTitle =
""
;
e.IsValid =
false
;
}
}
break
;
}
case
"Float"
:
{
float
numberFloat;
if
(!
float
.TryParse(FieldToValidateControl.Text,
out
numberFloat))
{
e.ErrorText =
"Le champ "
+ FieldToValidateControl.AccessibleDescription +
" ne doit contenir que des nombres"
;
e.ErrorTitle =
""
;
e.IsValid =
false
;
}
break
;
}
case
"String"
:
{
break
;
}
}
}
}
Hello, Fahmi,
The ControlValidation event is specifically designed to handle cases in which the validation logic may not be easily defined with validation rules. It gives you the possibility to provide conditional validation and control the situations in which the control being validated has correct content or not. Additional information about the public API that RadValidationProvider offers is available here: https://docs.telerik.com/devtools/winforms/controls/validation-provider/validation-rules
There is a ValidationMode property that controls when the validation logic will be performed for the associated control. The available options are None, OnValidating, OnTextChange, Programmatically. The default value is OnValidating. It is up to you when to validate the control.
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.