Just in Chronicles

Life as a Voyage

Triming Property Type Value – Description or Validation – While Saving Document Type in Umbraco

leave a comment »

When developers create a document type in Umbraco, they might notice that an extra line – either Line Feed (\n) or Carriage Return (\r), or both – is added into those two fields – Validation and Description – of each property. This extra white space will result in validation errors and prevent the content from saving, in the Content section.

In order to avoid this, the value must be trimmed while retrieving and saving by modifying the source code, UmbracoProject\umbraco\cms\businesslogic\propertytype\propertytype.cs. Find the following code lines from the file and put .Trim() as highlighted.

public string ValidationRegExp
{
	get { return _validationRegExp.Trim(); }
	set
	{
		_validationRegExp = value.Trim();
		InvalidateCache();
		SqlHelper.ExecuteNonQuery(
			"Update cmsPropertyType set validationRegExp = @validationRegExp where id = @id",
			SqlHelper.CreateParameter("@validationRegExp", value.Trim()), SqlHelper.CreateParameter("@id", Id));
	}
}
public string Description
{
	get
	{
		if (_description != null)
		{
			if (!_description.StartsWith("#"))
				return _description.Trim();
			else
			{
				Language lang = Language.GetByCultureCode(Thread.CurrentThread.CurrentCulture.Name);
				if (lang != null)
				{
					if (Dictionary.DictionaryItem.hasKey(_description.Substring(1, _description.Length - 1)))
					{
						var di =
							new Dictionary.DictionaryItem(_description.Substring(1, _description.Length - 1));
						return di.Value(lang.id).Trim();
					}
				}
			}

			return "[" + _description.Trim() + "]";
		}

		return _description.Trim();
	}
	set
	{
		_description = value.Trim();
		InvalidateCache();
		SqlHelper.ExecuteNonQuery(
			"Update cmsPropertyType set description = @description where id = @id",
			SqlHelper.CreateParameter("@description", value.Trim()),
			SqlHelper.CreateParameter("@id", Id));
	}
}
public string GetRawDescription()
{
	return _description.Trim();
}

By adding the .Trim() method, even though it can’t prevent displaying an extra line, but it won’t save the extra line into the database.

Advertisements

Written by Justin Yoo

27/03/2012 at 18:12

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s