Пример #1
0
        public string this[string propertyName]
        {
            get
            {
                var errors = _validationRules.Validate(this).Errors;

                _canSubmit = errors.Count() > 0 ? false : true;

                var error = errors.FirstOrDefault(e => e.PropertyName == propertyName);

                if (ErrorCollection.ContainsKey(propertyName) && error != null)
                {
                    ErrorCollection[propertyName] = error.ErrorMessage;
                }
                else if (error != null)
                {
                    ErrorCollection.Add(propertyName, error.ErrorMessage);
                }
                else
                {
                    ErrorCollection.Remove(propertyName);
                }

                OnPropertyChanged(nameof(ErrorCollection));

                if (ErrorCollectionView != null)
                {
                    ErrorCollectionView.Refresh();
                }

                return(error != null ? error.ErrorMessage : null);
            }
        }
        /// <summary>
        /// Валидация страницы
        /// </summary>
        /// <param name="columnName">Название элемента для валидации</param>
        /// <returns></returns>
        public override string Validate(string columnName)
        {
            // Текст ошибки
            string error = null;

            switch (columnName)
            {
            case nameof(Name):
                if (string.IsNullOrEmpty(Name))
                {
                    error = "Введите название компании";
                }
                else if (Name.Length < 3)
                {
                    error = "Минимум 3 символа";
                }
                else if (Name.Length > 150)
                {
                    error = "Максимум 150 символов";
                }
                else
                {
                    bool atLeastOneLetter = false;
                    for (int i = 0; i < Name.Length && !atLeastOneLetter; i++)
                    {
                        atLeastOneLetter = char.IsLetter(Name[i]);
                    }

                    if (!atLeastOneLetter)
                    {
                        error = "Введите хотя бы 1 букву";
                    }
                }
                break;

            case nameof(SelectedStatus):
                if (string.IsNullOrEmpty(SelectedStatus))
                {
                    error = "Выберите статус договора";
                }
                break;
            }

            if (ErrorCollection.ContainsKey(columnName))
            {
                ErrorCollection[columnName] = error;
            }
            else
            {
                ErrorCollection.Add(columnName, error);
            }


            OnPropertyChanged(nameof(ErrorCollection));

            return(error);
        }
 public string this[string columnName]
 {
     get
     {
         if (ErrorCollection.ContainsKey(columnName))
         {
             return(ErrorCollection[columnName]);
         }
         return(null);
     }
 }
Пример #4
0
        // Validation for textboxes
        string IDataErrorInfo.this[string PropertyName]
        {
            get
            {
                string error = null;
                bool   check = true;

                switch (PropertyName)
                {
                // If title is empty set error string
                case "Title":
                    if (string.IsNullOrWhiteSpace(Title))
                    {
                        error = "Title can not be empty.";
                    }
                    break;

                case "Year":
                    if (Year > DateTime.Now.Year)
                    {
                        error = "> " + DateTime.Now.Year.ToString();
                    }
                    break;
                }
                // if Dictionary already containts error for this property change it else add the error into dictionary
                // For example one textbox can have multiple validations (It can not be empty and minimum characters)
                // If error was set to "Title can not be empty" and now it is not empty but breaks the minimum character validation
                // Property was already in the dictionary and instead of adding we change its content to second validation error
                if (ErrorCollection.ContainsKey(PropertyName))
                {
                    ErrorCollection[PropertyName] = error;
                }
                else if (error != null)
                {
                    ErrorCollection.Add(PropertyName, error);
                }

                // Raise observable event and set CanAdd bool based on error
                OnPropertyChanged("ErrorCollection");
                foreach (KeyValuePair <string, string> entry in ErrorCollection)
                {
                    if (entry.Value != null)
                    {
                        check = false;
                    }
                }
                CanAdd = check;

                return(error);
            }
        }
        private void CheckForBlanks()
        {
            //Name
            if (string.IsNullOrEmpty(Name) && !ErrorCollection.ContainsKey("Name"))
            {
                ErrorCollection.Add("Name", "Field is empty");
            }
            //Min and Max Value
            if (string.IsNullOrEmpty(MinValue) && !ErrorCollection.ContainsKey("MinValue") && Visibility2 != Visibility.Visible)
            {
                ErrorCollection.Add("MinValue", "Field is empty");
            }

            if (string.IsNullOrEmpty(MaxValue) && !ErrorCollection.ContainsKey("MaxValue") && Visibility2 != Visibility.Visible)
            {
                ErrorCollection.Add("MaxValue", "Field is empty");
            }

            //Latitude and Longtitude
            if (string.IsNullOrEmpty(Latitude) && !ErrorCollection.ContainsKey("Latitude"))
            {
                ErrorCollection.Add("Latitude", "Field is empty");
            }

            if (string.IsNullOrEmpty(Longitude) && !ErrorCollection.ContainsKey("Longitude"))
            {
                ErrorCollection.Add("Longitude", "Field is empty");
            }

            //Description
            if (string.IsNullOrEmpty(Description) && !ErrorCollection.ContainsKey("Description"))
            {
                ErrorCollection.Add("Description", "Field is empty");
            }

            //Sensor type
            if (SelectedItem == null && !ErrorCollection.ContainsKey("SelectedItem"))
            {
                ErrorCollection.Add("SelectedItem", "Please choose sensor type");
            }

            //Link
            if (ToLinkWith == null && !ErrorCollection.ContainsKey("ToLinkWith"))
            {
                ErrorCollection.Add("ToLinkWith", "Please select a sensor");
            }
            RaisePropertyChanged(null);
        }
Пример #6
0
 public string this[string name]
 {
     get
     {
         string result = _inputValidator.ValidateInput(name, Name, Minimum, Maximum, Digits, Start, IsDecimalValue);
         if (ErrorCollection.ContainsKey(name))
         {
             ErrorCollection[name] = result;
         }
         else if (result != null)
         {
             ErrorCollection.Add(name, result);
         }
         OnPropertyChanged("ErrorCollection");
         return(result);
     }
 }
Пример #7
0
        private bool CanEditEMail()
        {
            if (ErrorCollection.ContainsKey(nameof(EditableEMail)))
            {
                return(false);
            }

            if (string.IsNullOrWhiteSpace(EditableEMail))
            {
                return(false);
            }

            if (EMail == null)
            {
                return(false);
            }

            if (EMails.FirstOrDefault(m => m.MailOfContact.Equals(EditableEMail)) == null)
            {
                return(true);
            }

            return(false);
        }
Пример #8
0
        private bool CanEditNumber()
        {
            if (ErrorCollection.ContainsKey(nameof(EditablePhoneNumber)))
            {
                return(false);
            }

            if (string.IsNullOrWhiteSpace(editablePhoneNumber))
            {
                return(false);
            }

            if (PhoneNumber == null)
            {
                return(false);
            }

            if (PhoneNumbers.FirstOrDefault(phnm => phnm.PhoneNumber.Equals(editablePhoneNumber)) == null)
            {
                return(true);
            }

            return(false);
        }
Пример #9
0
        public string this[string propertyname]
        {
            get
            {
                string result = null;
                switch (propertyname)
                {
                case "EntityNameTxt":
                    if (string.IsNullOrWhiteSpace(EntityNameTxt))
                    {
                        result = "Entity Name cannot be empty";
                    }
                    else if (IsBeginWNum(EntityNameTxt))
                    {
                        result = "Cannot begin with a number";
                    }
                    else if (IsValidName(EntityNameTxt))
                    {
                        result = "Not a valid name. Only Letters, Numbers or underscore are allowed";
                    }
                    else if (IsReservedWord(EntityNameTxt))
                    {
                        result = "This is a Reserved Word";
                    }
                    else if (ENameExist(EntityNameTxt))
                    {
                        result = "Component already Exist";
                    }
                    break;

                case "PortNameTxt":
                    if (string.IsNullOrWhiteSpace(PortNameTxt))
                    {
                        result = "Port Name cannot be empty";
                    }
                    else if (IsBeginWNum(PortNameTxt))
                    {
                        result = "Cannot begin with a number";
                    }
                    else if (IsValidName(PortNameTxt))
                    {
                        result = "Not a valid name. Only Letters, Numbers or underscore are allowed";
                    }
                    else if (IsReservedWord(PortNameTxt))
                    {
                        result = "This is a Reserved Word";
                    }
                    else if (PNameExist(PortNameTxt))
                    {
                        result = "Port Name Exists";
                    }
                    break;

                case "DirectionSel":
                    if (!IsDirectionSel(DirectionSel))
                    {
                        result = "No Direction Selected";
                    }
                    break;

                case "MsbTxt":
                    if (BitsEnable == true)
                    {
                        if (string.IsNullOrWhiteSpace(MsbTxt))
                        {
                            result = "MSB cannot be empty";
                        }
                        else if (IsInterger(MsbTxt))
                        {
                            result = "Only Integers are allowed";
                        }
                        break;
                    }
                    else
                    {
                        break;
                    }

                case "LsbTxt":
                    if (BitsEnable == true)
                    {
                        if (string.IsNullOrWhiteSpace(LsbTxt))
                        {
                            result = "LSB cannot be empty";
                        }
                        else if (IsInterger(LsbTxt))
                        {
                            result = "Only Integers are allowed";
                        }
                        break;
                    }
                    else
                    {
                        break;
                    }
                }

                if (ErrorCollection.ContainsKey(propertyname))
                {
                    ErrorCollection[propertyname] = result;
                }
                else if (result != null)
                {
                    ErrorCollection.Add(propertyname, result);
                }

                OnPropertyChanged("ErrorCollection");
                OnPropertyChanged("FinishEnable");
                OnPropertyChanged("AddPortEnable");
                return(result);
            }
        }
Пример #10
0
        public string this[string propName]
        {
            get
            {
                String result = null;

                switch (propName)
                {
                case "IPAddress":
                {
                    if (String.IsNullOrWhiteSpace(IPAddress))
                    {
                        result = "IPAddress can't be empty";
                    }
                    else
                    {
                        Regex ipAddrRegEx = new Regex(IPAddressRegEx);
                        if (!(ipAddrRegEx.Match(IPAddress).Success))
                        {
                            result = "Example: 127.0.0.1";
                        }
                    }

                    break;
                }

                case "UserName":
                {
                    if (String.IsNullOrWhiteSpace(UserName))
                    {
                        result = "User name can't be empty";
                    }
                    else if (UserName.Length < 3)
                    {
                        result = "Min length is 3";
                    }
                    else if (UserName.Length > 16)
                    {
                        result = "Max length is 16";
                    }
                    else
                    {
                        Regex userNameRegEx = new Regex(UserNameRegEx);
                        if (!(userNameRegEx.Match(UserName).Success))
                        {
                            result = "User name can contain only Uppercase, Lowcase english symbols, digits, underscore and dash";
                        }
                    }

                    break;
                }

                case "Password":
                {
                    if (String.IsNullOrWhiteSpace(Password))
                    {
                        result = "Password can't be empty";
                    }
                    else if (Password.Length < 6)
                    {
                        result = "Min length is 6";
                    }
                    else if (Password.Length > 18)
                    {
                        result = "Max length is 18";
                    }
                    else
                    {
                        Regex pswdRegEx = new Regex(Password);
                        if (!(pswdRegEx.Match(Password).Success))
                        {
                            result = "User name can contain only Uppercase, Lowcase english symbols, digits, underscore and dash";
                        }
                    }

                    break;
                }

                default:
                    break;
                }

                if (ErrorCollection.ContainsKey(propName))
                {
                    ErrorCollection[propName] = result;
                }
                else if (result != null)
                {
                    ErrorCollection.Add(propName, result);
                }

                RaisePropertyChangedEvent("ErrorCollection");
                return(result);
            }
        }
        /// <summary>
        /// Валидация страницы
        /// </summary>
        /// <param name="columnName">Название элемента для валидации</param>
        /// <returns></returns>
        public override string Validate(string columnName)
        {
            // Текст ошибки
            string error = null;

            switch (columnName)
            {
            case nameof(Login):
                Login = Login.Trim();
                if (string.IsNullOrEmpty(Login))
                {
                    error = "Логин не введён";
                }
                else if (Login.Length < 3)
                {
                    error = "Минимум 3 символа";
                }
                else if (Login.Length > 150)
                {
                    error = "Максимум 150 символов";
                }
                else if (RaP_DAO.IsLoginExists(id ?? 0, Login))
                {
                    error = "Этот логин занят";
                }
                else
                {
                    // Проверка, что в логин состоит только из букв и цифр
                    bool onlyLettersAndDigits = false;
                    bool atLeastOneLetter     = false;

                    for (int i = 0; i < Login.Length &&
                         !onlyLettersAndDigits &&
                         !atLeastOneLetter; i++)
                    {
                        onlyLettersAndDigits = char.IsLetter(Login[i]) || char.IsDigit(Login[i]);
                        atLeastOneLetter     = char.IsLetter(Password[i]);
                    }

                    if (!onlyLettersAndDigits)
                    {
                        error = "Логин должен состоять только из букв и цифр";
                    }
                    else if (!atLeastOneLetter)
                    {
                        error = "Введите хотя бы 1 букву";
                    }
                }
                break;

            case nameof(Password):
                Password = Password.Trim();
                if (string.IsNullOrEmpty(Password))
                {
                    error = "Введите пароль";
                }
                else if (Password.Length < 6)
                {
                    error = "Минимум 6 символов";
                }
                else if (Password.Length > 50)
                {
                    error = "Максимум 50 символов";
                }
                else if (Password == Login)
                {
                    error = "Пароль не может совпадать с логином";
                }
                else
                {
                    // Проверка, что в пароле есть хотя бы 1 буква
                    bool atLeastOneLetter = false;
                    for (int i = 0; i < Password.Length && !atLeastOneLetter; i++)
                    {
                        atLeastOneLetter = char.IsLetter(Password[i]);
                    }

                    if (!atLeastOneLetter)
                    {
                        error = "Введите хотя бы 1 букву";
                    }
                }
                break;

            case nameof(SelectedCompany):
                if (SelectedCompany == null)
                {
                    error = "Выберите компанию сотрудника";
                }
                break;
            }

            if (ErrorCollection.ContainsKey(columnName))
            {
                ErrorCollection[columnName] = error;
            }
            else
            {
                ErrorCollection.Add(columnName, error);
            }

            OnPropertyChanged(nameof(ErrorCollection));

            return(error);
        }
Пример #12
0
        public string this[string name]
        {
            get
            {
                string errorText = null; //a line of text that show when validate
                switch (name)
                {
                case "StudentId":
                    if (string.IsNullOrWhiteSpace(StudentId))
                    {
                        errorText = "Student ID cannot be empty";
                    }
                    else if (StudentId.ToString().Length > 10)
                    {
                        errorText = "Student ID maximun length is 10 charaters";
                    }
                    else if (!Regex.Match(StudentId, @"^[0-9]+$").Success)
                    {
                        errorText = "Invalid Student ID";
                    }
                    break;

                case "FirstName":
                    if (string.IsNullOrWhiteSpace(FirstName))
                    {
                        errorText = "First Name cannot be empty";
                    }
                    else if (FirstName.Length > 20)
                    {
                        errorText = "First Name maximun length is 20 charaters";
                    }
                    else if (!Regex.Match(FirstName, @"^[a-zA-Z]+$").Success)
                    {
                        errorText = "Invalid First Name";
                    }
                    break;

                case "LastName":
                    if (string.IsNullOrWhiteSpace(LastName))
                    {
                        errorText = "Last Name cannot be empty";
                    }
                    else if (LastName.Length > 20)
                    {
                        errorText = "Last Name maximun length is 20 charaters";
                    }
                    else if (!Regex.Match(LastName, @"^[a-zA-Z]+$").Success)
                    {
                        errorText = "Invalid Last Name";
                    }
                    break;

                case "BirthDate":
                    break;

                case "Gender":
                    break;

                case "City":
                    if (string.IsNullOrWhiteSpace(City))
                    {
                        errorText = "City cannot be empty";
                    }
                    else if (City.Length > 20)
                    {
                        errorText = "City maximun length is 20 charaters";
                    }
                    else if (!Regex.Match(City, @"^[a-zA-Z]+$").Success)
                    {
                        errorText = "Invalid City";
                    }
                    break;

                case "Email":
                    var regex = @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z";
                    if (string.IsNullOrWhiteSpace(Email))
                    {
                        errorText = "Email cannot be empty";
                    }
                    else if (!Regex.Match(Email, regex).Success)
                    {
                        errorText = "Email is invalid";
                    }
                    else if (Email.ToString().Length > 30)
                    {
                        errorText = "Email maximun length is 30 charaters";
                    }
                    break;

                case "Class":
                    if (string.IsNullOrWhiteSpace(Class))
                    {
                        errorText = "Class cannot be empty";
                    }
                    break;
                }
                if (ErrorCollection.ContainsKey(name))
                {
                    ErrorCollection[name] = errorText;
                }
                else if (errorText != null)
                {
                    ErrorCollection.Add(name, errorText);
                }
                NotifyOfPropertyChange(() => ErrorCollection);
                NotifyOfPropertyChange(() => CanSaveButton);
                return(errorText);
            }
        }