locked
Validation event work only once RRS feed

  • Question

  • I wrote a validation for some field like this:

    partial void Field_Validate(EntityValidationResultsBuilder results)
            { 
    if(condition...)
    this.field=0;
    }
    

    This code works but only once, on second try validation event don't fire.

    Then I added code for discard changes:

    this.DataWorkspace.OZONBGData.Fieldss_Single(this.IDField).Details.DiscardChanges();

    Now validation event works fine in debbuger but when I publish, validation works twice. Is there any way to make validation works all times when I enter values in datagrid?

    Thursday, November 8, 2012 11:46 AM

Answers

  • Hi Mickey,

    You need to populate 'results' with your validation errors otherwise Lightswitch will not know that validation failed. Please look at this page: http://msdn.microsoft.com/en-us/library/ff852065.aspx

    partial void Field_Validate(EntityValidationResultsBuilder results)
    {
        if (this.Field > ...)
        {
            results.AddPropertyError("Field cannot be ...");
        }
    
    }
    


    Dave Baker | Xpert360 blog | twitter : @xpert360 Opinions are my own. Please mark as answer if this helps solve your problem.

    • Proposed as answer by babloo1436 Saturday, November 10, 2012 4:27 AM
    • Marked as answer by Angie Xu Tuesday, November 13, 2012 12:06 AM
    Thursday, November 8, 2012 12:25 PM

All replies

  • Hi Mickey,

    You need to populate 'results' with your validation errors otherwise Lightswitch will not know that validation failed. Please look at this page: http://msdn.microsoft.com/en-us/library/ff852065.aspx

    partial void Field_Validate(EntityValidationResultsBuilder results)
    {
        if (this.Field > ...)
        {
            results.AddPropertyError("Field cannot be ...");
        }
    
    }
    


    Dave Baker | Xpert360 blog | twitter : @xpert360 Opinions are my own. Please mark as answer if this helps solve your problem.

    • Proposed as answer by babloo1436 Saturday, November 10, 2012 4:27 AM
    • Marked as answer by Angie Xu Tuesday, November 13, 2012 12:06 AM
    Thursday, November 8, 2012 12:25 PM
  • Thanks for your answer. I added 

    results.AddPropertyError("Field cannot be ...");

    but validation event executes once, again. Second time, when I enter valid value for field, validation error is still there. I set a breakpoint on this validation event, and only the first time this event occurred. 

    Thursday, November 8, 2012 12:56 PM
  • Validation won't fire unless the property value has changed (& for that the control must have lost focus). Could that be your problem? Are you doing exactly the same thing on the second attempt? Or could the control maybe still have focus?

    But Dave's right, if you don't have that line of code, LightSwitch won't display the validation failure to the user.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Thursday, November 8, 2012 1:39 PM
    Moderator
  • Not sure what your completed code looks like, but you should not need to discard changes to fire validation.  Just use the code as described by Xpert360 and it should work.
    Saturday, November 10, 2012 4:01 AM