コード例 #1
0
        public static BaseFormModel ToBaseForm(this Form form)
        {
            BaseFormModel _result = new BaseFormModel();

            _result.Id           = form.Id;
            _result.FormName     = form.FormName;
            _result.CreatedDate  = form.CreatedDate;
            _result.ModifiedDate = form.ModifiedDate;
            return(_result);
        }
コード例 #2
0
        public async Task <ActionResult> DeleteApplicant(BaseFormModel model)
        {
            var applicant = ApplicantService.GetApplicantById(int.Parse(model.Id));

            if (applicant == null)
            {
                return(Json(new { model = "failed", modelList = "Абітурієнта не знайдено!" }, JsonRequestBehavior.AllowGet));
            }

            var result = await ApplicantService.DeleteApplicant(applicant);

            return(Json(new { model = result.Successed ? "confirmed" : "failed", modelList = result.Message, type = "remove" }, JsonRequestBehavior.AllowGet));
        }
コード例 #3
0
        public static Form ToForm(this BaseFormModel form)
        {
            Form _result = new Form();

            if (form.Id.HasValue)
            {
                _result.Id = form.Id.GetValueOrDefault();
            }
            _result.FormName     = form.FormName;
            _result.CreatedDate  = form.CreatedDate;
            _result.ModifiedDate = form.ModifiedDate;
            return(_result);
        }
コード例 #4
0
        protected new bool MapRequestFormData(BaseFormModel model)
        {
            if (Request.HttpMethod != "POST")
            {
                return(false);
            }

            // CSRF protection: If the anti CSRF cookie is present, a matching token must be in the form data too.
            const string antiCsrfToken = "__RequestVerificationToken";

            if (Request.Cookies[antiCsrfToken] != null)
            {
                AntiForgery.Validate();
            }


            foreach (string formField in Request.Form)
            {
                if (formField == antiCsrfToken)
                {
                    // This is not a form field, but the anti CSRF token (already validated above).
                    continue;
                }



                FormFieldModel fieldModel = model.FormFields.FirstOrDefault(f => f.Name == formField);
                if (fieldModel == null)
                {
                    Log.Debug("Form [{0}] has no defined field for form field '{1}'", model.Id, formField);
                    continue;
                }


                // TODO: validate if field is valid string (no injection etc)

                //string formFieldValue = Request.Form[formField];
                List <string> formFieldValues = Request.Form.GetValues(formField).Where(f => f != "false").ToList();
                try
                {
                    fieldModel.Values = formFieldValues;
                }
                catch (Exception ex)
                {
                    Log.Debug("Failed to set Model [{0}] property '{1}' to value obtained from form data: '{2}'. {3}", model.Id, fieldModel.Name, formFieldValues, ex.Message);
                    ModelState.AddModelError(fieldModel.Name, ex.Message);
                }

                FormFieldValidator validator         = new FormFieldValidator(fieldModel);
                string             validationMessage = "Field Validation Failed";
                if (!validator.Validate(formFieldValues, ref validationMessage))
                {
                    if (validationMessage != null)
                    {
                        Log.Debug("Validation of property '{0}' failed: {1}", fieldModel.Name, validationMessage);
                        ModelState.AddModelError(fieldModel.Name, validationMessage);
                        continue;
                    }
                }
            }

            return(true);
        }
コード例 #5
0
        protected override ViewModel EnrichModel(ViewModel model)
        {
            BaseFormModel baseForm = base.EnrichModel(model) as BaseFormModel;

            if (baseForm != null)
            {
                if (Request.HttpMethod == "POST")
                {
                    FormModel form = baseForm as FormModel;

                    // MapRequestFormData validates model as well
                    MapRequestFormData(form);

                    if (ModelState.IsValid)
                    {
                        try
                        {
                            //Add logic to save form data here
                            foreach (var postAction in form.FormPostActions)
                            {
                                string formHandlerName = string.Empty;

                                try
                                {
                                    formHandlerName = postAction.FormHandler;

                                    var formHandler = FormHandlerRegistry.Get(formHandlerName);
                                    if (formHandler != null)
                                    {
                                        formHandler.ProcessForm(Request.Form, form.FormFields, postAction);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    if (!string.IsNullOrEmpty(form.ErrorRedirect.Url))
                                    {
                                        Log.Error(ex, "Error occured while processing form data with form handler: {0}. Error message: {1}", formHandlerName, ex.Message);
                                        return(new RedirectModel(form.ErrorRedirect.Url));
                                    }
                                    else
                                    {
                                        throw ex;
                                    }
                                }
                            }

                            if (!string.IsNullOrEmpty(form.SuccessRedirect.Url))
                            {
                                return(new RedirectModel(form.SuccessRedirect.Url));
                            }
                            else
                            {
                                return(new RedirectModel(WebRequestContext.Localization.Path));
                            }
                        }
                        catch (Exception ex)
                        {
                            if (!string.IsNullOrEmpty(form.ErrorRedirect.Url))
                            {
                                Log.Error(ex, "Error occured while saving form data.");
                                return(new RedirectModel(form.ErrorRedirect.Url));
                            }
                            else
                            {
                                throw ex;
                            }
                        }
                    }
                }
                else
                {
                    // Here we can add logic to enrich our model

                    // Load options from category
                    foreach (var field in baseForm.FormFields.Where(f => !string.IsNullOrEmpty(f.OptionsCategory)))
                    {
                        if (field.FieldType == FieldType.DropDown || field.FieldType == FieldType.CheckBox || field.FieldType == FieldType.RadioButton)
                        {
                            List <OptionModel> options = TaxonomyProvider.LoadOptionsFromCategory(field.OptionsCategory, WebRequestContext.Localization);
                            field.OptionsCategoryList = options;
                        }
                    }
                }
            }

            return(baseForm);
        }