/// <summary>
        /// Does validate.
        /// </summary>
        /// <param name="value">Value to validation.</param>
        /// <param name="culture">Culture info.</param>
        /// <param name="context">Cell validation context.</param>
        /// <returns>Validation result.</returns>
        public override ValidationResult Validate(object value,
                                                  CultureInfo culture,
                                                  CellValidationContext context)
        {
            bool isValid = true;

            string newValue = value as string;
            IVrpSolver solver = App.Current.Solver;
            NetworkDescription networkDescription = solver.NetworkDescription;

            if (newValue != null && networkDescription != null)
            {
                var wrapper = context.DataItem as RestrictionDataWrapper;

                ICollection<NetworkAttribute> networkAttributes = networkDescription.NetworkAttributes;
                foreach (NetworkAttribute attribute in networkAttributes)
                {
                    // If it is current attribute - find parameter to validate.
                    if (wrapper.Restriction.Name.Equals(attribute.Name,
                                                        StringComparison.OrdinalIgnoreCase))
                    {
                        Debug.Assert(null != attribute.Parameters);
                        NetworkAttributeParameter[] parameters = attribute.Parameters.ToArray();

                        int paramIndex = Parameters.GetIndex(context.Cell.FieldName);

                        // If parameter index was found.
                        if(paramIndex != -1)
                        {
                            var parameter = wrapper.Parameters[paramIndex];

                            // Get corresponding network attribute parameter
                            // and check that value can be converted to parameter type.
                            NetworkAttributeParameter param = parameters.FirstOrDefault(
                                x => x.Name == parameter.Name);

                            // If string is not empty or if parameter doesn't accept empty string -
                            // try to convert value.
                            if ((string)value != string.Empty || !param.IsEmptyStringValid)
                            {
                                try
                                {
                                    Convert.ChangeType(value, param.Type); // NOTE: ignore result
                                }
                                catch
                                {
                                    isValid = false;
                                }
                            }
                        }

                        break;
                    }
                }
            }

            return (isValid) ? ValidationResult.ValidResult :
                               new ValidationResult(false, App.Current.FindString("NotValidValueText"));
        }
 public CellValidatingEventArgs( 
   object value, 
   CultureInfo culture, 
   CellValidationContext context )
 {
   m_value = value;
   m_culture = culture;
   m_context = context;
 }
 public CellValidatingEventArgs(
     object value,
     CultureInfo culture,
     CellValidationContext context)
 {
     m_value   = value;
     m_culture = culture;
     m_context = context;
 }
        public override ValidationResult Validate(object value, CultureInfo culture, CellValidationContext context)
        {
            ValidationResult result = null;

            // not empty check
            bool isObjectEmpty = true;
            if (null != value)
                isObjectEmpty = string.IsNullOrEmpty(value.ToString().Trim()); // project name cannot consist only in blanks
            if (isObjectEmpty)
                result = new ValidationResult(false, (string)Application.Current.FindResource("ProjectNameValidationRuleIncorrectNameError"));

            if (null == result)
            {
                string name = value.ToString().Trim();
                if (-1 != name.IndexOfAny(new char[] {'\\', '/', '*', ';', ',', ':', '|', '"'}))
                    result = new ValidationResult(false, (string)Application.Current.FindResource("ProjectNameValidationRuleIncorrectNameError"));
                else
                {
                    ProjectsPage projectsPage = (ProjectsPage)App.Current.MainWindow.GetPage(PagePaths.ProjectsPagePath);
                    ProjectDataWrapper currentItem = (ProjectDataWrapper)projectsPage.XceedGrid.CurrentItem;

                    // check duplicate
                    ItemCollection wrappedCollection = projectsPage.XceedGrid.Items;
                    foreach (ProjectDataWrapper wrapper in wrappedCollection)
                    {
                        if (name.Equals(wrapper.Name, StringComparison.InvariantCultureIgnoreCase) && (wrapper != currentItem))
                        {
                            result = new ValidationResult(false, (string)App.Current.FindResource("ProjectNameValidationRuleDuplicateNameError"));
                            break; // NOTE: exit - error founded
                        }
                    }

                    if (null == result)
                    {   // normal length check
                        string fileName = name + ProjectConfiguration.FILE_EXTENSION; // NOTE: check only one file name,
                            // but real created two files: ProjectConfiguration.FILE_EXTENSION and DatabaseEngine.DATABASE_EXTENSION
                        string filePath = null;
                        try
                        {
                            filePath = _GetDatabaseAbsolutPath(App.Current.ProjectCatalog.FolderPath, fileName);
                        }
                        catch
                        {
                        }

                        // valid name check
                        if (!FileHelpers.IsFileNameCorrect(filePath) || !FileHelpers.ValidateFilepath(filePath))
                            result = new ValidationResult(false, (string)Application.Current.FindResource("ProjectNameValidationRuleIncorrectNameError"));
                    }
                }
            }

            return (null == result)? ValidationResult.ValidResult : result;
        }
    public override ValidationResult Validate( object value, CultureInfo culture, CellValidationContext context )
    {
      // Get the DataItem from the context and cast it to a DataRow
      DataRowView dataRowView = context.DataItem as DataRowView;

      // Convert the value to a long to make sure it is numerical.
      // When the value is not numerical, then an InvalidFormatException will be thrown.
      // We let it pass unhandled to demonstrate that an exception can be thrown when validating
      // and the grid will handle it nicely.
      long id = Convert.ToInt64( value, CultureInfo.CurrentCulture );

      // Try to find another row with the same ID
      System.Data.DataRow[] existingRows = dataRowView.Row.Table.Select( context.Cell.FieldName + "=" + id.ToString( CultureInfo.InvariantCulture ) );

      // If a row is found, we return an error
      if( ( existingRows.Length != 0 ) && ( existingRows[ 0 ] != dataRowView.Row ) )
        return new ValidationResult( false, "The value must be unique" );

      // If no row was found, we return a ValidResult
      return ValidationResult.ValidResult;
    }
        public override System.Windows.Controls.ValidationResult Validate(object value, System.Globalization.CultureInfo culture, Xceed.Wpf.DataGrid.CellValidationContext context)
        {
            if (value == null)
            {
                return(ValidationResult.ValidResult);
            }

            Cell cell = context.Cell;

            OrderQuantity checkingObject = cell.DataContext as OrderQuantity;
            double        minValue       = checkingObject.MinValue;
            double        maxValue       = checkingObject.MaxValue;

            if (value is string)
            {
                value = double.Parse((string)value);
            }
            if (cell.FieldName == OrderQuantity.PROP_NAME_MinValue)
            {
                minValue = (double)value;
            }
            else if (cell.FieldName == OrderQuantity.PROP_NAME_MaxValue)
            {
                maxValue = (double)value;
            }
            else
            {
                System.Diagnostics.Debug.Assert(false);
            }

            string newValue = value.ToString();

            if (minValue > maxValue)
            {
                return(new ValidationResult(false, (string)App.Current.FindResource("NotValidRangeText")));
            }

            return(ValidationResult.ValidResult);
        }
        public override System.Windows.Controls.ValidationResult Validate(object value, System.Globalization.CultureInfo culture, Xceed.Wpf.DataGrid.CellValidationContext context)
        {
            Cell cell = context.Cell;

            OrderCategory checkingObject = cell.DataContext as OrderCategory;

            if (checkingObject != null)
            {
                if (value != null)
                {
                    string newValue = value.ToString();

                    foreach (OrderCategory element in SymbologyManager.OrderCategories)
                    {
                        if (!element.Equals(checkingObject) && element.Value != null && element.Value.Equals(newValue))
                        {
                            return(new ValidationResult(false, (string)App.Current.FindResource("ValueValidationRuleText")));
                        }
                    }
                }
            }

            return(ValidationResult.ValidResult);
        }
        public override ValidationResult Validate(object value, CultureInfo culture, CellValidationContext context)
        {
            // not empty check
            bool isObjectEmpty = true;
            if (null != value)
                isObjectEmpty = string.IsNullOrEmpty(value.ToString().Trim());
            if (isObjectEmpty)
                return new ValidationResult(false, (string)Application.Current.FindResource("ReportTemplateEmptyName"));

            // unique name check
            string name = value.ToString();
            if (!string.IsNullOrEmpty(ReportDataWrapper.StartTemplateName))
            {
                if (0 == string.Compare(ReportDataWrapper.StartTemplateName, name, true))
                    return ValidationResult.ValidResult;
            }

            ReportsGenerator generator = App.Current.ReportGenerator;
            ICollection<string> presentedNames = generator.GetPresentedNames(true);
            foreach (string nameTemplate in presentedNames)
            {
                if (0 == string.Compare(nameTemplate, name, true))
                    return new ValidationResult(false, (string)Application.Current.FindResource("ReportTemplateNotUniqueName"));
            }

            // normal length check
            bool isLong = false;
            string templatePath = ReportsGenerator.GetNewTemplatePath(name, ReportDataWrapper.StartTemplatePath);
            string fileName = null;
            try
            {
                fileName = ReportsGenerator.GetTemplateAbsolutelyPath(templatePath);
                new FileInfo(fileName);
            }
            catch (PathTooLongException)
            {
                isLong =true;
            }
            catch (Exception)
            {
            }

            if (isLong)
                return new ValidationResult(false, (string)Application.Current.FindResource("ReportTemplateLongName"));

            // valid name check
            if (!FileHelpers.ValidateFilepath(fileName))
                return new ValidationResult(false, (string)Application.Current.FindResource("ReportTemplateInvalidName"));

            return ValidationResult.ValidResult;
        }
Example #9
0
    private ValidationResult ValidateCellRules( out Exception validationException, out CellValidationRule ruleInError )
    {
      ValidationResult result = ValidationResult.ValidResult;
      ruleInError = null;
      validationException = null;

      CellValidationContext cellValidationContext =
        new CellValidationContext( this.GetRealDataContext(), this );

      CultureInfo culture = this.Language.GetSpecificCulture();

      foreach( CellValidationRule cellValidationRule in this.CellValidationRules )
      {
        try
        {
          result = cellValidationRule.Validate( this.Content, culture, cellValidationContext );
        }
        catch( Exception exception )
        {
          validationException = exception;
          result = new ValidationResult( false, exception.Message );
        }

        if( !result.IsValid )
        {
          ruleInError = cellValidationRule;
          break;
        }
      }

      ColumnBase parentColumn = this.ParentColumn;

      if( ( parentColumn != null ) && ( result.IsValid ) )
      {
        foreach( CellValidationRule cellValidationRule in parentColumn.CellValidationRules )
        {
          try
          {
            result = cellValidationRule.Validate( this.Content, culture, cellValidationContext );
          }
          catch( Exception exception )
          {
            validationException = exception;
            result = new ValidationResult( false, exception.Message );
          }

          if( !result.IsValid )
          {
            ruleInError = cellValidationRule;
            break;
          }
        }
      }

      return result;
    }