Exemplo n.º 1
0
        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 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();
        }
Exemplo n.º 2
0
        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 results = ObjectValidator.Validate(product);

            foreach (var result in results)
            {
                //var firstMessage = results[0];
                MessageBox.Show(this, result.ErrorMessage, "Validation Failed",
                                MessageBoxButtons.OK);
                return;
            }
            ;

            Product      = product;
            DialogResult = DialogResult.OK;
            Close();
        }
Exemplo n.º 3
0
        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,
            };

            // Validate product
            try
            {
                ObjectValidator.Validate(product);
            } catch (ValidationException)
            {
                MessageBox.Show(this, "Product not valid.", "Error", MessageBoxButtons.OK);
                return;
            };

            Product      = product;
            DialogResult = DialogResult.OK;
            Close();
        }
Exemplo n.º 4
0
        private void OnSave(object sender, EventArgs e)
        {
            //Force validation of child controls
            if (!ValidateChildren())
            {
                return;
            }

            // Create movie - using object initializer syntax
            var movie = new Movie()
            {
                Name           = _txtName.Text,
                Description    = _txtDescription.Text,
                Length         = ConvertToLength(_txtLength),
                IsDiscontinued = _chkIsDiscontinued.Checked,
            };

            //Validate movie using IValidatableObject
            var errors = ObjectValidator.Validate(movie);

            if (errors.Count() > 0)
            {
                //Get first error
                DisplayError(errors.ElementAt(0).ErrorMessage);
                return;
            }
            ;

            //Return from form
            Movie        = movie;
            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,
            };


            ObjectValidator.Validate(product);

            Product      = product;
            DialogResult = DialogResult.OK;
            Close();
        }