Custom PropertyLongString - Could not save property, and it has been reverted.

Vote:
 

Hi,

I'm trying to create a custom PropertyLongString based on a class that has 2 string properties. This is the guide I'm following: http://azanganeh.com/blog/2016/10/30/episerver-custom-property-in-simple-steps-by-step-example/. Whenever I try to save the property in the CMS editor, I get a validation error which doesn't help much:

Could not save property, and it has been reverted. Please try again.

Error converting value "console.log('hi');" to type 'Models.Properties.JavascriptString'. Path '', line 1, position 20. Could not cast or convert from System.String to Models.Properties.JavascriptString.

 

The editor control is just a simple textare based on dijit/form/TextBox.

Here's the JavascriptString class:

public class JavascriptString
{
	public JavascriptString() { }

	public string Actual { get; set; }

	public string Minified { get; set; }
}

And the JavascriptStringProperty class:

[EditorHint(SiteUIHints.JavascriptString)]
[PropertyDefinitionTypePlugIn(Description = "A property for javascript", DisplayName = "Javascript")]
public class JavascriptStringProperty : PropertyLongString
{
		private Minifier minifier;

		public JavascriptStringProperty()
		{
				minifier = new Minifier();
		}

		public override object Value
		{
				get
				{
						var value = base.Value as string;
						if (value == null)
						{
								return null;
						}

						return JsonConvert.DeserializeObject(value);
				}
				set
				{
						JavascriptString js;
						if (value is JavascriptString)
						{
								js = value as JavascriptString;
								if (!string.IsNullOrEmpty(js.Actual))
								{
										js.Minified = minifier.MinifyJavaScript(js.Actual);
								}
						}
						else if (value is string)
						{
								var v = value as string;
								js = new JavascriptString
								{
										Actual = v,
										Minified = minifier.MinifyJavaScript(v)
								};
						}
						else
						{
								js = new JavascriptString();
						}
						base.Value = JsonConvert.SerializeObject(js);
				}
		}

		public override object SaveData(PropertyDataCollection properties)
		{
				return LongString;
		}

		public override Type PropertyValueType
		{
				get
				{
						return typeof(JavascriptString);
				}
		}
}

Then I'm using this property like so:

[UIHint(SiteUIHints.JavascriptString)]
[BackingType(typeof(JavascriptStringProperty))]
[Display(Order = 4, Name = "Javascript", Description = "Javacript for this button.")]
public virtual JavascriptString Javascript { get; set; }

I'm really not sure what the problem is here. Is there something wrong with the setter for the Value, or the SaveData() method (I'm not really sure when this method gets called, couldn't find any info on it)? Or maybe the PropertyValueType needs to be String? Any help would be great.

#181930
Sep 05, 2017 17:52
Vote:
 

Edward, you haven't shared your EditorDescriptor, I just want to make sure that you have something like this in your project before we go any further:

[EditorDescriptorRegistration(TargetType = typeof(JavascriptString), UIHint = Global.SiteUIHints.JavascriptString)]
public class JavascriptStringEditorDescriptor : EditorDescriptor
{
	public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
	{
		ClientEditingClass = "your/custom/dojo/property/edtior/path";

		base.ModifyMetadata(metadata, attributes);
	}
}
#181935
Sep 05, 2017 23:02
Vote:
 

Thanks Bartosz, yes I have an EditorDescriptor already like this:

[EditorDescriptorRegistration(TargetType = typeof(JavascriptString), UIHint = SiteUIHints.JavascriptString)]
public class JavascriptStringEditorDescriptor : EditorDescriptor
{
    public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
    {
        ClientEditingClass = "app/editors/javascripteditor/JavascriptStringEditor";
        base.ModifyMetadata(metadata, attributes);
    }
}

I can give the widget javascript and template as well if that would help. But the actual control textarea is functioning, I have able to save the property as just a simple string. I just can't get it to save when I try to serialize and deserialize it as a JSON string.

#181974
Sep 06, 2017 15:37