// This function saves the field after a post, then shows the field form again.
        protected override DriverResult Editor(ContentPart part, HashedStringField field, IUpdateModel updater, dynamic shapeHelper)
        {
            if (AuthorizeEdit(part, field))
            {
                var    viewModel = new HashedStringFieldEditViewModel(field.PartFieldDefinition.Settings.GetModel <HashedStringFieldSettings>());
                string prefix    = GetPrefix(field, part);

                if (updater.TryUpdateModel(viewModel, prefix, null, null))
                {
                    if (!viewModel.Settings.Required && viewModel.ResetField)
                    {
                        // Resets to NULL Value, Salt and HashAlgorithm of the HashedString Field.
                        field.Value         = null;
                        field.HashAlgorithm = null;
                        field.Salt          = null;
                    }
                    else
                    {
                        if (Validate(viewModel, field, prefix, updater))
                        {
                            _secureFieldService.HashValue(field, viewModel.Value);
                        }
                        else
                        {
                            return(ContentShapeFromViewModel(part, field, ShapeTypeAuthorized, TemplateNameAuthorized, viewModel, shapeHelper));
                        }
                    }
                }
            }

            // Call the function to show the view.
            return(Editor(part, field, shapeHelper));
        }
        private HashedStringFieldEditViewModel CreateViewModel(HashedStringField field)
        {
            var settings = field.PartFieldDefinition.Settings.GetModel <HashedStringFieldSettings>();

            var vm = new HashedStringFieldEditViewModel(settings)
            {
                //Settings = settings,
                DisplayName = field.PartFieldDefinition.DisplayName,
                HasValue    = !string.IsNullOrWhiteSpace(field.Value)
            };

            return(vm);
        }
        private bool Validate(HashedStringFieldEditViewModel viewModel, HashedStringField field, string prefix, IUpdateModel updater)
        {
            var settings = field.PartFieldDefinition.Settings.GetModel <HashedStringFieldSettings>();

            if (settings.Required)
            {
                // If there is no previous Value and there is no Value in the viewModel.
                if (string.IsNullOrWhiteSpace(field.Value) && string.IsNullOrWhiteSpace(viewModel.Value))
                {
                    updater.AddModelError(prefix, T("The field {0} is mandatory.", T(field.DisplayName)));
                    return(false);
                }

                if (string.IsNullOrWhiteSpace(viewModel.Value) && !string.IsNullOrWhiteSpace(field.Value))
                {
                    // Keep the already saved Value without showing a error.
                    return(false);
                }
            }

            if (string.IsNullOrWhiteSpace(viewModel.Value) && !string.IsNullOrWhiteSpace(field.Value) && !viewModel.SaveIfEmpty)
            {
                // Keep the already saved Value without showing a error.
                return(false);
            }

            if (settings.ConfirmRequired && !string.Equals(viewModel.Value, viewModel.ConfirmValue))
            {
                updater.AddModelError(prefix, T("The value of the field {0} must match the Confirm value.", T(field.DisplayName), settings.Pattern));
                return(false);
            }

            if (!string.IsNullOrWhiteSpace(settings.Pattern) && !CheckPattern(viewModel.Value, settings.Pattern))
            {
                updater.AddModelError(prefix, T("The value of the field {0} is not valid." + Environment.NewLine + "Pattern: {1}", T(field.DisplayName), settings.Pattern));
                return(false);
            }

            return(true);
        }
 private DriverResult ContentShapeFromViewModel(ContentPart part, HashedStringField field, string shapeType, string templateName, HashedStringFieldEditViewModel viewModel, dynamic shapeHelper)
 {
     return(ContentShape(shapeType, GetDifferentiator(field, part),
                         () => {
         return shapeHelper.EditorTemplate(
             TemplateName: templateName,
             Model: viewModel,
             Prefix: GetPrefix(field, part));
     }));
 }