C
C#2y ago
br4kejet

✅ In WPF using a custom dialog service, how would i "validate" the dialog data before closing it?

I have a NewUserWindow, whose data context is a NewUserViewModel and then i have a dialog service that is responsible for creating users (Task<string> CreateUserAsync(string defaultUsername = null)) being the new user's name (or null if they clicked "Cancel"). But when the user clicks "OK" to create the user, i want to ensure the user doesn't already exist before the dialog closes (if the user exists, don't close the dialog). Is there a good way to do this, without passing a list of existing users to my dialog service (which would look like Task<string> CreateUserAsync(ObservableCollection<string> existingUsers, string defaultUsername = null))?
3 Replies
Zendist
Zendist2y ago
Do you have any uniqueness constraints in your backing data store? I.e. if you use SQL, do you have an index that ensures one cannot reuse the same username? If so, you could rely on a duplicate key exception and catch it to stop the dialog from closing if the user already exists?
br4kejet
br4kejetOP2y ago
That was kinda just an example, i was trying to achieve a way to "validate" the state of the dialog's data context before closing it but in order to validate it, it requires access to 1 or more other view models, in the example case UserManagerViewModel which contains that observable collection of existing users This is what i have atm:
public async void CreateUserAction() {
IUserDialogService service = IoC.Instance.Provide<IUserDialogService>();
NewUserDialogResult result = await service.CreateUserAsync(this.Users, "User " + this.Users.Count);
if (result.Result) {
this.Users.Add(new UserViewModel(this) {Username = result.ViewModel.Username});
}
}
public async void CreateUserAction() {
IUserDialogService service = IoC.Instance.Provide<IUserDialogService>();
NewUserDialogResult result = await service.CreateUserAsync(this.Users, "User " + this.Users.Count);
if (result.Result) {
this.Users.Add(new UserViewModel(this) {Username = result.ViewModel.Username});
}
}
just not really sure if this is a good way of doing it, because i have to pass the existing users to the dialog service
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server