This question is locked. New answers and comments are not allowed.
RadControls for Silverlight, v.2011.3.1220.1040 (Dev)
My application uses the following tables: Customers and Logins (One Customer can have many user accounts (logins)).
I am using RadDataForm to edit the Logins collection of the customer. The user is allowed to add/delete/update logins to the customer account and finally click Save to save (datacontext.SubmitChanges) the changes.
When the user edits/adds a login record and clicks the 'Save' button the application throws an InvalidOperationException stating that the login record is being edited and need to call Commit or CancelEdit before submitting.
I added code to check if there is any login that is being edited and handle it.
private
void
SaveButton_Click(
object
sender, RoutedEventArgs e)
{
bool
canSave =
true
;
canSave = HandlePendingEdit();
if
(canSave)
{
_context.SubmitChanges();
}
}
private
bool
HandlePendingEdit()
{
bool
canSave =
true
;
Login login =
null
;
IEditableCollectionView logins = (IEditableCollectionView)
this
.LoginsDataForm.ItemsSource;
if
(logins.IsAddingNew)
{
login = logins.CurrentAddItem
as
Login;
}
else
if
(logins.IsEditingItem)
{
login = logins.CurrentEditItem
as
Login;
}
if
(login !=
null
)
{
List<System.ComponentModel.DataAnnotations.ValidationResult> validationResults =
new
List<System.ComponentModel.DataAnnotations.ValidationResult>();
// check if the currently being edited item has errors
if
(!Validator.TryValidateObject(login,
new
ValidationContext(login,
null
,
null
), validationResults))
// show the errors and cancel save
{
canSave =
false
;
foreach
(var item
in
validationResults)
{
login.ValidationErrors.Add(item);
}
}
else
// No errors - commit the edit and procedd with the save
{
this
.LoginsDataForm.CommitEdit();
}
}
return
canSave;
}
This works ok except for the following:
The LoginId field is a required field.
When the user clicks ok in the RadDataForm without entering a value in the Login field, the form shows the validation error in the control and also in the validation summary.
But when the user clicks 'Save' without hitting OK first, then my code that triggers the validation causes the error to be shown in the Login input control (TextBox) but not in the ValidationSummary.
I have a sample (project and the script to create the two sample tables) to reproduce this behavior. I am not able to attach the project files though.
Steps to reproduce:
1. Click Edit icon in the RadDataForm
2. clear the Login data field
3. Click Ok to end the edit
4. You should see the validation error (both in textbox and validation summary) - attached screenshot - Validation Summary and textbox error.png
5. Cancel Edit
6. Click Edit again
7. Clear Login data field
8. Click 'Save' button at the top without clicking Ok/Cancel of RadDataForm
9. The error message is shown only in the text box not in the Validation Summary - attached screenshot - Textbox only error.png
What am I doing wrong / missing? I want to show the validation error in the validation summary in both scenarios.
Is there a better way to handle the scenario of the user clicking the Save button while in edit mode?
Thanks for your time
Please let me know if it will be helpful if I sent my sample project...