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

Suggestion: Built-in validators

4 Answers 126 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 24 Jul 2008, 03:29 PM
It would be oh, so wonderful to have at least a RequiredFieldValidator built into the grid ColumnEditor controls so that I wouldn't have to use template columns just to get a validator! :)

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Jul 2008, 06:36 AM
Hi Richard,

I have found the forum link which discuss about built-in validators with Grid column. Go through the link and see if it helps.
How to customize a GridColum with built-in (default) validators

Thanks
Shinu.
0
Richard
Top achievements
Rank 1
answered on 25 Jul 2008, 07:28 PM
Thanks for the link.  I have expanded on the suggestions there to create an extended bound column class with an assortment of validators that can be turned on selectively.

I still think this kind of thing would be handy 'out of the box' instead of as a user extension :)

using System; 
using System.ComponentModel; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
 
/// <summary> 
/// Extension to telerik:GridBoundColumn 
/// adding a selection of Validators that can 
/// be turned on and used as required 
/// </summary> 
 
namespace JVD.WebControls 
 
    public class ValidatorBoundColumn : GridBoundColumn 
    { 
        #region Constructor 
 
        public ValidatorBoundColumn() 
        { 
            RequiredFieldValidator = new RequiredFieldValidator(); 
            CompareValidator1 = new CompareValidator(); 
            CompareValidator2 = new CompareValidator(); 
            RegexValidator = new RegularExpressionValidator(); 
            RangeValidator = new RangeValidator(); 
            CustomValidator = new CustomValidator(); 
        } 
        #endregion Constructor 
        #region Properties 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateRequired { getset; } 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateCompare1 { getset; } 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateCompare2 { getset; } 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateRegex { getset; } 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateRange { getset; } 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateCustom { getset; } 
        #endregion Properties 
        #region Accessors 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public RequiredFieldValidator RequiredFieldValidator { getprivate set; } 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public CompareValidator CompareValidator1 { getprivate set; } 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public CompareValidator CompareValidator2 { getprivate set; } 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public RegularExpressionValidator RegexValidator { getprivate set; } 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public RangeValidator RangeValidator { getprivate set; } 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public CustomValidator CustomValidator { getprivate set; } 
        #endregion Accessors 
        #region Overrides 
 
        public override void InitializeCell(TableCell cell, int columnIndex, GridItem inItem) 
        { 
            base.InitializeCell(cell, columnIndex, inItem); 
 
            if (inItem.IsInEditMode) 
            { 
                string lBaseID = cell.ClientID; 
                string lControlID = String.Format("{0}_TextBox", DataField); 
                cell.Controls[0].ID = lControlID; 
                if (ValidateRequired) 
                { 
                    RequiredFieldValidator.ID = lBaseID + "_RequiredFieldValidator"
                    RequiredFieldValidator.ControlToValidate = lControlID; 
                    cell.Controls.Add(RequiredFieldValidator); 
                } 
                if (ValidateCompare1) 
                { 
                    CompareValidator1.ID = lBaseID + "_CompareValidator1"
                    CompareValidator1.ControlToValidate = lControlID; 
                    cell.Controls.Add(CompareValidator1); 
                } 
                if (ValidateCompare2) 
                { 
                    CompareValidator2.ID = lBaseID + "_CompareValidator2"
                    CompareValidator2.ControlToValidate = lControlID; 
                    cell.Controls.Add(CompareValidator2); 
                } 
                if (ValidateRegex) 
                { 
                    RegexValidator.ID = lBaseID + "_RegexValidator"
                    RegexValidator.ControlToValidate = lControlID; 
                    cell.Controls.Add(RegexValidator); 
                } 
                if (ValidateRange) 
                { 
                    RangeValidator.ID = lBaseID + "_RangeValidator"
                    RangeValidator.ControlToValidate = lControlID; 
                    cell.Controls.Add(RangeValidator); 
                } 
                if (ValidateCustom) 
                { 
                    CustomValidator.ID = lBaseID + "_CustomValidator"
                    CustomValidator.ControlToValidate = lControlID; 
                    cell.Controls.Add(CustomValidator); 
                } 
            } 
        } 
        #endregion Overrides 
    } 
 
 

Example column using a RequiredFieldValidator

<Columns> 
     <JVD:ValidatorBoundColumn UniqueName="Description" DataField="Description" ValidateRequired="true"
          <RequiredFieldValidator Display="Dynamic" SetFocusOnError ="true" ErrorMessage="<%$ Resources:JVD, Validation_Description_Required %>" /> 
     </JVD:ValidatorBoundColumn> 
</Columns> 
 
0
Accepted
Sebastian
Telerik team
answered on 28 Jul 2008, 05:58 AM
Hello Richard,

Thank you for the suggestion - I will forward it to our development department for further consideration. If this turns out to be a common request, you may see it included in a future version of RadGrid.

At this point you may consider the solution pointed by Shinu or extending the built-in GridBoundColumn to include validator controls in it as you already did. We appreciate posting your implementation in our public forums - I updated your Telerik points for the involvement.

Kind regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Richard
Top achievements
Rank 1
answered on 28 Jul 2008, 06:58 PM
I discovered today that my column class as posted above will only work in a top-level grid.  To work in a detail grid it needs a couple more overrides.  Below is the class with the overrides added, plus a version for DateTime columns (no regex validator and only one compare validator - as a note, I included two compare validators in the bound column so that I can use one to compare datatype and the other to compare value.
using System; 
using System.ComponentModel; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
 
/// <summary> 
/// Extension to telerik:GridBoundColumn 
/// adding a selection of Validators that can 
/// be turned on and used as required 
/// </summary> 
 
namespace JVD.WebControls 
 
    public class ValidatorBoundColumn : GridBoundColumn 
    { 
        #region Constructor 
 
        public ValidatorBoundColumn() 
        { 
            RequiredFieldValidator = new RequiredFieldValidator(); 
            CompareValidator1 = new CompareValidator(); 
            CompareValidator2 = new CompareValidator(); 
            RegexValidator = new RegularExpressionValidator(); 
            RangeValidator = new RangeValidator(); 
            CustomValidator = new CustomValidator(); 
        } 
        #endregion Constructor 
        #region Properties 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateRequired { getset; } 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateCompare1 { getset; } 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateCompare2 { getset; } 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateRegex { getset; } 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateRange { getset; } 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateCustom { getset; } 
        #endregion Properties 
        #region Accessors 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public RequiredFieldValidator RequiredFieldValidator { getprivate set; } 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public CompareValidator CompareValidator1 { getprivate set; } 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public CompareValidator CompareValidator2 { getprivate set; } 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public RegularExpressionValidator RegexValidator { getprivate set; } 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public RangeValidator RangeValidator { getprivate set; } 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public CustomValidator CustomValidator { getprivate set; } 
        #endregion Accessors 
        #region Overrides 
 
        public override void InitializeCell(TableCell cell, int columnIndex, GridItem inItem) 
        { 
            base.InitializeCell(cell, columnIndex, inItem); 
 
            if (inItem.IsInEditMode) 
            { 
                string lBaseID = cell.ClientID; 
                string lControlID = String.Format("{0}_TextBox", DataField); 
                cell.Controls[0].ID = lControlID; 
                if (ValidateRequired) 
                { 
                    RequiredFieldValidator.ID = lBaseID + "_RequiredFieldValidator"
                    RequiredFieldValidator.ControlToValidate = lControlID; 
                    cell.Controls.Add(RequiredFieldValidator); 
                } 
                if (ValidateCompare1) 
                { 
                    CompareValidator1.ID = lBaseID + "_CompareValidator1"
                    CompareValidator1.ControlToValidate = lControlID; 
                    cell.Controls.Add(CompareValidator1); 
                } 
                if (ValidateCompare2) 
                { 
                    CompareValidator2.ID = lBaseID + "_CompareValidator2"
                    CompareValidator2.ControlToValidate = lControlID; 
                    cell.Controls.Add(CompareValidator2); 
                } 
                if (ValidateRegex) 
                { 
                    RegexValidator.ID = lBaseID + "_RegexValidator"
                    RegexValidator.ControlToValidate = lControlID; 
                    cell.Controls.Add(RegexValidator); 
                } 
                if (ValidateRange) 
                { 
                    RangeValidator.ID = lBaseID + "_RangeValidator"
                    RangeValidator.ControlToValidate = lControlID; 
                    cell.Controls.Add(RangeValidator); 
                } 
                if (ValidateCustom) 
                { 
                    CustomValidator.ID = lBaseID + "_CustomValidator"
                    CustomValidator.ControlToValidate = lControlID; 
                    cell.Controls.Add(CustomValidator); 
                } 
            } 
        } 
 
        public override GridColumn Clone() 
        { 
            ValidatorBoundColumn lReturn = new ValidatorBoundColumn(); 
            lReturn.CopyBaseProperties(this); 
            return lReturn; 
        } 
 
        protected override void CopyBaseProperties(GridColumn fromColumn) 
        { 
            base.CopyBaseProperties(fromColumn); 
 
            ValidatorBoundColumn lColumn = (ValidatorBoundColumn) fromColumn; 
 
            CompareValidator1 = lColumn.CompareValidator1; 
            CompareValidator2 = lColumn.CompareValidator2; 
            CustomValidator = lColumn.CustomValidator; 
            RangeValidator = lColumn.RangeValidator; 
            RegexValidator = lColumn.RegexValidator; 
            RequiredFieldValidator = lColumn.RequiredFieldValidator; 
 
            ValidateCompare1 = lColumn.ValidateCompare1; 
            ValidateCompare2 = lColumn.ValidateCompare2; 
            ValidateCustom = lColumn.ValidateCustom; 
            ValidateRange = lColumn.ValidateRange; 
            ValidateRegex = lColumn.ValidateRegex; 
            ValidateRequired = lColumn.ValidateRequired; 
        } 
        #endregion Overrides 
    } 
 
 
using System.ComponentModel; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
 
/// <summary> 
/// Extension to telerik:GridDateTimeColumn 
/// adding a selection of Validators that can 
/// be turned on and used as required 
/// </summary> 
 
namespace JVD.WebControls 
 
    public class ValidatorDateTimeColumn : GridDateTimeColumn 
    { 
        #region Constructor 
 
        public ValidatorDateTimeColumn() 
        { 
            RequiredFieldValidator = new RequiredFieldValidator(); 
            CompareValidator = new CompareValidator(); 
            RangeValidator = new RangeValidator(); 
            CustomValidator = new CustomValidator(); 
        } 
        #endregion Constructor 
        #region Properties 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateRequired { getset; } 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateCompare { getset; } 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateRange { getset; } 
 
        [Category("Validation"), DefaultValue("false")] 
        public bool ValidateCustom { getset; } 
        #endregion Properties 
        #region Accessors 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public RequiredFieldValidator RequiredFieldValidator { getprivate set; } 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public CompareValidator CompareValidator { getprivate set; } 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public RangeValidator RangeValidator { getprivate set; } 
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
        PersistenceMode(PersistenceMode.InnerProperty)] 
        public CustomValidator CustomValidator { getprivate set; } 
        #endregion Accessors 
        #region Overrides 
 
        public override void InitializeCell(TableCell cell, int columnIndex, GridItem inItem) 
        { 
            base.InitializeCell(cell, columnIndex, inItem); 
 
            if (inItem.IsInEditMode) 
            { 
                string lBaseID = cell.ClientID; 
                string lControlID = cell.Controls[0].ID; 
                if (ValidateRequired) 
                { 
                    RequiredFieldValidator.ID = lBaseID + "_RequiredFieldValidator"
                    RequiredFieldValidator.ControlToValidate = lControlID; 
                    cell.Controls.Add(RequiredFieldValidator); 
                } 
                if (ValidateCompare) 
                { 
                    CompareValidator.ID = lBaseID + "_CompareValidator"
                    CompareValidator.ControlToValidate = lControlID; 
                    cell.Controls.Add(CompareValidator); 
                } 
                if (ValidateRange) 
                { 
                    RangeValidator.ID = lBaseID + "_RangeValidator"
                    RangeValidator.ControlToValidate = lControlID; 
                    cell.Controls.Add(RangeValidator); 
                } 
                if (ValidateCustom) 
                { 
                    CustomValidator.ID = lBaseID + "_CustomValidator"
                    CustomValidator.ControlToValidate = lControlID; 
                    cell.Controls.Add(CustomValidator); 
                } 
            } 
        } 
 
        public override GridColumn Clone() 
        { 
            ValidatorDateTimeColumn lReturn = new ValidatorDateTimeColumn(); 
            lReturn.CopyBaseProperties(this); 
            return lReturn; 
        } 
 
        protected override void CopyBaseProperties(GridColumn fromColumn) 
        { 
            base.CopyBaseProperties(fromColumn); 
            ValidatorDateTimeColumn lColumn = (ValidatorDateTimeColumn)fromColumn; 
 
            CompareValidator = lColumn.CompareValidator; 
            CustomValidator = lColumn.CustomValidator; 
            RangeValidator = lColumn.RangeValidator; 
            RequiredFieldValidator = lColumn.RequiredFieldValidator; 
 
            ValidateCompare = lColumn.ValidateCompare; 
            ValidateCustom = lColumn.ValidateCustom; 
            ValidateRange = lColumn.ValidateRange; 
            ValidateRequired = lColumn.ValidateRequired; 
        } 
        #endregion Overrides 
    } 
 
 
Tags
Grid
Asked by
Richard
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Richard
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or