Validate Insert, Update and Delete based on flag
-
Friday, August 03, 2012 8:44 PM
Hello,
How can I validate Insert, Update or Delete based on Flag (Boolean) ?
E.g. Flag = "Y" is possible to save information, otherwise not.
The quick answer is: _validade, BUT when the Flag changes to "N" is not possible to save... BTW it's related to Invoices.
Any ideas?
Thanks in advance.
All Replies
-
Friday, August 03, 2012 9:02 PM
Sure, you can !
There are _Can_CRUD methods for all CRUD operations on entities.
E.g.:
partial void Customers_CanInsert(ref bool result)
{
}You can hook up here the build-in permission mechanism in lightSwitch in such a way you can specify which CRUD operations a user can do based on it's permissions.
The documentation will learn you more details.
paul van bladel
-
Friday, August 03, 2012 9:54 PM
Paul,
I'll go further on it.
But my problem is not related with user permissions, but with "data" permission.
Allow me:
E.g. Flag = "Y" is possible to save information, otherwise not.
"Flag" is a database entity.
Thanks again.
-
Saturday, August 04, 2012 6:06 AM
ok, that's a different story.
Let's elaborate for inserts: In the Customer_Inserting method you have access to the current entity where you can throw an exception if your condition is not met. Same goes for updates, inserts and deletes.
Another alternative is to hook up things in the Savechanges_executing method (on datasource level this time)
EntityChangeSet changes = dataService.Details.GetChanges(); foreach (var modifiedEntity in changes.ModifiedEntities) { } foreach (var insertedEntity in changes.AddedEntities) { }
//same for deletes...
And there is a third option (and probably the best), is to implement your logic in the Customers_Validate method !
partial void Customers_Validate(Customer entity, EntitySetValidationResultsBuilder results) { }
The advantage is that you don't need to throw exceptions because you can use the EntitySetValidationResultBuilder.
Furthermore the same logic is used client side (for free !)
I advice to use the 3rd option.
paul van bladel
- Edited by Paul Van Bladel Saturday, August 04, 2012 6:20 AM
-
Saturday, August 04, 2012 2:59 PM
Paul,
For sure, the third option is the best, and the client side should be used in my solution:
From Microsoft How-To:
Private Sub Customers_Validate _ (results As Microsoft.LightSwitch.Framework.Client.ScreenValidationResultsBuilder) If Me.DataWorkspace.NorthwindData.Details.HasChanges Then Dim changeSet As EntityChangeSet = _ Me.DataWorkspace.NorthwindData.Details.GetChanges() Dim entity As IEntityObject For Each entity In changeSet.DeletedEntities.OfType(Of Customer)() Dim cust As Customer = CType(entity, Customer) If cust.Country = "USA" Then entity.Details.DiscardChanges() results.AddScreenResult("Unable to remove this customer." & _ "Cannot delete customers that are located in the USA.", _ ValidationSeverity.Informational) End If Next End If End Sub
For Updates you can use <changeSet.ModifiedEntities>, and so on.
The Client Side is very important to me, because I have 2 screens related to Invoices: one to generate, and other to confirm the payment, where the Status is changed.
Using the Server Side, I'll never could change the Status...
Many thanks again.
- Marked As Answer by antoniobrito Saturday, August 04, 2012 3:01 PM

