Exemplo n.º 1
0
        public static IForm <Laporan> BuildForm()
        {
            OnCompletionAsyncDelegate <Laporan> processReport = async(context, state) =>
            {
                await Task.Run(() =>
                {
                    state.NoLaporan  = $"LP-{DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss")}";
                    state.TglLaporan = DateTime.Now;
                }
                               );
            };
            var builder = new FormBuilder <Laporan>(false);
            var form    = builder
                          .Field(nameof(Nama), validate:
                                 async(state, value) =>
            {
                var result = new ValidateResult {
                    IsValid = true, Value = value, Feedback = "ok, nama valid"
                };
                bool res = value != null ? !string.IsNullOrEmpty(value.ToString()):false;
                if (!res)
                {
                    result.Feedback = "tolong namanya di isi ya";
                    result.IsValid  = false;
                }
                return(result);
            })
                          .Field(nameof(Telpon), validate:
                                 async(state, value) =>
            {
                var result = new ValidateResult {
                    IsValid = true, Value = value, Feedback = "ok, telpon valid"
                };
                bool res = FormValidation.isValidPhoneNumber(value.ToString());
                if (!res)
                {
                    result.Feedback = "tolong isi dengan nomor telpon yang benar ya, cth : 08171234567";
                    result.IsValid  = false;
                }
                return(result);
            })
                          .Field(nameof(Email), validate:
                                 async(state, value) =>
            {
                var result = new ValidateResult {
                    IsValid = true, Value = value, Feedback = "ok, email valid"
                };
                bool res = FormValidation.IsValidEmail(value.ToString());
                if (!res)
                {
                    result.Feedback = "tolong isi dengan email yang benar ya";
                    result.IsValid  = false;
                }
                return(result);
            })
                          .Field(nameof(TipeLaporan))
                          .Field(nameof(Keterangan), validate:
                                 async(state, value) =>
            {
                var result = new ValidateResult {
                    IsValid = true, Value = value, Feedback = "ok, keterangan valid"
                };
                bool res = value != null ? !string.IsNullOrEmpty(value.ToString()) : false;
                if (!res)
                {
                    result.Feedback = "tolong keterangannya di isi ya";
                    result.IsValid  = false;
                }
                return(result);
            })
                          .Field(nameof(Lokasi), validate:
                                 async(state, value) =>
            {
                var result = new ValidateResult {
                    IsValid = true, Value = value, Feedback = "ok, lokasi valid"
                };
                bool res = value != null ? !string.IsNullOrEmpty(value.ToString()) : false;
                if (!res)
                {
                    result.Feedback = "tolong lokasi di isi ya";
                    result.IsValid  = false;
                }
                return(result);
            })
                          .Field(nameof(Waktu))
                          .Field(nameof(SkalaPrioritas), validate:
                                 async(state, value) =>
            {
                var result = new ValidateResult {
                    IsValid = true, Value = value, Feedback = "ok, skala valid"
                };
                bool res = int.TryParse(value.ToString(), out int jml);
                if (res)
                {
                    if (jml <= 0)
                    {
                        result.Feedback = "input data yang benar ya, minimum di isi 1";
                        result.IsValid  = false;
                    }
                    else if (jml > 10)
                    {
                        result.Feedback = "input data yang benar ya, maximum di isi 10";
                        result.IsValid  = false;
                    }
                }
                else
                {
                    result.Feedback = "tolong isi dengan angka ya";
                    result.IsValid  = false;
                }
                return(result);
            })
                          .Confirm(async(state) =>
            {
                var pesan = $"Abang uda terima laporan dari {state.Nama} tentang {state.TipeLaporan.ToString()}, sudah ok ?";
                return(new PromptAttribute(pesan));
            })
                          .Message($"Makasih ya atas laporannya.")
                          .OnCompletion(processReport)
                          .Build();

            return(form);
        }