How to remove one or more fields from the DataForm.Validating() event in Silverlight 4? - silverlight

I have a data form that is bound to an object whose properties are decorated with System.ObjectModel.DataAnnotation attributes for validaton.
The problem I am facing is that some properties of this class are only conditionally needed and do not need to be validated. For example when an admin of the app decides to edit a user,
he or she may enter a password/password confirm/password question/password answer. Or he/she may entirely skip those properties.
So if the admin decides to enter any of those 4 fields, they all have to be present and the validation rules for all these fields have to be applied. However if the admin only wants to change the FirstName, LastName, Email, or whatever other arbitrary properties - the password related fields do not need to be validated.
Is there a way to "Exclude" them from the validation process?
this is a sample of the object I work with:
public class RegistrationData
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string PasswordConfirm { get; set; }
public string PasswordQuestion { get; set; }
public string PasswordAnswer { get; set; }
}
I have a DataForm called registrationForm in the Xaml and the error I get is in this code:
private void RegistrationButton_Click(object sender, RoutedEventArgs e)
{
if( this.registerForm.ValidateItem() )
{
//Does not pass validaton if the password properties are not filled in.
}
}
Any ideas on how to fix it?
I was thinking of using two DataForms... and split the user object in two, but that involves a lot of code...

I would recommend to use the INotifyDataError interface on your RegistrationData object.
public string LabelWrapper
{
get
{
return this.Label;
}
set
{
ValidateRequired("LabelWrapper", value, "Label required");
ValidateRegularExpression("LabelWrapper", value, #"^[\w-_ ]+$", "Characters allowed (a-z,A-Z,0-9,-,_, )");
this.Label = value;
this.RaisePropertyChanged("LabelWrapper");
}
}
public string DependentLabelWrapper
{
get
{
return this.DependentLabel;
}
set
{
if(LabelWrapper != null){
ValidateRequired("DependentLabelWrapper", value, "Label required");
ValidateRegularExpression("LabelWrapper", value, #"^[\w-_ ]+$", "Characters allowed (a-z,A-Z,0-9,-,_, )");
}
this.DependentLabel = value;
this.RaisePropertyChanged("DependentLabelWrapper");
}
}
I recommend you to look at this link http://blogs.msdn.com/b/nagasatish/archive/2009/03/22/datagrid-validation.aspx to learn more about different validation types.
Also MSDN has a nice explanation on how to use it
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifydataerrorinfo%28VS.95%29.aspx

This question brought me to another solution. I now use CustomValidation:
[CustomValidation(typeof(RegistrationDataValidation), "ValidatePassword")]
public class RegistrationData
{
public bool IsNewUser { get; set; }
... // other registration properties
}
public static class RegistrationDataValidation
{
public static ValidationResult ValidatePassword(MembershipServiceUser user, ValidationContext context)
{
if (user.IsNewUser && string.IsNullOrEmpty(user.Password))
{
return new ValidationResult("Password required");
}
return ValidationResult.Success;
}
}
I added a property IsNewUser which I set in the client when adding a new user. The custom validation method checks this property and executes the desired validation. I still have a RegularExpression Attribute on the password which will be validated as well.
In comparison to #Staindart's solution this is checked on the client synchronously.

The simplest and ugliest way would be to tap into the DataForm.ValidatingItem event. Like so:
void dfEditForm_ValidatingItem(object sender, System.ComponentModel.CancelEventArgs e)
{
foreach (ValidationSummaryItem item in dfEditForm.ValidationSummary.Errors)
{
if (item.Sources.Where(W => W.PropertyName != "myIgnoredPropertyName").Count() > 0)
e.Cancel = true;
}
}

Related

Winforms, EF Core, dropdown combobox bound to BindingSource. Is there a descent way of persisting newly added items? BindingList?

I am trying to build a Patient's edit form that will get Patient data from a database using EF Core and the DbContext derived AppDbContext.
On the same form there will be a dropdown combobox that will be displaying all the available insurances (fetched from the database).
What I want to achieve is the ability, the user to be able to select an existing insurance (which is obvious and easily achieved) or to add a new one by typing it into the combobox and this new entry should be selected as the patient's insurance from now on until the SaveChanges takes place and when the same patient is reopened for editing.
I use two BindingSources one for the patient itself (bsPatient) and one for the insurances list (bsInsurances).
I have the following two models (1:many relationship)
public class Insurance
{
public int Id { get; set; }
public string Name { get; set; } = String.Empty;
public virtual ObservableCollectionListSource<Person> Persons { get; } = new();
}
public class Person
{
public int Id { get; set; }
public string LastName { get; set; } = String.Empty;
public string FirstName { get; set; } = String.Empty;
public DateTime DOB { get; set; }
public int InsuranceId { get; set; }
public Insurance Insurance { get; set; } = null;
}
And this is the DbContext:
public class AppDbContext : DbContext
{
private const string DatabaseFileName = "MyPatientsDB.sqlite3";
public DbSet<Person> Persons { get; set; }
public DbSet<Insurance> Insurances { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder
.UseSqlite(#$"Data Source={DatabaseFileName}");
On the form there will be
public partial class PatientForm : Form
{
private AppDbContext _db = new();
private int _patientId = 0;
ObservableCollection<Insurance> _insurancesList = new();
public PatientForm(int patientId)
{
InitializeComponent();
_patientId = patientId;
}
protected async override void OnLoad(EventArgs e)
{
base.OnLoad(e);
await _db.Insurances.LoadAsync();
_insurancesList = new ObservableCollection<Insurance>(_db.Insurances.Local);
bsInsurances.DataSource = _insurancesList;
bsPatient.DataSource = await _db.Persons.FirstOrDefaultAsync(p => p.Id == _patientId);
}
protected async override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
bsInsurances.EndEdit();
if (cbInsurances.FindStringExact(cbInsurances.Text) == -1)
{
var newInsurance = new Insurance { Id = 0, Name = cbInsurances.Text };
_db.Insurances.Local.Add(newInsurance);
}
bsPatient.EndEdit();
await _db.SaveChangesAsync();
_db?.Dispose();
}
}
So far, I am able to save correctly the Insurance selection of the combobox when an already existing item is selected. The problem arises when the user inserts a new insurance entry into the combo textbox. This new entry can not be saved to the db and be displayed the next time the same patient is opened for editing.
I would be grateful if someone could point me towards which direction to follow to achieve this. I mean, while editing a patient's data how to be able to insert a new entry into the insurances combo and this new entry to be persisted into the db and be displayed and selected the next time the patient is opened for editing.
I think I've found a solution. I don't know if it is the best one but it seems to be working at least into my project. I am just referring it in case someone else has the same query.
Please if anyone has a better solution I would be grateful for his/her help.
protected async override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
bsInsurances.EndEdit();
if (cbInsurances.FindStringExact(cbInsurances.Text) == -1)
{
if (bsPatient != null && bsPatient.DataSource != null)
{
(bsPatient.DataSource as Person).Insurance = new Insurance() { Name = cbInsurances.Text as string };
}
}
bsPatient.EndEdit();
await _db.SaveChangesAsync();
_db?.Dispose();
}

How to bind a custom static object to Null

I have the following class:
public class UserInformation : IDataErrorInfo
{
public string Name { get; set; }
public string Surname { get; set; }
public string Identifier { get; set; }
public string Email { get; set; }
private const string matchEmailPattern = #"^[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$";
public override string ToString()
{
return Name + " " + Surname;
}
public string this[string columnName]
{
get
{
if (columnName == null) return string.Empty;
string result = string.Empty;
switch (columnName)
{
case "Name":
if (string.IsNullOrEmpty(Name))
result = "Name cannot be empty.";
break;
case "Surname":
if (string.IsNullOrEmpty(Surname))
result = "Surname cannot be empty.";
break;
case "Identifier":
if (string.IsNullOrEmpty(Identifier))
result = "Identifier cannot be empty.";
break;
case "Email":
if (string.IsNullOrEmpty(Email))
result = "Email cannot be empty.";
else
{
if (!Regex.IsMatch(Email, matchEmailPattern))
result = "Email format is invalid.";
}
break;
}
return result;
}
}
public string Error { get; private set; }
}
Notice that this is a validation class for WPF TextBoxes. In my Resources, I declare it as a static resource:
<custompackage:UserInformation x:Key="UserInformation" />
And I use it to validate some TextBox. That works fine. But whenever I want to use this attribute twice, it contains the values introduced the first time I use the static resource binding.
How can I empty this static resource object programmatically (I mean, when I decide to do so) so I can ensure next time I use it it will be null?
If you don't want the resource to be shared across its usage, set x:Shared to false on the resource. This will make sure that you will get new resource instance per its usage.
<custompackage:UserInformation x:Key="UserInformation"
x:Shared="false" />
you can derive from MarkupExtension:
public class UserInformation : MarkupExtension, IDataErrorInfo
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
// the rest of your Class
}
And then you don't need to declare it as a static resource you can simply use it like markup instead of {StaticResource xxx} you write {UserInformation} and because its a MarkupExtension it will instantiate a class and return it, solving you problem as its a new instance per usage.

Showing specific data based on the currently logged in user

I have a question that deals with the logistics of returning rows of data in a SQL database (Entity Framework) based on the user that is logged in; I have mainly focused on desktop C# applications and while making the switch to ASP.NET MVC 4 I'm having a bit of difficulty when it comes to figuring this out (I've searched around and none of the answers seem to provide exactly what I'm looking for):
I would like to use the authorization built in to ASP.NET (MVC4), and allow users to post data about their websites (site category, url, age, etc.) with a form, and have the form store the data (using Entity Framework) to a database (called PrimaryDomainsDb) that is tied to their Id in the UserProfile table.
When the user clicks a button to show their list of domains, how can I make the application pull their list of domains (relevant rows of data) while ignoring other users rows?
Again, I'm mainly looking for the logistics and concepts (using foreign keys, for example) and psuedocode rather than actually spoonfeeding me a bunch of code.
If anyone has any best practice ideas (i.e. link the UserProfile to the PrimaryDomainDb this way, and use EF to call the rows matching their Id this way to return the rows to the View), it would be much appreciated.
Some sample code:
I currently have my PrimaryDomain code first set up like this (this doesn't have the decorators that specify min/max length, etc.):
public class PrimaryDomain
{
public virtual int Id { get; set; }
public virtual string SiteName { get; set; }
public virtual string SiteURL { get; set; }
public virtual SitePlatforms SitePlatform { get; set; }
public virtual decimal? SiteDA { get; set; }
public virtual decimal? SitePA { get; set; }
public virtual string SiteAge { get; set; }
public virtual DateTime? LastStatusUpdate { get; set; }
public virtual string SiteIP { get; set; }
}
And I have a User class that is different than the one provided by ASP.NET WebSecurity, that looks like this: (also, I know that "password" should not be in string formatting, this is just for initial set-up purposes - and password should probably be removed altogether and handled by WebSecurity, I think).
public class User
{
public virtual int Id { get; set; }
public virtual string Username { get; set; }
public virtual string Password { get; set; }
public virtual string Email { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string MozAccessID { get; set; }
public virtual string MozKey { get; set; }
public virtual decimal AccuountBalance { get; set; }
public virtual PrivateProxy PrivateProxies { get; set; }
public virtual PrimaryDomain PrimaryDomains { get; set; }
}
When pulling the data for Views I run everything through a repository using direct injection:
public interface IUserDataSource
{
IQueryable<User> Users { get; }
IQueryable<PrimaryDomain> PrimaryDomains { get; }
void Save();
}
This is my UserDb class, which is fed in whenever the code calls for an IUserDataSource (via direct injection):
public class UserDb : DbContext, IUserDataSource
{
public UserDb()
: base("DefaultConnection")
{
}
public DbSet<User> Users { get; set; }
public DbSet<PrimaryDomain> PrimaryDomains { get; set; }
IQueryable<User> IUserDataSource.Users
{
get { return Users; }
}
IQueryable<PrimaryDomain> IUserDataSource.PrimaryDomains
{
get { return PrimaryDomains; }
}
void IUserDataSource.Save()
{
SaveChanges();
}
}
And this is, for example, how I would pass the PrimaryDomains model to the View:
public class NetworkController : Controller
{
//
// GET: /Network/
private IUserDataSource _db;
public NetworkController(IUserDataSource db)
{
_db = db;
}
public ActionResult ListDomains()
{
var allDomains = _db.PrimaryDomains;
return View(allDomains);
}
}
But instead of pulling the entire PrimaryDomains list from the data source, I would like to add a way to reference the currently logged in user id to make the application only show the domains for that specific user, not all domains, and when adding a new domain via the form to reference the User Id and add it into the table as well.
My original question may have caused some confusion as to what I'm trying to achieve; It's my fault for posing the wrong way of going about what I'm trying to do. After much research and learning, I've found that exactly what I'm looking for is a multi-tenant data architecture approach.
This is probably what you are looking for. If I understood you correctly you want to use WebSecurity to login or register users but you want to use entity framework to store some user-specific data. Code below connects WebSecurity tables with your database CodeFirst created using EntityFramework.
You create class below (from tutorial).
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
{
private static SimpleMembershipInitializer _initializer;
private static object _initializerLock = new object();
private static bool _isInitialized;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Ensure ASP.NET Simple Membership is initialized only once per app start
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
}
private class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
try
{
if(!WebSecurity.Initialized)
WebSecurity.InitializeDatabaseConnection("ConnectionString", "DbUsers", "UserId", "Email", autoCreateTables: true);
}
catch (Exception ex)
{
throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
}
}
}
}
It creates necessary tables for registering and logging your users. The magic is in second, third and fourth parameter. It is respectively table, userId column and userName column from YOUR database that you can create by EntityFramework. WebSecurity uses that table along with other self-generated tables to manage your users and let them register, login and so on.
Then in your code first you simply create table
public class DbUser
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[MaxLength(40)]
public string Email { get; set; }
[MinLength(3)]
[MaxLength(30)]
[Required]
public string FirstName { get; set; }
[MinLength(3)]
[MaxLength(50)]
[Required]
public string LastName { get; set; }
}
Then you can simply query data from controller. In example below I use UserId stored by WebSecurity membership to retrieve account info from database.
public ActionResult AccountInfo()
{
if (FormsAuthentication.CookiesSupported == true && Request.Cookies[FormsAuthentication.FormsCookieName] != null)
{
var userId = WebSecurity.CurrentUserId;
var userInfo = context.Users.FirstOrDefault(x => x.UserId == userId);
userInfo.Password = "";
return View(userInfo);
}
else
{
ModelState.AddModelError("", "Wystąpił bląd autoryzacji, zaloguj się jeszcze raz.");
return RedirectToAction("Login", "Account");
}
}
EDIT:
Regarding your edited question as I understand besides the fact that you need to integrate WebSecurity with EF as above (I also forgot to mention that after creating InitializeSimpleMmebershipAttribute class as above you need to decorate your controller with that attribute) you also have problems with implementing generic repository. If that line is a problem:
var allDomains = _db.PrimaryDomains;
Then i suggest to read this article about implementing generic repository:
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
If you want thing realy simple all you need is just add to your interface method
GetDomainByUserId(int userId)
and just implement that interface like that:
public class UserDb : DbContext, IUserDataSource
{
public UserDb()
: base("DefaultConnection")
{
}
public DbSet<User> Users { get; set; }
public DbSet<PrimaryDomain> PrimaryDomains { get; set; }
IQueryable<User> IUserDataSource.Users
{
get { return Users; }
}
IQueryable<PrimaryDomain> IUserDataSource.PrimaryDomains
{
get { return PrimaryDomains; }
}
IQueryable<PrimaryDomain> GetDomainByUserId(int userId)
{
return PrimaryDomains.Where(x => x.Id == userId).ToQueryable();
}
void IUserDataSource.Save()
{
SaveChanges();
}
}
But this is very bad approach and I strongly recommend reading that article.

Trouble binding entity framework 5 object with windows control

I'm using Entity Framework for the first time (database first, entity framework 5) in a windows forms application (vs2010, .net 4). I am having trouble with the binding between my entity object and the windows forms controls. I have textbox, datetimepicker and combobox controls. When I open a window with the bound controls, the correct data is displayed in the controls. However, when I change the value in one of my controls and tab off the control, the value reverts to the original value in the control as if the value is not getting pushed to the object. Here are code exerpts:
My entity object:
namespace Entities
{
using System;
using System.Collections.Generic;
public partial class ExternalDocument
{
public int ExternalDocumentID { get; set; }
public bool Active { get; set; }
public bool Closed { get; set; }
public Nullable<int> CompanyID { get; set; }
public Nullable<int> ContactID { get; set; }
public string DocumentNbr { get; set; }
public Nullable<System.DateTime> DocumentDate { get; set; }
public Nullable<System.DateTime> DateReceived { get; set; }
public virtual Company Company { get; set; }
public virtual Contact Contact { get; set; }
}
}
The data binding:
private void SetDataBindings()
{
LoadComboBoxValues();
this.textDocumentNbr.DataBindings.Add("Text", this.document, "DocumentNbr");
this.textDocumentNbr.Leave += new EventHandler(textDocumentNbr_Leave);
this.dateDocument.DataBindings.Add(new Binding("Value", this.document, "DocumentDate"));
this.dateReceived.DataBindings.Add("Value", this.document, "DateReceived");
this.comboCompanyID.DataBindings.Add("SelectedValue", document, "CompanyID");
}
I have wondered if there is an entity framework error when the object property is set but I have not been able to figure out a good way to trap any such errors. My entity framework object does not have the On< PropertyName >Changing methods that are created for earlier versions of entity framework. I have been trying to trap errors when focus leaves the control but think this can't be the best method:
private void dateDocument_Leave(object sender, EventArgs e)
{
string errorString = this.entitiesController.GetValidationErrors();
this.errorDocumentDate.SetError(this.dateDocument, errorString);
}
public string GetValidationErrors()
{
string errorString = "";
List<DbEntityValidationResult> errorList = (List<DbEntityValidationResult>)this.finesse2Context.GetValidationErrors();
if (errorList.Count > 0)
{
foreach(var eve in errorList)
{
errorString += "Entity of type " + eve.Entry.Entity.GetType().Name + " in state" + eve.Entry.State + " has the following validation errors:"; ;
foreach (var ve in eve.ValidationErrors)
{
errorString += "- Property: " + ve.PropertyName + " Error: " + ve.ErrorMessage;
}
}
}
return errorString;
}
Any help would be appreciated. Thanks!
It turns out that an exception is received when the binding pushes a non nullable value to a nullable property in the object unless "formattingEnabled" is specified on the binding.
So, binding like this works:
this.dateDocument.DataBindings.Add(new Binding("Value", this.document, "DocumentDate", true));
whereas this does not:
this.dateDocument.DataBindings.Add(new Binding("Value", this.document, "DocumentDate"));
I am still unclear how I could trap that type of error since the Binding simply catches the error and replaces the value in the control with the original value.

DomainContext sometimes still HasChanges after SubmitChanges completes

I have a very simple server model that includes a parent entity with a [Composition] list of child entities. In my client, I have 2 functions. One function removes all the child entities from the parent and the other removes all and also edits a property on the parent entity.
When I simply remove all child entities and SubmitChanges(), all is well.
When I remove all child entities and edit the parent and SubmitChanges(), there are still pending changes (HasChanges == true) when the SubmitChanges() callback is fired.
I am using Silveright 4 RTM and RIA Services 1.0 RTM.
Any ideas what is going on here?
Here are the server entities:
public class RegionDto
{
public RegionDto()
{
Cities = new List<CityDto>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
[Include]
[Composition]
[Association("RegionDto_CityDto", "Id", "RegionId")]
public List<CityDto> Cities { get; set; }
}
public class CityDto
{
[Key]
public int Id { get; set; }
public int RegionId { get; set; }
public string Name { get; set; }
}
And here is the client code:
public static class CState
{
private static RegionDomainContext _domainContext;
public static RegionDomainContext DomainContext
{
get
{
if (_domainContext == null)
{
_domainContext = new RegionDomainContext();
}
return _domainContext;
}
}
public static void SaveChanges()
{
DomainContext.SubmitChanges(op =>
{
if (DomainContext.HasChanges && !DomainContext.IsSubmitting)
{
var w = new ChildWindow();
w.Content = "The DomainContext still has unsaved changes.";
w.Show();
}
}, null);
}
}
public partial class MainPage : UserControl
{
private void ClearCitiesEditRegion(object sender, RoutedEventArgs e)
{
var region = (RegionDto)regionList.SelectedItem;
if (region != null)
{
region.Name += "*";
while (region.Cities.Count > 0)
{
region.Cities.Remove(region.Cities.First());
}
CState.SaveChanges();
}
}
private void ClearCities(object sender, RoutedEventArgs e)
{
var region = (RegionDto)regionList.SelectedItem;
if (region != null)
{
while (region.Cities.Count > 0)
{
region.Cities.Remove(region.Cities.First());
}
CState.SaveChanges();
}
}
}
When you run this code the ChildWindow is only shown when you the ClearCitiesEditRegion() method is called. The only difference between this and the ClearCities() method is the line where I edit the region.Name property.
You can also download a sample project that reproduces this here: http://dl.dropbox.com/u/2393192/RIA_Services_Problem.zip
I received an answer to this on the Silverlight forums. Apparently this is a bug in RIA Service 1.0. The following is Matthew's response on the Silverlight forums.
Yes, I've confirmed this is a bug.
Thanks for reporting it and providing
the repro. As you discovered, the bug
will only repro in composition
scenarios where the parent has been
modified in addition to one or more
children. The workaround is to do an
explicit AcceptChanges if the submit
was successful. For example, here is
the code you'd write in a submit
callback:
if (!submitOperation.HasError)
{
((IChangeTracking)ctxt.EntityContainer).AcceptChanges();
}
This will accept all changes and reset
change state correctly.

Resources