Ejemplo n.º 1
0
 /// <summary>
 /// Gets a string that indicates the status of the specified field as a CSS class. This will include
 /// some combination of "modified", "valid", or "invalid", depending on the status of the field.
 /// </summary>
 /// <param name="editContext">The <see cref="EditContext"/>.</param>
 /// <param name="accessor">An identifier for the field.</param>
 /// <returns>A string that indicates the status of the field.</returns>
 public static string FieldCssClass <TField>(this EditContext editContext, Expression <Func <TField> > accessor)
 => FieldCssClass(editContext, FieldIdentifier.Create(accessor));
Ejemplo n.º 2
0
        public void CanUseEmptyFieldName()
        {
            var fieldIdentifier = new FieldIdentifier(new object(), string.Empty);

            Assert.Equal(string.Empty, fieldIdentifier.FieldName);
        }
Ejemplo n.º 3
0
 public FieldState(FieldIdentifier fieldIdentifier)
 {
     _fieldIdentifier = fieldIdentifier;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Gets the validation messages within this <see cref="ValidationMessageStore"/> for the specified field.
 ///
 /// To get the validation messages across all validation message stores, use <see cref="EditContext.GetValidationMessages(FieldIdentifier)"/> instead
 /// </summary>
 /// <param name="accessor">The identifier for the field.</param>
 /// <returns>The validation messages for the specified field within this <see cref="ValidationMessageStore"/>.</returns>
 public IEnumerable <string> this[Expression <Func <object> > accessor]
 => this[FieldIdentifier.Create(accessor)];
Ejemplo n.º 5
0
 /// <summary>
 /// Removes all messages within this <see cref="ValidationMessageStore"/> for the specified field.
 /// </summary>
 /// <param name="accessor">Identifies the field for which to remove the messages.</param>
 public void Clear(Expression <Func <object> > accessor)
 => Clear(FieldIdentifier.Create(accessor));
Ejemplo n.º 6
0
 /// <summary>
 /// Adds the messages from the specified collection for the specified field.
 /// </summary>
 /// <param name="accessor">Identifies the field for which to add the messages.</param>
 /// <param name="messages">The validation messages to be added.</param>
 public void Add(Expression <Func <object> > accessor, IEnumerable <string> messages)
 => Add(FieldIdentifier.Create(accessor), messages);
Ejemplo n.º 7
0
 /// <summary>
 /// Gets the validation messages within this <see cref="ValidationMessageStore"/> for the specified field.
 ///
 /// To get the validation messages across all validation message stores, use <see cref="EditContext.GetValidationMessages(FieldIdentifier)"/> instead
 /// </summary>
 /// <param name="fieldIdentifier">The identifier for the field.</param>
 /// <returns>The validation messages for the specified field within this <see cref="ValidationMessageStore"/>.</returns>
 public IEnumerable <string> this[FieldIdentifier fieldIdentifier]
 => _messages.TryGetValue(fieldIdentifier, out var messages) ? messages : Array.Empty <string>();
 /// <summary>
 /// Determines whether the specified fields in this <see cref="EditContext"/> has been modified.
 /// </summary>
 /// <param name="editContext">The <see cref="EditContext"/>.</param>
 /// <param name="accessor">Identifies the field whose current validation messages should be returned.</param>
 /// <returns>True if the field has been modified; otherwise false.</returns>
 public static bool IsModified(this EditContext editContext, Expression <Func <object> > accessor)
 => editContext.IsModified(FieldIdentifier.Create(accessor));
Ejemplo n.º 9
0
 /// <summary>
 /// Adds a validation message for the specified field.
 /// </summary>
 /// <param name="accessor">Identifies the field for which to add the message.</param>
 /// <param name="message">The validation message.</param>
 public void Add(Expression <Func <object> > accessor, string message)
 => Add(FieldIdentifier.Create(accessor), message);
 /// <summary>
 /// Gets the current validation messages for the specified field.
 ///
 /// This method does not perform validation itself. It only returns messages determined by previous validation actions.
 /// </summary>
 /// <param name="editContext">The <see cref="EditContext"/>.</param>
 /// <param name="accessor">Identifies the field whose current validation messages should be returned.</param>
 /// <returns>The current validation messages for the specified field.</returns>
 public static IEnumerable <string> GetValidationMessages(this EditContext editContext, Expression <Func <object> > accessor)
 => editContext.GetValidationMessages(FieldIdentifier.Create(accessor));
Ejemplo n.º 11
0
        /// <summary>
        /// Attempts to validate a single field or property of a form model or child object model.
        /// </summary>
        /// <param name="validator"></param>
        /// <param name="editContext"></param>
        /// <param name="fieldIdentifier"></param>
        /// <returns></returns>
        private async Task <ValidationResult> TryValidateField(IValidator validator, EditContext editContext, FieldIdentifier fieldIdentifier)
        {
            try
            {
                var vselector = new MemberNameValidatorSelector(new[] { fieldIdentifier.FieldName });
                var vctx      = CreateValidationContext(fieldIdentifier.Model, validatorSelector: vselector);
                return(await validator.ValidateAsync(vctx));
            }
            catch (Exception ex)
            {
                var msg = $"An unhandled exception occurred when validating field name: '{fieldIdentifier.FieldName}'";

                if (editContext.Model != fieldIdentifier.Model)
                {
                    msg += $" of a child object of type: {fieldIdentifier.Model.GetType()}";
                }

                msg += $" of <EditForm> model type: '{editContext.Model.GetType()}'";
                throw new UnhandledValidationException(msg, ex);
            }
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Removes all messages within this <see cref="ValidationMessageStore"/> for the specified field.
 /// </summary>
 /// <param name="store">The <see cref="ValidationMessageStore"/>.</param>
 /// <param name="accessor">Identifies the field for which to remove the messages.</param>
 public static void Clear(this ValidationMessageStore store, Expression <Func <object> > accessor)
 => store.Clear(FieldIdentifier.Create(accessor));
Ejemplo n.º 13
0
 /// <summary>
 /// Adds the messages from the specified collection for the specified field.
 /// </summary>
 /// <param name="store">The <see cref="ValidationMessageStore"/>.</param>
 /// <param name="accessor">Identifies the field for which to add the messages.</param>
 /// <param name="messages">The validation messages to be added.</param>
 public static void AddRange(this ValidationMessageStore store, Expression <Func <object> > accessor, IEnumerable <string> messages)
 => store.AddRange(FieldIdentifier.Create(accessor), messages);
Ejemplo n.º 14
0
 /// <summary>
 /// Adds a validation message for the specified field.
 /// </summary>
 /// <param name="store">The <see cref="ValidationMessageStore"/>.</param>
 /// <param name="accessor">Identifies the field for which to add the message.</param>
 /// <param name="message">The validation message.</param>
 public static void Add(this ValidationMessageStore store, Expression <Func <object> > accessor, string message)
 => store.Add(FieldIdentifier.Create(accessor), message);