Beispiel #1
0
        private void AddRequiredListValidation(Control control)
        {
            if (!IsRequired)
            {
                return;
            }

            CheckBoxListValidator rfv = new CheckBoxListValidator();

            this.Controls.Add(rfv);

            rfv.ControlToValidate = control.ID;
            rfv.Display           = ValidatorDisplay.Dynamic;
            rfv.ErrorMessage      = string.Format(Resources.Controls.Validator_Format_RequiredField, this.Label);
            rfv.Text            = Resources.Controls.Validator_RequiredField;
            rfv.ValidationGroup = ValidationGroup;
            rfv.CssClass        = ValidationCssClass;
        }
        public BaseValidator GetInputValidator()
        {
            BaseValidator validator = null;

            switch ((FieldType)this.Type)
            {
            case FieldType.CheckBox:
                validator = new CheckBoxListValidator();
                break;

            default:
                validator = new RequiredFieldValidator();
                break;
            }

            validator.ControlToValidate = this.GetInputControlId();
            validator.ValidationGroup   = FormBuilder_Module.GetValidationGroup(this.TemplateId);
            validator.Text         = "Required";
            validator.Display      = ValidatorDisplay.None;
            validator.ErrorMessage = string.Format("{0} input is required.", this.Name);
            validator.Visible      = true;

            return(validator);
        }
        public static void SetupPropertyControl(
            Page currentPage,
            Panel parentControl,
            mojoProfilePropertyDefinition propertyDefinition,
            String propertyValue,
            Double legacyTimeZoneOffset,
            TimeZoneInfo timeZone,
            string siteRoot)
        {
            if (propertyValue == null)
            {
                propertyValue = String.Empty;
            }

            string validatorSkinID = "Profile";

            if (currentPage is mojoPortal.Web.UI.Pages.Register)
            {
                validatorSkinID = "Registration";
            }

            Literal rowOpenTag = new Literal();

            rowOpenTag.Text = "<div class='settingrow " + propertyDefinition.CssClass + "'>";
            parentControl.Controls.Add(rowOpenTag);

            SiteLabel label = new SiteLabel();

            label.ResourceFile = propertyDefinition.ResourceFile;
            // if key isn't in resource file use assume the resource hasn't been
            //localized and just use the key as the resource
            label.ShowWarningOnMissingKey = false;
            label.ConfigKey = propertyDefinition.LabelResourceKey;
            label.CssClass  = "settinglabel";

            if (propertyDefinition.ISettingControlSrc.Length > 0)
            {
                Control c = null;
                if (propertyDefinition.ISettingControlSrc.EndsWith(".ascx"))
                {
                    c = currentPage.LoadControl(propertyDefinition.ISettingControlSrc);
                }
                else
                {
                    try
                    {
                        c = Activator.CreateInstance(System.Type.GetType(propertyDefinition.ISettingControlSrc)) as Control;
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex);
                    }
                }

                if ((c != null) && (c is ISettingControl))
                {
                    c.ID = "isc" + propertyDefinition.Name;
                    parentControl.Controls.Add(label);

                    ISettingControl settingControl = (ISettingControl)c;

                    settingControl.SetValue(propertyValue);
                    parentControl.Controls.Add(c);

                    if (propertyDefinition.IncludeHelpLink)
                    {
                        AddHelpLink(parentControl, propertyDefinition);
                    }
                }
            }
            else if (propertyDefinition.OptionList.Count > 0)
            {
                if (propertyDefinition.Type == "CheckboxList")
                {
                    CheckBoxList cbl = CreateCheckBoxListQuestion(propertyDefinition, propertyValue);
                    cbl.ID            = "cbl" + propertyDefinition.Name;
                    cbl.EnableTheming = false;
                    cbl.CssClass      = "forminput";

                    cbl.TabIndex     = 10;
                    label.ForControl = cbl.ID;
                    parentControl.Controls.Add(label);

                    parentControl.Controls.Add(cbl);

                    if (propertyDefinition.IncludeHelpLink)
                    {
                        AddHelpLink(parentControl, propertyDefinition);
                    }

                    if (propertyDefinition.RequiredForRegistration)
                    {
                        CheckBoxListValidator rfv = new CheckBoxListValidator();
                        rfv.SkinID            = validatorSkinID;
                        rfv.ControlToValidate = cbl.ID;

                        rfv.ErrorMessage = string.Format(CultureInfo.InvariantCulture, Resources.ProfileResource.ProfileRequiredItemFormat,
                                                         ResourceHelper.GetResourceString(propertyDefinition.ResourceFile, propertyDefinition.LabelResourceKey));

                        //rfv.Display = ValidatorDisplay.None;
                        rfv.ValidationGroup = "profile";
                        parentControl.Controls.Add(rfv);
                    }
                }
                else
                {
                    // add a dropdownlist with the options

                    DropDownList dd = CreateDropDownQuestion(propertyDefinition, propertyValue);
                    dd.ID            = "dd" + propertyDefinition.Name;
                    dd.EnableTheming = false;
                    dd.CssClass      = "forminput " + propertyDefinition.CssClass;

                    dd.TabIndex      = 10;
                    label.ForControl = dd.ID;
                    parentControl.Controls.Add(label);

                    parentControl.Controls.Add(dd);

                    if (propertyDefinition.IncludeHelpLink)
                    {
                        AddHelpLink(parentControl, propertyDefinition);
                    }

                    if (propertyDefinition.RequiredForRegistration)
                    {
                        RequiredFieldValidator rfvDd = new RequiredFieldValidator();
                        rfvDd.SkinID            = validatorSkinID;
                        rfvDd.ControlToValidate = dd.ID;
                        //if(dd.Items.Count > 0)
                        //{
                        //    rfvDd.InitialValue = dd.Items[0].Value;
                        //}


                        rfvDd.ErrorMessage = string.Format(CultureInfo.InvariantCulture, Resources.ProfileResource.ProfileRequiredItemFormat,
                                                           ResourceHelper.GetResourceString(propertyDefinition.ResourceFile, propertyDefinition.LabelResourceKey));


                        rfvDd.ValidationGroup = "profile";
                        parentControl.Controls.Add(rfvDd);
                    }

                    if (propertyDefinition.RegexValidationExpression.Length > 0)
                    {
                        RegularExpressionValidator regexValidator = new RegularExpressionValidator();
                        regexValidator.SkinID               = validatorSkinID;
                        regexValidator.ControlToValidate    = dd.ID;
                        regexValidator.ValidationExpression = propertyDefinition.RegexValidationExpression;
                        regexValidator.ValidationGroup      = "profile";
                        if (propertyDefinition.RegexValidationErrorResourceKey.Length > 0)
                        {
                            regexValidator.ErrorMessage = ResourceHelper.GetResourceString(
                                propertyDefinition.ResourceFile,
                                propertyDefinition.RegexValidationErrorResourceKey);
                        }

                        //regexValidator.Display = ValidatorDisplay.None;
                        parentControl.Controls.Add(regexValidator);
                    }
                }
            }
            else
            {
                switch (propertyDefinition.Type)
                {
                case "System.Boolean":
                    CheckBox checkBox = new CheckBox();
                    checkBox.TabIndex = 10;
                    checkBox.ID       = "chk" + propertyDefinition.Name;
                    checkBox.CssClass = "forminput " + propertyDefinition.CssClass;
                    label.ForControl  = checkBox.ID;
                    parentControl.Controls.Add(label);
                    parentControl.Controls.Add(checkBox);
                    if (propertyDefinition.IncludeHelpLink)
                    {
                        AddHelpLink(parentControl, propertyDefinition);
                    }

                    if (propertyValue.ToLower() == "true")
                    {
                        checkBox.Checked = true;
                    }
                    break;

                case "System.DateTime":
                    // TODO: to really make this culture aware we should store the users
                    // culture as well and use the user's culture to
                    // parse the date
                    DatePickerControl datePicker = CreateDatePicker(propertyDefinition, propertyValue, legacyTimeZoneOffset, timeZone, siteRoot);

                    datePicker.TabIndex = 10;
                    datePicker.ID       = "dp" + propertyDefinition.Name;
                    datePicker.CssClass = "forminput " + propertyDefinition.CssClass;
                    parentControl.Controls.Add(label);

                    parentControl.Controls.Add(datePicker);


                    if (propertyDefinition.IncludeHelpLink)
                    {
                        AddHelpLink(parentControl, propertyDefinition);
                    }

                    if (propertyDefinition.RequiredForRegistration)
                    {
                        RequiredFieldValidator rfvDate = new RequiredFieldValidator();
                        rfvDate.SkinID            = validatorSkinID;
                        rfvDate.ControlToValidate = datePicker.ID;

                        rfvDate.ErrorMessage = string.Format(CultureInfo.InvariantCulture, Resources.ProfileResource.ProfileRequiredItemFormat,
                                                             ResourceHelper.GetResourceString(propertyDefinition.ResourceFile, propertyDefinition.LabelResourceKey));

                        //rfvDate.Display = ValidatorDisplay.None;
                        rfvDate.ValidationGroup = "profile";
                        parentControl.Controls.Add(rfvDate);
                    }

                    if (propertyDefinition.RegexValidationExpression.Length > 0)
                    {
                        RegularExpressionValidator regexValidatorDate = new RegularExpressionValidator();
                        regexValidatorDate.SkinID               = validatorSkinID;
                        regexValidatorDate.ControlToValidate    = datePicker.ID;
                        regexValidatorDate.ValidationExpression = propertyDefinition.RegexValidationExpression;
                        regexValidatorDate.ValidationGroup      = "profile";
                        if (propertyDefinition.RegexValidationErrorResourceKey.Length > 0)
                        {
                            regexValidatorDate.ErrorMessage = ResourceHelper.GetResourceString(
                                propertyDefinition.ResourceFile,
                                propertyDefinition.RegexValidationErrorResourceKey);
                        }

                        //regexValidatorDate.Display = ValidatorDisplay.None;
                        parentControl.Controls.Add(regexValidatorDate);
                    }

                    break;

                case "System.String":
                default:

                    TextBox textBox = new TextBox();
                    textBox.TabIndex = 10;
                    textBox.ID       = "txt" + propertyDefinition.Name;
                    textBox.CssClass = "forminput " + propertyDefinition.CssClass;
                    label.ForControl = textBox.ID;
                    parentControl.Controls.Add(label);

                    if (propertyDefinition.MaxLength > 0)
                    {
                        textBox.MaxLength = propertyDefinition.MaxLength;
                    }

                    if (propertyDefinition.Columns > 0)
                    {
                        textBox.Columns = propertyDefinition.Columns;
                    }

                    if (propertyDefinition.Rows > 1)
                    {
                        textBox.TextMode = TextBoxMode.MultiLine;
                        textBox.Rows     = propertyDefinition.Rows;
                    }

                    parentControl.Controls.Add(textBox);
                    if (propertyDefinition.IncludeHelpLink)
                    {
                        AddHelpLink(parentControl, propertyDefinition);
                    }

                    if (propertyValue.Length > 0)
                    {
                        textBox.Text = propertyValue;
                    }
                    if (propertyDefinition.RequiredForRegistration)
                    {
                        RequiredFieldValidator rfv = new RequiredFieldValidator();
                        rfv.SkinID            = validatorSkinID;
                        rfv.ControlToValidate = textBox.ID;

                        rfv.ErrorMessage = string.Format(CultureInfo.InvariantCulture, Resources.ProfileResource.ProfileRequiredItemFormat,
                                                         ResourceHelper.GetResourceString(propertyDefinition.ResourceFile, propertyDefinition.LabelResourceKey));

                        //rfv.Display = ValidatorDisplay.None;
                        rfv.ValidationGroup = "profile";
                        parentControl.Controls.Add(rfv);
                    }

                    if (propertyDefinition.RegexValidationExpression.Length > 0)
                    {
                        RegularExpressionValidator regexValidator = new RegularExpressionValidator();
                        regexValidator.SkinID               = validatorSkinID;
                        regexValidator.ControlToValidate    = textBox.ID;
                        regexValidator.ValidationExpression = propertyDefinition.RegexValidationExpression;
                        regexValidator.ValidationGroup      = "profile";
                        if (propertyDefinition.RegexValidationErrorResourceKey.Length > 0)
                        {
                            regexValidator.ErrorMessage = ResourceHelper.GetResourceString(
                                propertyDefinition.ResourceFile,
                                propertyDefinition.RegexValidationErrorResourceKey);
                        }

                        //regexValidator.Display = ValidatorDisplay.None;
                        parentControl.Controls.Add(regexValidator);
                    }

                    break;
                }
            }


            Literal rowCloseTag = new Literal();

            rowCloseTag.Text = "</div>";
            parentControl.Controls.Add(rowCloseTag);
        }