Custom Validation of a Computed Property
-
6 พฤษภาคม 2555 9:35
Hi I have searched this, but can't find a solution :( Please help!
Ok so my problem is that I can't get the validation rule to fire! The calculation of the number of SatelliteFarmers works...but my validation never fires. Basically I just need to call the validate method after everytime the NumberSatFarmers_Compute() method is called...but i have no idea of how to do that :(
here the code is;
public partial class Mentor
{
public Int32 count;
partial void NumberSatFarmers_Compute(ref int result)
{
count = (from farmer in SatelliteFarmers
where farmer.Mentor.Id == this.Id
select farmer).Count();
result = count;
}
partial void NumberSatFarmers_Validate(EntityValidationResultsBuilder results)
{
if (this.NumberSatFarmers>3)
results.AddPropertyError("<Error-Message>");
}
}
- แก้ไขโดย i8aNooB 6 พฤษภาคม 2555 9:38
ตอบทั้งหมด
-
6 พฤษภาคม 2555 10:48
I Don't Know will it work or not as i had not tried but you can try this thing,
You can call the Validate Sub Routine after assigning the value for NumberSatFarmer i.e after result = count.
Sharad Soni
-
6 พฤษภาคม 2555 11:00yes, but how do I call it in C# code in lightswitch? Cause the method is looking for the parameter (EntityValidationResultsBuilder results) so i dont know how I am meant to call it....
-
6 พฤษภาคม 2555 11:33
It seems that your app doesn't have a callback subscribed to your method.
Can I suggest renaming your method to Mentor_Validate() instead? This means that the validation will be at the record level rather than the field level, but I think from memory that the validators in LightSwitch are record level by default.
Happy to be told I'm wrong of course. :)
Jeremy Huppatz
Managing Consultant
Solitaire SystemsIf you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.
-
7 พฤษภาคม 2555 5:58
Hey Jeremy,
Unfortunately it didn't work...the validation still does not fire.
Here a screenshot is;
-
7 พฤษภาคม 2555 7:36
Hey I8aNoob (nice nickname, didn't get it at first but really funny :-))
The reason why your validation doesn't fire is because validation only is triggered if a property on the actual entity (Mentor) changes. In your case, Mentor doesn't change in itself, but the number of child entities change (SatFarmers).
I found a small workaround which works with a couple of glitches but does what you need. You can add a random property on your Mentor ("RandomBool"). Whenever the NumberSatFarmers_Computer method is called, change the value of this random property if a validation error should be thrown. This will trigger a re-validation (because you changed something on the Mentor-entity) and your validation error will be shown...
In code (sample, adjust to your needs)
partial void Tab2ItemsLength_Compute(ref int result) { // Set result to the desired field value result = this.Table2Items.Count(); if(!this.isValid()) this.SomeRandomBool = !this.SomeRandomBool; } partial void Tab2ItemsLength_Validate(EntityValidationResultsBuilder results) { if(!this.isValid()) { results.AddPropertyError("Whoa momma, too many farmers!"); } } private bool isValid() { return (this.Tab2ItemsLength <= 2); }
However...
I'm not really agreeing with your design here. In your setup, (after modifications above) I can't add a more than 5 sattelite farmers to a mentor because of the validation on your computed property. However, I can create and add as many sattelite farmers to one mentor from the Satelite farmers list-detail screen. IMHO, it would be best to move the business logic about the maximum number of sattelite farmers per mentor, to the sattelite farmer entity instead. (SatteliteFarmer_Created / _Edited / ... methods).
Keep rocking LS!
Jan
It's your story - time to switch on the innovation. || About me || LightSwitch blog
- ทำเครื่องหมายเป็นคำตอบโดย i8aNooB 7 พฤษภาคม 2555 11:14
-
7 พฤษภาคม 2555 11:15
Thanks Jan,
Used ur advice and made the validation in SattelliteFarmer and it works now. I understand how the validation rules work now. Thanks :)
-
7 พฤษภาคม 2555 12:08
Ur welcome...
Once again, great nickname! :D
Keep rocking LS!
It's your story - time to switch on the innovation. || About me || LightSwitch blog