Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom Dropdownlist does not keep attributes / properties #2156

Open
mbarmettler opened this issue Jan 16, 2019 · 0 comments
Open

custom Dropdownlist does not keep attributes / properties #2156

mbarmettler opened this issue Jan 16, 2019 · 0 comments

Comments

@mbarmettler
Copy link

Hi,

i try to create a new widget for showing up countries in a dropdownlist on a form.
sofar the control is initializing and showing up some data.
but - if i try to change the properties on it - for example "required" or "label/question" - it does not keep the values and does not give it back to the designer either.

here is my controller:

``
private ValidatorDefinition validatorDefinition;
private string serializedChoices;

    /// <inheritDocs />
    public string SerializedChoices
    {
        get
        {
            if (string.IsNullOrEmpty(this.serializedChoices))
            {
                this.serializedChoices = this.BuildInitialChoicesString();
                
            }

            return this.serializedChoices;
        }

        set
        {
            this.serializedChoices = value;
        }
    }

    /// <inheritDocs />
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public override ValidatorDefinition ValidatorDefinition
    {
        get
        {
            if (this.validatorDefinition == null)
            {
                this.validatorDefinition = base.ValidatorDefinition;

                this.validatorDefinition.RequiredViolationMessage = Res.Get<FormResources>().RequiredInputErrorMessage;
            }

            return this.validatorDefinition;
        }

        set
        {
            this.validatorDefinition = value;
        }
    }

    /// <inheritDocs />
    public override IMetaField GetMetaField(IFormFieldControl formFieldControl)
    {
        var metaField = base.GetMetaField(formFieldControl);

        if (string.IsNullOrEmpty(metaField.Title))
        {
            metaField.Title = Res.Get<FieldResources>().SelectChoice;
        }

        if (string.IsNullOrEmpty(metaField.DefaultValue))
        {
            var choices = this.DeserializeChoices();
            if (choices.Any())
            {
                metaField.DefaultValue = choices.FirstOrDefault();
            }
        }

        return metaField;
    }
    
    /// <summary>
    /// Prepopulates the initial choices.
    /// </summary>
    /// <returns></returns>
    public virtual string BuildInitialChoicesString()
    {
        var initialChoices = new string[]
        {
            Res.Get<FieldResources>().OptionSelect,
            Res.Get<FieldResources>().OptionFirstChoice,
            Res.Get<FieldResources>().OptionSecondChoice
        };
        return JsonConvert.SerializeObject(initialChoices);
        //var cultureList = LocationContext.Instance.Cultures("en");
        //var orderedcultureList = cultureList.OrderBy(x => x.CountryName).ToList();
        //return JsonConvert.SerializeObject(orderedcultureList.Select(x => x.CountryName).ToArray());
    }

    /// <inheritDocs />
    public override bool IsValid(object value)
    {
        if (this.ValidatorDefinition.Required.HasValue && this.ValidatorDefinition.Required.Value)
        {
            var strValue = value as string;
            if (!string.IsNullOrEmpty(strValue))
            {
                var choices = this.DeserializeChoices();
                if (choices.Contains(strValue))
                {
                    return base.IsValid(value);
                }
            }

            return false;
        }
        else
        {
            return base.IsValid(value);
        }
    }

    public IEnumerable<string> DeserializeChoices()
    {
        if (string.IsNullOrEmpty(this.SerializedChoices))
        {
            return Enumerable.Empty<string>();
        }

        return JsonConvert.DeserializeObject<IEnumerable<string>>(this.SerializedChoices);
    }

    /// <inheritDocs />
    private string BuildValidationAttributesString()
    {
        if (this.ValidatorDefinition.Required.HasValue && this.ValidatorDefinition.Required.Value)
        {
            return "required='required'";
        }

        return string.Empty;
    }

    public override object GetViewModel(object value, IMetaField metaField)
    {
        //workaround
        var cultureList = LocationContext.Instance.Cultures("en");
        var orderedcultureList = cultureList.OrderBy(x => x.CountryName).ToList();

        var countrySelectList = DataObjectConverter<List<SelectListItem>>.ConvertToViewModel(cultureList);

        List<string> countrStrings = new List<string>();
        foreach (var item in orderedcultureList)
        {
            countrStrings.Add(item.CountryName);
        }

        this.Value = value;

        return new CountryViewModel()
        {
            Value = value,
            CssClass = this.CssClass,
            MetaField = metaField,
            IsRequired = this.ValidatorDefinition.Required.HasValue ? this.ValidatorDefinition.Required.Value : false,
            ValidationAttributes = this.BuildValidationAttributesString(),
            RequiredViolationMessage = this.ValidatorDefinition.RequiredViolationMessage,
            Countries = countrySelectList,
            Choices = countrStrings
        };
    }
@mbarmettler mbarmettler changed the title Dropdownlist Read.defaul.cshtml missing Model.MetaField custom Dropdownlist does not keep attributes / properties Jan 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant