/// <summary>
        /// This class implements Validation rule to validate required text.
        /// </summary>
        /// <remarks>This validation is rule used by <see cref="T:NeuroSpeech.UIAtoms.Controls.AtomTextBox"/>
        /// control to validate property <see cref="P:System.Windows.Controls.TextElement.Text"/>.
        /// You can use this rule to validate any string property on the control. It returns
        /// <see cref="T:NeuroSpeech.UIAtoms.Validations.AtomValidationError"/> if the string is empty.
        ///
        /// The child classes implement more specific validation by overriding method
        /// <see cref="M:NeuroSpeech.UIAtoms.Validations.AtomStringValidationRule.IsStringValid"/>
        /// to do specific business logic validation.
        /// </remarks>
        /// <param name="view">Control to validate</param>
        /// <param name="property">Dependency Property to validate</param>
        /// <param name="value">Text Value to validate</param>
        /// <returns></returns>
        public override AtomValidationError Validate(View e, BindableProperty property, object value)
        {
            //AtomTrace.WriteLine("AtomTextBox.Validate called...");

            string text       = value as string;
            bool   isRequired = AtomForm.GetIsRequired(e);

            if (isRequired && string.IsNullOrEmpty(text))
            {
                return new AtomValidationError
                       {
                           Property = property,
                           Message  = AtomForm.GetMissingValueMessage(e),
                           Source   = e
                       }
            }
            ;

            int min = GetMinimumLength(e);

            if (min != -1 && min != 0)
            {
                if (text == null || text.Length < min)
                {
                    return(new AtomValidationError
                    {
                        Property = property,
                        Message = string.Format(AtomStringValidationRule.GetMinimumLengthErrorMessage(e), min),
                        Source = e
                    });
                }
            }

            int max = GetMaximumLength(e);

            if (max != -1 && max != 0 && max != int.MaxValue)
            {
                if (text.Length > max)
                {
                    return(new AtomValidationError
                    {
                        Property = property,
                        Message = string.Format(AtomStringValidationRule.GetMaximumLengthErrorMessage(e), min),
                        Source = e
                    });
                }
            }

            return(IsStringValid(value as string, property, e));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="p"></param>
        /// <param name="property"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        protected override AtomValidationError IsStringValid(string p, BindableProperty property, View e)
        {
            string regex = GetValidationRegEx(e);

            if (string.IsNullOrEmpty(regex) || Regex.IsMatch(p, regex))
            {
                return(null);
            }
            return(new AtomValidationError
            {
                Source = e,
                Property = property,
                Message = AtomForm.GetInvalidValueMessage(e)
            });
        }
Beispiel #3
0
     public override AtomValidationError Validate(View view, BindableProperty property, object value)
     {
         if (AtomForm.GetIsRequired(view))
         {
             if (value == null)
             {
                 return new AtomValidationError
                        {
                            Source   = view,
                            Property = property,
                            Message  = AtomForm.GetMissingValueMessage(view)
                        }
             }
             ;
         }
         return(null);
     }
 }
Beispiel #4
0
        internal static AtomValidationError Validate(View content)
        {
            var validator = AtomForm.GetValidator(content);

            if (validator == null)
            {
                return(null);
            }

            var rule = validator.ValidationRule;

            if (rule == null)
            {
                return(null);
            }

            object value = null;

            var property = validator.BindableProperty;

            if (property != null)
            {
                value = content.GetValue(property);
            }
            else
            {
                if (validator.Property == null)
                {
                    throw new ArgumentNullException($"Both Property and BindableProperty are null for {validator.GetType().FullName} for {content}");
                }
                PropertyInfo px = content.GetType().GetProperty(validator.Property);
                value = px.GetValue(content);
            }

            var error = validator.ValidationRule.Validate(content, property, value);

            AtomForm.SetError(content, error?.Message);
            return(error);
        }
Beispiel #5
0
     public override AtomValidationError Validate(View view, BindableProperty property, object value)
     {
         if (AtomForm.GetIsRequired(view))
         {
             var dt = DateTime.MinValue;
             if (value != null)
             {
                 dt = Convert.ToDateTime(value);
             }
             if (dt == DateTime.MinValue)
             {
                 return new AtomValidationError
                        {
                            Source   = view,
                            Property = property,
                            Message  = AtomForm.GetMissingValueMessage(view)
                        }
             }
             ;
         }
         return(null);
     }
 }