Exemple #1
0
        private async void SaveMember(object sender, EventArgs e)
        {
            var form = (Worker)BindingContext;

            form.WhenAdded  = DateTime.Now;
            form.BirthDate  = birthDate.Date;
            form.CompanyId  = companies[company.SelectedIndex].Id;
            form.PositionId = positions[position.SelectedIndex].Id;
            var fields = new List <string>();
            var errors = new List <string>();

            var errorsList = _worker.IsDataValid(form)
                             .Where(x => !string.IsNullOrWhiteSpace(x))
                             .ToList();

            foreach (var combined in errorsList.Select(error => error.Split(new[] { ';' }, 2)).Where(combined => !string.IsNullOrWhiteSpace(combined.Last())))
            {
                fields.Add(combined.First());
                errors.Add(combined.Last());
            }

            if (!errors.Any())
            {
                _worker.Create(form);
                errors.Clear();
                await Navigation.PopAsync();
            }
            else
            {
                for (int i = 0; errors.Count - 1 >= i; i++)
                {
                    await DisplayAlert(fields[i], errors[i], "OK");
                }

                errors.Clear();
            }
        }
 public void Create(Worker worker)
 {
     _workerRepository.Create(worker);
 }