This is a migrated thread and some comments may be shown as answers.

RadColorPicker Validation

5 Answers 81 Views
ColorPicker
This is a migrated thread and some comments may be shown as answers.
Pradeep Enugala
Top achievements
Rank 1
Pradeep Enugala asked on 22 May 2010, 12:49 PM
Hi,
       How to add RequiredFildValidator or Custom Validator for RadColorPcker?
       Iam getting "Control 'RadColorPicker' referenced by the ControlToValidate property of 'RequiredFieldValidator'  
      cannot be validated
" error message When RequiredFieldValiator is aded to RadColorPicker.
Thank you,
Durga.

5 Answers, 1 is accepted

Sort by
0
Tsvetie
Telerik team
answered on 26 May 2010, 03:10 PM
Hi Durga,
You should use a CustomValidator control without setting its ControlToValidate property. You can use either the ClientValidationFunction to validate the color picker client-side, or the ServerValidate event of the validator to validate the color picker server-side.

All the best,
Tsvetie
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
subhashini T
Top achievements
Rank 1
answered on 10 Jun 2010, 06:42 AM
Not setting the controltovalidate for radcolorpicker does work but whenever validation fails, the related error message doesn't get displayed, unlike the usual case(for the other controls).  Any specific reasons for the same?
0
Tsvetie
Telerik team
answered on 15 Jun 2010, 01:37 PM
Hi subhashini T,
In case you do not set the ControlToValidate property of the custom validator, you must check yourself whether the value of the control is valid. For example, in case you are using client-side validation, you use the IsValid property of the arguments object:
<script language="javascript">
   function ClientValidate(source, arguments)
   {
        if (arguments.Value % 2 == 0 ){
            arguments.IsValid = true;
        } else {
            arguments.IsValid = false;
        }
   }
</script>

For server-side validation, you use the IsValid property of the args object:
void ServerValidation(object source, ServerValidateEventArgs args)
      {
         try
         {
            // Test whether the value entered into the text box is even.
            int i = int.Parse(args.Value);
            args.IsValid = ((i%2) == 0);
         }
 
         catch(Exception ex)
         {
            args.IsValid = false;
         }
      }

Additional information is available in MSDN:
  1. CustomValidator control
  2. ClientValidationFunction
  3. ServerValidate

Regards,
Tsvetie
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Joshy Varghese
Top achievements
Rank 1
answered on 19 Jul 2010, 11:13 PM

 

Can you explain how OnServerValidate will fire with out ControlTo Validate. In my debug OnServerValidate event is not firing at all.

<telerik:RadColorPicker ID="cPicker1" runat= "server" ShowIcon="true" ></telerik:RadColorPicker>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Select differnt color"
OnServerValidate="ColorPickerValidate" ></asp:CustomValidator>


protected void ColorPickerValidate(object source, ServerValidateEventArgs args)
{
if (ColorTranslator.ToHtml(cPickerBodyBg.SelectedColor=="#FFFFF))
{
args.IsValid = false;
}
}

Thanks!

0
Tsvetie
Telerik team
answered on 22 Jul 2010, 08:23 PM
Hi Joshy Varghese,
The handler that you have attached will be called on postback. For example, in case you add an ASP Button control and you click that button, the ServerValidate event of the custom validator will be raised even though the ControlToValidate property is not set. Have a look at the paragraph below - it is taken from MSDN:

quote:
It is possible to use a CustomValidator control without setting the ControlToValidate property. This is commonly done when you are validating multiple input controls or validating input controls that cannot be used with validation controls, such as the CheckBox control. In this case, the Value property of the arguments parameter passed to the event handler for the ServerValidate event and to the client-side validation function always contains an empty string (""). However, these validation functions are still called, where appropriate, to determine validity on both the server and client. To access the value to validate, you must programmatically reference the input control you want to validate and then retrieve the value from the appropriate property. For example, to validate a CheckBox control on the server, do not set the ControlToValidate property of the validation control and use the following code for the handler for the ServerValidate event.

In case you have configured your color picker control to postback the page, you should additionally call the Validate method of the Page manually in the handler for the ColorChange event of the color picker control:
<telerik:RadColorPicker ID="cPicker1" runat="server" ShowIcon="true"
    AutoPostBack="true" OnColorChanged="OnColorChanged">
</telerik:RadColorPicker>

protected void OnColorChanged(object source, EventArgs args)
{
    Page.Validate();
}

Regards,
Tsvetie
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
ColorPicker
Asked by
Pradeep Enugala
Top achievements
Rank 1
Answers by
Tsvetie
Telerik team
subhashini T
Top achievements
Rank 1
Joshy Varghese
Top achievements
Rank 1
Share this question
or