New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the Community Exchange, where community members ask and answer questions about DNN. To get started, just start typing your question below and either select one of the suggested questions or ask a new question of your own.

Can I add extra validators on profile properties?

Return to previous page

  • 11/26/2012
  • 1997 Views

Question:

Adriaan Venter long time ago

I would like to create my own custom profile property data type, where I would like to add a custom validator to the property in the property data type class. Is this possible?

 It seems as though the fieldeditor only fires events of the 2 validators it added (required + regular expression) itself.

 

Here is my code, copied from the core...:

 #region Copyright

//
// DotNetNuke® - http://www.dnnsoftware.com
// Copyright (c) 2002-2011
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

#endregion

#region Usings

using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Instrumentation;
using DotNetNuke.Services.Localization;

#endregion

namespace DotNetNuke.UI.WebControls
{
    /// -----------------------------------------------------------------------------
    /// Project:    DotNetNuke
    /// Namespace:  DotNetNuke.UI.WebControls
    /// Class:      TextEditControl
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The TextEditControl control provides a standard UI component for editing
    /// string/text properties.
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    ///     [cnurse]    02/21/2006    created
    /// </history>
    /// -----------------------------------------------------------------------------
    [ToolboxData("<{0}:EmidTextEditControl runat=server></{0}:EmidTextEditControl>")]
    public class EmidTextEditControl : EditControl
    {
        private TextBox stringField;
        private CustomValidator cv;


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// OldStringValue returns the Boolean representation of the OldValue
        /// </summary>
        /// <value>A String representing the OldValue</value>
        /// <history>
        ///     [cnurse]    06/14/2006    created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected string OldStringValue
        {
            get
            {
                return Convert.ToString(OldValue);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// StringValue is the value of the control expressed as a String
        /// </summary>
        /// <value>A string representing the Value</value>
        /// <history>
        ///     [cnurse]    02/21/2006    created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override string StringValue
        {
            get
            {
                string strValue = Null.NullString;
                if (Value != null)
                {
                    strValue = Convert.ToString(Value);
                }
                return strValue;
            }
            set
            {
                Value = value;
            }
        }


        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            stringField = new TextBox();
            stringField.ControlStyle.CopyFrom(ControlStyle);
            stringField.ID = ID + "emid";
            Controls.Add(stringField);

            Controls.Add(new LiteralControl("&amp;nbsp;"));

            cv = new CustomValidator();
            cv.Display = ValidatorDisplay.Dynamic;
            cv.ID = UniqueID + "_Cus";
            cv.ErrorMessage = "error!!";
            cv.ValidateEmptyText = true;
            cv.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(CheckRadioButtons);
            //cv.EnableClientScript = true;
            cv.ControlToValidate = ID + "emid";
            Controls.Add(cv);
        }

        protected virtual void LoadDateControls()
        {
            if (StringValue != Null.NullString)
            {
                stringField.Text = StringValue;
            }
        }

        public override bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            EnsureChildControls();
            bool dataChanged = false;
            string presentValue = StringValue;
            string postedValue = postCollection[postDataKey + "emid"];
            if (!presentValue.Equals(postedValue))
            {
                Value = postedValue;
                dataChanged = true;
            }

            return dataChanged;
        }
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// OnDataChanged runs when the PostbackData has changed.  It raises the ValueChanged
        /// Event
        /// </summary>
        /// <history>
        ///     [cnurse]    02/21/2006    created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void OnDataChanged(EventArgs e)
        {
            var args = new PropertyEditorEventArgs(Name);
            args.Value = StringValue;
            args.OldValue = OldStringValue;
            args.StringValue = StringValue;
            base.OnValueChanged(args);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            LoadDateControls();

            if (Page != null &amp;&amp; EditMode == PropertyEditorMode.Edit)
            {
                Page.RegisterRequiresPostBack(this);
            }
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// RenderEditMode renders the Edit mode of the control
        /// </summary>
        /// <param name="writer">A HtmlTextWriter.</param>
        /// <history>
        ///     [cnurse]    02/27/2006    created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void RenderEditMode(HtmlTextWriter writer)
        {
            RenderChildren(writer);
        }
        protected void CheckRadioButtons(object source, ServerValidateEventArgs args)
        {
            bool result = false; // forcing error to check whether page shows validator
            args.IsValid = result;
        }


    }
}

Can I add extra validators on profile properties?

Adriaan Venter
Adriaan Venter long time ago
Add an Answer

Answers

Sign In to Participate
Or register to become a member