MVC3 Razor dynamic form creation and validation - asp.net-mvc

I have setup a page on my website where admin staff can create a competition and add any number of fields for certains types and they can be set to required or not to this competition.
When the competition page itself is loaded the questions load dynamically from the database using editor templates for each question type ie Text, Multi Select, Multi Line Text, Email
There are a few tables but the main ones are competition, CompetitionQuestions and QuestionTypes
Each competition can have multiple questions of different types.
The settings on the Question types have the validation type ie Email, RegularExpression, Length etc
and the Questions when added for each competition can be set as required or not
Now I can get my competition page to lead fine dynamically adding the questions for the page with an EditorFor and an Editor Template for each type.
My problem is I don't know how to manually apply validation for the required questions and the validation based on the question type validation settings?

Related

Pre-set Formulas in Validation and alerts?

I'am using Orbeon Forms CE edition. Is there any option to select pre-set formulas for input fields in form builder? In email field, when I select formula in validation I am writing my condition or regex to validate the form data, is there any option to add validation conditions and regex in Dropdown data rather than writing...
You can define those validations as simple types in an XML schema, for instance using regular expressions. Then you can attach your schema to the form in Form Builder, and those simple types will show up in a dropdown in the Control Settings dialog, in the Validations and Alerts tab. However, this feature is only available in Orbeon Forms PE. For more on this, see schema support.

Rails: many-to-one display on form via dynamically added input text boxes

I have a form in rails that allows the user to create a new object (call it a search).
This object has_many excluded_phrases.
What I would like is the ability to display one text box per excluded phrase added.
The form will start out with only a single text box, allowing the user to add one excluded phrase. If they want to add more, there will be button labeled "+" that will dynamically add one more text box, and allow the user to add as many more items as he wants.
On form submit, this should populate the db with the user search, and create all the required phrases that are linked to that user search.
Help?
All inspiration needed is there:
http://railscasts.com/episodes/197-nested-model-form-part-2
This popular rails plugin does exactly what you want.

Raw HTML in Form Filter

At my company, we had developed a athletes management solution, were each athlete is inserted in the application by administrators users. For the referred solution, it was used the symfony admin generator.
On the second project iteration, one of the clients request was to turn the printed athletes list more legible. To accomplish that, we had created proper CSS styles, to be used when the user selects the browser print option.
However, the athletes form filter has some HTML tags (symfony widgets) whose do not render properly, namely, the select tag that has possible multiple choices (the select choices do not appear on the print preview).
So, I would to know if is possible to insert raw HTML directly on the form filter (get the selected options and convert them is labels or plain text), that will be only visible when the user selects the print option.
You could put a custom widget in place of the standard choice widget that adds in the values you want to be shown when printing but hidden with styles by default. Then you show them using your print style sheet.

Which way to structure Data for User Generated Form Templates

I'm thinking about an idea for something, as well as learning Ruby on Rails (easy eh :) ). I want something to allow a user to generate forms as templates, then assign these templates as forms in a location in a tree hierarchy, then allow users to fill in instances of these forms and save the data.
So, I've got two different thoughts as how to structure the data. I'm currently thinking a template will have one or more sections, and a section will have one or more fields and the field will then have attributes (type, color, position, validation etc).
So, should this be described as linked tables ;
field belongs_to_a section belongs_to_a template
or should I have one template table, that has a field called body that can be serialized in and out to XML or something to render ?
<template>
<section>
<field attrib=foo attrib2=bar><field>
</section>
<template>
Any ideas or comments welcome, even if I'm totally on the wrong track....
I would opt for the first design. If I were a user (or a developer) I'd much rather enter data in logically related tables than write XML to define a screen. In fact, Oracle have used a data structure not unlike this behind their very successful tool Application Express.

What is the best way to handle repeating forms in MVC?

The best public example that I can think of off the top of my head would be the amazon shopping cart. Where you have a page that displays multiple distinct records that can have multiple distinct fields updated.
I can't put each one in a form tag because the user may modify more than one record and then submit.
I can't just update all the records that I get back because:
1. Performance
2. Auditing
3. If someone changed the record that the user 'didn't change' when they were viewing the page and then the user submits those changes would be overwritten.
So how to best handle getting the data back and then getting which records where changed out of that?
Is that clear?
Use binding! Don't be iterating the form collection in your actions.
Steve Sanderson wrote a blog post about how to do it. I wrote a blog post on how to do it with MvcContrib.FluentHtml. Both posts are very detailed and include downloadable code.
Generate your form in a repeater, and append an ID to the form elements that increments with each new form. Save the number of repeated form elements in a hidden field. Then in your controller, read the value of this hidden field - that'll be the number of forms to read. Then, in a loop, retrieve each form's fields by specifying the name of the field, plus the loop index appended to the name, as the key.
You can use some javascript logic to detect when a form's value changes, and update a hidden field in that form's section if that occurs; or you can hide the original values inside a hidden field with each form section (although I don't recommend this as too many fields / forms will bloat your page).
one (but not necessarily the best) approach is to store which items are changed in a js-variable or something on the client side as they are changed, and then only send the data that is actually different from what the user recieved.
and as Erik stated, you could use hidden form elements to make sure that it works without js as well.

Resources