Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Sep 16, 2010 @ 15:19
    Tim
    0

    Default ValidationGroup name?

    Hi,

    Does anyone know what the default validation group name is for contour? I have a custom control I'm adding, but it doesn't seem to be adding the validation group to it's validationgroup setting, so it's not getting fired along with the standard form controls.

    Does anyone know how I'd go about setting this? The control I'm adding is the Telerik RadCaptcha one. I know there's the ReCaptcha one, but this is to replace that, as ReCaptcha requires you to authenticate server side to google, and the very strict security on the server prevents this, and changing the security is not an option.

    Cheers,

    Tim.

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Sep 16, 2010 @ 17:05
    Tim
    0

    I've figured out a (hacky) way of doing this in the unlikely offchance that this is something anyone else runs into. I just attach an event to the Init event of the RadCaptcha control (any webcontrol should have this event to tap into), and then have the following code to work out what the validationgroup should be:

    string validationGroup = ((RadCaptcha)sender).Parent.ClientID;
    int start = validationGroup.IndexOf("_RenderForm_");
    int end = validationGroup.IndexOf("_", start + 12);
    ((RadCaptcha)sender).ValidationGroup = validationGroup.Substring(0, end);

    Basically, get the parent controls' ID, and strip out everything after the macro control to get the macro client id.

    Cheers,

    Tim.

  • Greg Berlin 818 posts 634 karma points
    Sep 25, 2010 @ 16:21
    Greg Berlin
    0

    Ugh, this is really annoying me.  I've created a custom control (dropdownlist with validation), and trying to get it to validate with the contour form. It's just not working very well for me, assumedly because i dont have the right validationGroup set.

    I've tried the above code, and various combinations. I've debugged and looked through all the controls being rendered, and can't find anything that even contains "_RenderForm_" in it.  Is this supposed to work for the latest version (1.1.3)??

    Here's the code i have currently:

    
    

        public class ValidatedDropdownList : DropDownList

        {

            private RangeValidator ran;

            public string InvalidMessage;

            public string IntroMessage;

            public string ClientScript = "true";

     

            public ValidatedDropdownList()

            {

                //InvalidMessage = invalidMsg;

            }

     

            protected override void OnInit(EventArgs e)

            {

                ran = new RangeValidator();

                ran.MinimumValue = "0";

                ran.MaximumValue = "99999999";

     

                ran.ControlToValidate = this.ID;

                ran.ErrorMessage = InvalidMessage;

                ran.EnableClientScript = (this.ClientScript.ToLower() != "false");

     

                string validationGroup = Parent.ClientID; // I can't find the correct control to use as the base to determine the validation group

                int start = validationGroup.IndexOf("_RenderForm_");

                int end = validationGroup.IndexOf("_", start + 12);

     

                ran.ValidationGroup = validationGroup.Substring(0, end);

     

                if (this.SelectedIndex < 0)

                    ran.IsValid = false;

                //ran.IsValid = false;

     

                Controls.Add(ran);

     

            }

     

     

            protected override void Render(HtmlTextWriter w)

            {

                base.Render(w);

                base.RenderChildren(w);

                //req.RenderControl(w);

            }

     

            /// <summary>

            /// Override the CreateControlCollection to overcome the "does not allow child controls".

            /// </summary>

            /// <returns></returns>

            protected override ControlCollection CreateControlCollection()

            {

                return new ControlCollection(this);   

            }

        }

     

    Please, if anybody can help steer me in the right direction, i'll be eternally grateful.  I'll happily post my full working code once i'm done, as i have been unable to find a full example outlining how to create a custom fieldset, which renders a custom webcontrol, which contains custom validation.  If there is an example out there, a link to that would suffice too.

    Cheers

    Greg

  • Greg Berlin 818 posts 634 karma points
    Sep 25, 2010 @ 16:39
    Greg Berlin
    0

    Actually it seems like client side validation is working okay for my control, but when that passes and it posts back, it fails on the server side.  

    This has me stumped.  I'm going to bed before it breaks me completely. :|

    I posted a bit more info here btw:  http://our.umbraco.org/forum/umbraco-pro/contour/12897-Best-way-to-create-a-lookup-field?p=0#comment47984

  • Greg Berlin 818 posts 634 karma points
    Sep 26, 2010 @ 04:58
    Greg Berlin
    0

    Hooray, i got this working.  Turns out it wasn't the validation group (I didnt even need to set this, since it's the only form on the page).. i just had to override the OnSelectedIndexChanged function, and call the validation from there.

    Here's my working code of my custom control if anybody is interested:

        public class ValidatedDropdownList : DropDownList
        {
            private RangeValidator rangeValidator;
            public string InvalidMessage;
            public string IntroMessage;
            public string ClientScript = "true";
    
            protected override void OnInit(EventArgs e)
            {
                rangeValidator = new RangeValidator();
                rangeValidator.MinimumValue = "0";
                rangeValidator.MaximumValue = "99999999";
    
                rangeValidator.ControlToValidate = this.ID;
                rangeValidator.ErrorMessage = InvalidMessage;
                rangeValidator.EnableClientScript = (this.ClientScript.ToLower() != "false");
    
                rangeValidator.IsValid = validateSelectedValue(this.SelectedValue);
    
                Controls.Add(rangeValidator);            
            }
    
            protected override void OnSelectedIndexChanged(EventArgs e)
            {
                base.OnSelectedIndexChanged(e);
                rangeValidator.IsValid = validateSelectedValue(this.SelectedValue);
            }
    
            private bool validateSelectedValue(string inValue)
            {
                Int32 selectedValue = -5;
                bool b = Int32.TryParse(inValue, out selectedValue);
    
                return (b && selectedValue > 0);
            }
    
            protected override void Render(HtmlTextWriter w)
            {
                base.Render(w);
                base.RenderChildren(w);
            }
    
            /// <summary>
            /// Override the CreateControlCollection to overcome the "does not allow child controls".
            /// </summary>
            /// <returns></returns>
            protected override ControlCollection CreateControlCollection()
            {
                return new ControlCollection(this);   
            }
        }
    
  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Oct 29, 2010 @ 17:46
    Ismail Mayat
    0

    Tim,

    I need to get validation group for contour in own custom control the oninit event does not give me sender is there another way i can get it?

    I have

    protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                if (PerformUniqueEmailTest && !Page.IsPostBack)
                {
                    AddEmailValidatorToEmail();
                }
            }

    in my addemailvalidatortoemail method in my Webcontrol i have custom validator that does unique email check on members (got contour registratation form) however the validator does not fire as i dont have contour validationgroup!!

    Regards

    Ismail

  • Harald Ulriksen 207 posts 249 karma points
    Oct 29, 2010 @ 21:14
    Harald Ulriksen
    0

    Ismail,

    I've done some test and it seems to me that it uses the ID of the render form user control as name of the validation group.

    If you add an Event handler to the PreRender event of your validation control you can locate the RenderForm usercontrol (if you have multiple forms on a page you might want to iterate over the parent property until you get the form) and set the validationgroup based on the Id.

    Hope this helps,
    Harald

    http://harald.ulriksen.net

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Nov 01, 2010 @ 09:03
    Ismail Mayat
    100

    Harald,

    Managed to sort this nicely, in fact thanks to your injection suggestion i can get other custom validators injected in to double check email and password entered twice are correct.  I managed to get the validation group by using a generic find control like method e.g:

     /// <summary>
            /// get find control method using generics
            /// </summary>
            /// <typeparam name="T">type of item you want</typeparam>
            /// <param name="ancestor">parent to start looking from</param>
            /// <returns>collection of T items</returns>
            public static List<T> ControlsByTypeUsingAncestor<T>(Control ancestor) where T : Control
            {
                var controls = new List<T>();
    
                //Append to search results if we match the type 
                if (typeof (T).IsAssignableFrom(ancestor.GetType()))
                {
                    controls.Add((T) ancestor);
                }
    
                //Recurse into child controls 
                foreach (Control ctrl in ancestor.Controls)
                {
                    controls.AddRange(ControlsByTypeUsingAncestor<T>(ctrl));
                }
    
                return controls;
            }

     

    I called it this way to get the validation gruop

    string vGroup = string.Empty;
                List<RequiredFieldValidator> reqValidators = ExtensionMethods.ControlsByTypeUsingAncestor<RequiredFieldValidator>(this.Page);
                var validatorWithGroupSet = (from v in reqValidators
                                            where v.ValidationGroup != string.Empty
                                            select v).FirstOrDefault();
                vGroup = validatorWithGroupSet.ValidationGroup;

    Regards

    Ismail

Please Sign in or register to post replies

Write your reply to:

Draft