help with custom form control

lawrence whittemore asked on March 20, 2023 14:09

We've previously used form controls that allowed us to encrypt data being stored into the database from a form submission but in 13 we are no longer able to use those controls, so I am trying to set something up to work similarly. I'm confused as to how this is supposed to work, when I run this in the debugger, the GetValue() function is hit like 4 times when saving the form just once. I just want to encrypt the data using our helper on the save and decrypt on the load?

 public class TestFormControlComponent : FormComponent<TestFormControlProperties, string>
{
    [BindableProperty]
    public string Value { get; set; }


    public override string GetValue()
    {
        if (Value.Length > 0)
        {
            Value = EncryptionHelper.Encrypt(Value);
        }
        else
        {
            Value = Value;
        }
        return Value;
    }


    public override void SetValue(string value)
    {
        if (value != null)
        {
            value = EncryptionHelper.Decrypt(value);
        }
        else
        {
            value = "";
        }

        Value = value;
    }

}

Correct Answer

Brenden Kehren answered on March 20, 2023 17:57

If it's a control for a form, this would need to reside in your MVC code. So I'd suggest testing there and performing your actions there as it will have a different effect on your input.

The form controls I've created for v13 have this line of code as well:

public override bool CustomAutopostHandling => true;

I'd also simplify your code to look something like this because the act of "setting" a value also calls the Override SetValue() (honestly this is prob your issue):

public override string GetValue()
{
    if (Value.Length > 0)
    {
        return EncryptionHelper.Encrypt(Value);
    }
    else
    {
        return Value;
    }
}


public override void SetValue(string value)
{
    if (value != null)
    {
        // note capital V vs. v
        Value = EncryptionHelper.Decrypt(value);
    }
    else
    {
        // note capital V vs. v
        Value = "";
    }
}
0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on March 20, 2023 16:16

Are you using these form controls in the CMS or on the customer facing site? Where are you capturing data from?

0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on March 20, 2023 16:32

in testing I am just doing it in the CMS by adding a new record to the form.

0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on March 20, 2023 16:56

I added this to see if it made a difference

public override object GetObjectValue()
    {
        var value = EncryptionHelper.Decrypt(base.GetObjectValue().ToString());
        return value;
    }

and when I hit save on the form it seems to jump from GetValue to GetObjectValue a bunch of times, but still ends up encrypting the text 1 too many times

0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on March 20, 2023 18:53

Thanks as always

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.