private void OnSave(object sender, EventArgs e) { if (!ValidateChildren()) { return; } ; //var product = new Product(); //product.Id = Product?.Id ?? 0; //product.Name = _txtName.Text; //product.Description = _txtDescription.Text; //product.Price = GetPrice(_txtPrice); //product.IsDiscontinued = _chkDiscontinued.Checked; //Object initializer syntax var product = new Product() { Id = Product?.Id ?? 0, Name = _txtName.Text, Description = _txtDescription.Text, Price = GetPrice(_txtPrice), IsDiscontinued = _chkDiscontinued.Checked, }; //Add validation //var error = product.Validate(); //if (!String.IsNullOrEmpty(error)) //Using IValidatableObject if (!ObjectValidator.TryValidate(product, out var errors)) { //Show the error ShowError("Not valid", "Validation Error"); return; } ; Product = product; this.DialogResult = DialogResult.OK; Close(); }
private void OnSave(object sender, EventArgs e) { if (!ValidateChildren()) { return; } ; var product = new Product() { Id = Product?.Id ?? 0, Name = _txtName.Text, Description = _txtDescription.Text, Price = GetPrice(_txtPrice), IsDiscontinued = _chkDiscontinued.Checked, }; //TODO: Validate product var validationResults = new ObjectValidator().TryValidateFullobject(product); if (validationResults.Count() > 0) { var builder = new System.Text.StringBuilder(); foreach (var result in validationResults) { builder.AppendLine(result.ErrorMessage); } ; // Show error message MessageBox.Show(this, builder.ToString(), "Save failed", MessageBoxButtons.OK, MessageBoxIcon.Error); DialogResult = DialogResult.None; return; } ; Product = product; DialogResult = DialogResult.OK; Close(); }
private void OnSave(object sender, EventArgs e) { //Force validation of child controls if (!ValidateChildren()) { return; } // Create product - using object initializer syntax var product = new Product() { Name = _txtName.Text, Description = _txtDescription.Text, Price = ConvertToPrice(_txtPrice), IsDiscontinued = _chkIsDiscontinued.Checked, }; //Validate product using IValidatableObject //var message = product.Validate(); //if (!String.IsNullOrEmpty(message)) //{ // DisplayError(message); // return; //}; var errors = ObjectValidator.Validate(product); if (errors.Count() > 0) { //Get first error DisplayError(errors.ElementAt(0).ErrorMessage); return; } ; //Return from form Product = product; DialogResult = DialogResult.OK; Close(); }
private void OnSave(object sender, EventArgs e) { //Force validation of child controls if (!ValidateChildren()) { return; } // Create product - using object initializer syntax var product = new Product() { Name = _txtName.Text, Description = _txtDescription.Text, Price = ConvertToPrice(_txtPrice), IsDiscontinued = _checkIsDiscontinued.Checked, }; //Validate product using IValidatableObject var errors = ObjectValidator.TryValidate(product); if (errors.Count() > 0) { //Get first error DisplayError(errors.ElementAt(0).ErrorMessage); return; } ; // return; //} else // _errorProvider.SetError(_txtName, ""); //Return form form Product = product; DialogResult = DialogResult.OK; //DialogResult = DialogResult.None; Close(); }
private void OnSave(object sender, EventArgs e) { if (!ValidateChildren()) { return; } ; var product = new Product() { Id = Product?.Id ?? 0, Name = _txtName.Text, Description = _txtDescription.Text, Price = GetPrice(_txtPrice), IsDiscontinued = _chkDiscontinued.Checked, }; var results = ObjectValidator.TryValidateObject(product); Product = product; DialogResult = DialogResult.OK; Close(); }
private void OnSave(object sender, EventArgs e) { if (!ValidateChildren()) { return; } ; var product = new Product() { Id = Product?.Id ?? 0, Name = _txtName.Text, Description = _txtDescription.Text, Price = GetPrice(_txtPrice), IsDiscontinued = _chkDiscontinued.Checked, }; //TODO: Validate product var validationResults = ObjectValidator.TryValidateFullObject((System.ComponentModel.DataAnnotations.IValidatableObject)product); Product = product; DialogResult = DialogResult.OK; Close(); }