Ticket T284797
Visible to All Users
Duplicate

We have closed this ticket because another page addresses its subject:

Validation - Check validation rules when input focus changes

Displaying validation errors

created 10 years ago (modified 10 years ago)

I am using the last solution described in this problem  to read in a product price.
and my code is

C#
[VisibleInDetailView(true)] [VisibleInListView(true)] [ImmediatePostData] [NotMapped] [DisplayName("Product")] public virtual Product DisplayProduct { get { return Product; } set { Product = value; if (Product != null) { var os = ObjectSpace; if (SalesHeader.PriceList == null) { throw new ExceptionDataValidation("uh uh price list is not entered"); } var priceList = os.FindObject<PriceList>(CriteriaOperator.Parse("[Id]=? ", SalesHeader.PriceList.Id)); if (priceList == null) { throw new ExceptionDataValidation("uh uh price list is not found"); } Description = Product.Description; TaxRate = os.FindObject<TaxRate>(CriteriaOperator.Parse("[Id]=? ", HandyDefaults.DefaultTaxRate(os).Id)); if (SalesHeader.PriceList.PriceListType == PriceListTypeEnum.DiscountFromRetailPrice) { Price = Product.RetailPrice * (100 - priceList.Percentage) / 100; } else { Price = Product.StandardCost * (100 + priceList.Percentage) / 100; } os.SetModified(this); } } }

My problem is that the error displays as " An exception has been thrown by the target of an invocation"

How do I get the desired exception message to be displayed?

I don't think validation rules will work because they only get checked on Saving.

Answers approved by DevExpress Support

created 10 years ago

Hello Kirsten,

It is not a good idea to throw exceptions from property setters be you in the XAF application context or in the non-XAF one, because these exceptions are caught by the standard WinForms binding mechanism.
That said, I suggest you either implement your validation logic in the IXafEntityObject.OnSaving method OR in the getter of the protected property marked with the RuleFromBoolPropertyAttribute.
In the former case, you will need to throw ValidationException instead of the ExceptionDataValidation one to allow XAF to automatically recognize it and nicely present in the UI.

    Comments (2)

      thanks Dennis, however your solution is for when the user attempts to save the record.
      I want the warning to occur when the user attempts to tab out of the product field.  Do you have a suggestion for that?

      Dennis Garavsky (DevExpress) 10 years ago

        Sure, we have described possible solutions for this scenario in the How to perform validation when some event is executed (e.g. editor loses focus) ticket. I hope this helps.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.