XAF: Soft Validation (What’s New in 13.2)

XAF Team Blog
28 November 2013

Our next major release (13.2), updates our Validation Module with more features that come with different behavior rules, such as Warnings and Information. These can be applied to your existing validation rules using code or the Model Editor.

The XAF validation module is a mature and flexible validation system, which allows you to create rules that can validate any business problem, For more information, check out the module documentation.

Suppose we have a Business Object that disallows saving a Customer if the Name property is blank. If this attempted the UI should respond with a “Everybody has a name!” message.

[DefaultClassOptions]

public class Customer : BaseObject {

    public Customer(Session session) : base(session) { }

    [RuleRequiredField("Customer_Name_Required", DefaultContexts.Save, "Everybody has a name!")]

    public string Name { get; set; }

    public string City { get; set; }

    public int Age { get; set; }

}

When an end user tries to save the Customer without a name, XAF will show the Validation Error listview and consequently abort.

image

Creating a warning rule

A common requirement for our Customer class is to warn the end user that, if possible, he/she should provide the Customer’s city. To do this, we need to mark the City property with a  RuleRequiredField. However, this time we have to set the ResultType to Warning as illustrated below.

[DefaultClassOptions]

public class Customer : BaseObject {

    public Customer(Session session) : base(session) { }

    [RuleRequiredField("Customer_Name_Required", DefaultContexts.Save, "Everybody has a name!")]

    public string Name { get; set; }

    [RuleRequiredField("Customer_City_Warning", DefaultContexts.Save, "Provide the City if possible",

        ResultType = ValidationResultType.Warning)]

    public string City { get; set; }

    public int Age { get; set; }

}

When the end user saves the Customer (in runtime) providing a Name and not a City, XAF will again show the Validation Error listview however, this time there will be an extra Ignore action.

image

If the end user executes the Close action, the object will not be saved however if he executes the Ignore action it will be saved. In both cases in the detailview there will be a warning action by the City textbox to draw the end user’s attention.

image

Creating an Information rule

XAF allows you to create validation rules even at runtime using the Model Editor. So let’s say we want to just inform the end user, if he enters a Customer Age not between 10 and 60. This means that we need to use a RuleRangeField rule as illustrated in the next two images.

image

image

When the end user saves a Customer (in runtime) with an invalid Age range, XAF will not display the Validation Error listview at all. However, it will display an Information icon to draw his/her attention to the Age property.

image

Where to see more examples?

You may explore our FeatureCenter application located in your C:\Users\Public\Documents\DXperience 13.2 Demos\eXpressApp Framework\FeatureCenter\CS folder to see a few rules in action.

What about the web?

The validation module as a platform agnostic module works similarly for the web.

image

The soft validation is in its first release (CTP), thus we look forward to your feedback as we get it ready for it’s official release. Let me know what you think.

6 comment(s)
James Zhong
James Zhong

Thanks for blogging soft validation feature!

My question: Is there any way to configure if warning/information validation will be shown to end users? Some end users need the soft validation rules, but others don't need them (too much information is annoying, especially in detail views in Web).

27 November, 2013
Konstantin B (DevExpress)
Konstantin B (DevExpress)

James, you can disable all warning and info rules via the ValidationModule.IgnoreWarningAndInformationRules property.

27 November, 2013
James Zhong
James Zhong

Konstantin, thanks for your explanation! ValidationModule is now even better for more business scenarios.

27 November, 2013
Chris Royle (LOB)
Chris Royle (LOB)

Just a small observation from the screen-shots above, the warning/info message above states "'Age' must be within the '10' to '60' range."

My guess is that the validation error message templates have been reused to formulate this message - but as it's informational - the message would be more accurate if it read: "'Age' should be within the '10' to '60' range."

27 November, 2013
James Zhong
James Zhong

I agree with Chris. I also noticed the template message issue.

28 November, 2013
Krzysztof Krzyzsłof
Krzysztof Krzyzsłof

It would be really nice to have an option to enable/disable in each view the following functionality (enabled by default):

Show warnings and informations only if the property was modified or the current object is new.

(this would be easily done by adding a ModifiedProperties list in BaseObject class):

private List<string> modifiedProperties = new List<string>();

[NonPersistent]

[VisibleInListView(false)]

[VisibleInDetailView(false)]

[VisibleInLookupListView(false)]

[Appearance("ModifiedPropertiesAlwaysHide", Visibility = ViewItemVisibility.Hide)]

public IList<string> ModifiedProperties {

get { return modifiedProperties; }

}

protected override void OnChanged(string propertyName, object oldValue, object newValue) {

           base.OnChanged(propertyName, oldValue, newValue);

           if (!string.IsNullOrEmpty(propertyName) && !modifiedProperties.Contains(propertyName))

               modifiedProperties.Add(propertyName);

}

protected override void OnSaved() {

           base.OnSaved();

           modifiedProperties.Clear();

}

13 March, 2014

Please login or register to post comments.