Fill out form, come back later. Query String for pre-fill?
Hey all,
I was hoping I could get some advice, I am building a bespoke solution just now to retrieve form fields and pre-populate the fields that have been filed.
The forms are quite extensive.
The issues I am seeing so far:
Nothing can be mandatory or else the full form needs filled in before submit, making pre fill useless. I would like to have some mandatory fields.
Can i fill any parts of the form with a query string?
Is there a current workflow for this type of situation?
I build a model which handles all the validation rules.
I build a Partial View to handle the front-end
This links up to a SurfaceController handle the back-end submit.
Here is my Model example
namespace CMS.Models
{
public class ContactForm
{
[Required(ErrorMessage = "Please enter your first name")]
[StringLength(50, ErrorMessage = "First name cannot exceed 50 characters")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter your last name")]
[StringLength(50, ErrorMessage = "Last name cannot exceed 50 characters")]
public string LastName { get; set; }
[Required(ErrorMessage = "Please enter your email address")]
[StringLength(255, ErrorMessage = "Email cannot exceed 255 characters")]
[Email(ErrorMessage = "Please enter a valid email address")]
public string Email { get; set; }
[Required(ErrorMessage = "Please enter your phone number")]
[StringLength(20, ErrorMessage = "Phone number cannot exceed 20 characters")]
public string Phone { get; set; }
[Required(ErrorMessage = "Please enter your enquiry")]
[StringLength(1000, ErrorMessage = "Enquiry cannot exceed 1000 characters")]
public string Enquiry { get; set; }
public string OverrideEmail { get; set; }
public HttpPostedFileBase Attachment { get; set; }
public ContactForm()
{
}
}
}
Here is my surface controller example. I store this in AppCode/Controllers/SurfaceControllers/FormsSurfaceController.cs. I would do the same. I wont give you the whole file as I handle a lot of forms. You need two methods though.
[HttpGet]
[ActionName("ContactForm")]
public ActionResult ContactFormGet()
{
//override email
ContactForm model = new ContactForm();
Node currentPageNode = ContentManager.GetCurrentNode();
if (currentPageNode.NodeTypeAlias == "contactMultiplePage")
{
CMS.Models.ContactMultiplePage contactMultiplePage = new CMS.Models.ContactMultiplePage(currentPageNode);
if (contactMultiplePage != null)
{
model.OverrideEmail = contactMultiplePage.ContactFormEmail;
}
}
return PartialView("ContactForm", model);
}
[HttpPost]
[ActionName("ContactForm")]
public FileUploadJsonResult ContactFormPost(ContactForm model)
{
var errorMessage = string.Empty;
if (!ModelState.IsValid)
{
var errors = ModelState.Where(x => x.Value.Errors.Count > 0).Select(x => new { x.Key, x.Value.Errors }).ToArray();
errorMessage = "<h3>There were errors submitting your enquiry.....</h3>";
if (errors.Count() > 0)
{
foreach (var error in errors)
{
var message = "<span class='validation-error'>* " + error.Errors[0].ErrorMessage + "</span><br/>";
errorMessage += (message);
}
errorMessage += "<br/><br/>";
return new FileUploadJsonResult
{
Data = new
{
Success = false,
ErrorMessage = errorMessage
}
};
}
}
//send the submit resume emails here!
EmailManager.ContactFormToUser(model);
EmailManager.ContactFormToAdministrator(model);
return new FileUploadJsonResult
{
Data = new
{
Success = true,
ReturnUrl = SiteSettings.GetSiteSettingStringProperty("contactFormPage") + "/complete"
}
};
}
Fill out form, come back later. Query String for pre-fill?
Hey all,
I was hoping I could get some advice, I am building a bespoke solution just now to retrieve form fields and pre-populate the fields that have been filed.
The forms are quite extensive.
The issues I am seeing so far:
Nothing can be mandatory or else the full form needs filled in before submit, making pre fill useless. I would like to have some mandatory fields.
Can i fill any parts of the form with a query string?
Is there a current workflow for this type of situation?
Thanks,
Marc
Hi,
To handle complex forms I build my own.
I build a model which handles all the validation rules.
I build a Partial View to handle the front-end
This links up to a SurfaceController handle the back-end submit.
Here is my Model example
Here is my surface controller example. I store this in AppCode/Controllers/SurfaceControllers/FormsSurfaceController.cs. I would do the same. I wont give you the whole file as I handle a lot of forms. You need two methods though.
Here is my PartialView
And here is how to call your partial view and load the form
With this setup you have a lot of control. You can collect Query strings in the GET method and past them to the frontend etc.
Hope this helps.
Kind Regards
David
Thanks David, this is a superb answer, wish I could high five you more for it.
Ill need to take time to digest it properly, many thanks mate!
M
Hi Marc,
No problems. Let me know if I can be any more help.
Kind Regards
David
is working on a reply...