I'm working on a very dynamic site build at the moment. What I'm trying to do is creating something like a survey that can be created dynamically from a control panel.
In the control panel you add input fields (these are saved in a database), what the user then see is a form that I generate from the database. So if I add 3 input fields to the database the survey will contain 3 fields. If I add 20 fields the survey will have 20 fields.
Now my problem is that I want to validate these fields, and I would like to be able to hook me in with the standard validation flow. I can't create a Model with validation rules since the number of fields and their names are dynamic the only thing I know is what kind of data that is expected in every field (this rule is found in the database).
In an ordinary case I would get the automatic highlighted fields that are not valid and so on thanks to the built in validation flow with ValidationResult and so on.
So the question now is can I somehow simulate parts of the validation and then hook me back in with the validation result, and if not valid, the form prints the error messages and fill the fields with the data that was given?
Regards
Tobias
What I would do is create some kind of expando model, my own ModelMetadataProvider and might also need my own ModelValidator for that model.
Then, you can easily create validation using the Html.EditorFor and other Html helpers, as they use the metadata to create validation.
BTW, you might also need to create a model binder :)
meta data:
http://mgolchin.net/posts/21/dive-deep-into-mvc-modelmetadata-and-modelmetadataprovider
http://weblogs.asp.net/seanmcalinden/archive/2010/06/11/custom-asp-net-mvc-2-modelmetadataprovider-for-using-custom-view-model-attributes.aspx
http://bradwilson.typepad.com/blog/2010/01/why-you-dont-need-modelmetadataattributes.html
validator:
http://dotnetslackers.com/articles/aspnet/Customizing-ASP-NET-MVC-2-Metadata-and-Validation.aspx#s2-validation
http://dotnetslackers.com/articles/aspnet/Experience-ASP-NET-MVC-3-Beta-the-New-Dependency-Injection-Support-Part2.aspx#s10-new-support-for-validator-provider
model binder:
http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx
This might bo overkill... But these are the extensibility points that you can use.
Related
I am currently trying to sort out my user registration in Smyfony2, loosely following their documentation here:
http://symfony.com/doc/current/cookbook/doctrine/registration_form.html
Sadly, their example is a bit simplistic.
My User Entity has a number of fields which only get added to during registration, and can't be changed there after, so I separated those out from my UserType into my RegistrationType.
The problem now, is that Symfony can't find any of the fields, requested for the form, which live within the User Entity, because it is looking for them in the Registration model. How do I get the Registration model to point to User Entity?
In the documenation example, they avoid all this as the "terms and conditions" checkbox doesn't get added to the database.
e.g. they use this:
$builder->add('user', new UserType());
but as I mentioned, that only has the fields I want the user to edit after registration.
I tried the data_class, but it complained about Form\Model\Registration wasn't of type Entity\User.
These seems like a really common issue when you are trying to embed bits of forms for a single entity, yet it doesn't cover it in the documentation.
And no, I don't want to use FoSUserBundle.
Actually, it's possible and really easy to have several form types for the same model class. You can have the RegistrationType with lots of fields and then the UserType with only some of those fields. Both use the same User model.
I need your help. I need to create a validation framework for complex form which will be generated dynamically.
For instance, user selects some Template he wants to change, then he receives a list of fields to be filled with data. Number of fields can reach up to 150.
There will be few types of validation rules
Regular expresions
Is field mandatory
data type - string, numeric or alphanumeric (can be validated with RegExp)
All the rules for each field will be stored in the database.
The complexity is that each field may have it's own regular expression, therefore validation error messages are also different. I have attached image describing how the control will look:
I'm thinking on using JQuery framework and assign each control to be validated using the attribute, but not sure if this task could be done using the JQuery.
Will appreciate any advices on this!
Thank you very much!
Depending multiple choices done by a user in few steps I have to generate a form in a web page for the user.
In a database I had all the necessary stuff (regex validation of every form field, name, type etc.) I would like to know what could be the best way to autogenerate a form using MVC3.
Should I autogenerate a model, set the model of my views to dynamic, and inject some validation attributes to every property of my dynamic model?
How should I get the values on my post action?
As the fields are all dynamic (from the database), your model could very easily store an IEnumerable where Question is an object which has information about the type of field. i.e. Id, TypeId (text, checkbox, select list), Wording, Heading, ValidationTypeId etc.
Then use mvchelpers passing in Question to a method which would determine the html to output. This could very well include a validator.
On the form loop over Model.SurveyQuestions and for each row send Question to the mvc helper. The helper, knowing everything about the Question can output the label, type of input box and the required validator.
This is a wise way to accomplish what you are trying to do as your input fields is dynamic. I just completed a project doing exactly this.
Actually, I have a form with an input to persist an entity. I want to create another form somewhere else with more information and I want to save everything from the submit which is in the first form. I can't include the second inputs in the same form, because this is not the same view. What's the best way to resolve this issue?
Thanks in advance
This sounds difficult.
Have I understood this right:
You have on entity with some required information and some nullable, additional information.
Now there are two forms one which creates the entity with the required information and you than want to update the form with the additionals.
Is this correct?
I would try two things:
Default
You create one FormType with all the fields and have two views which only render the required fields (for this don't use form_rest).
Than do the form handling like descriped in the docs.
It could be that this doesn't work with the validation.
Use FormModels
Under Form/Model/FormModelClass you have the two classes which only have the attributes the one form requires. The Form component interacts with them and maps all attributes to this models. When the form is submitted successfullly and valid you can manuelly map the attributes back to your orginal entity and persist it
Suppose I have a model named 'company' holding some properties like
Name
Address
fax
And My View (of IEnumarable type (MVC# RAZOR)) has two forms one(form submitting data to different actions) to display list all the available company(existing). And another(form) i used to create new client with Name TextBox Like
#Html.TextBox("name","")
Here i want to add validation to that particular field. Suggest me the possible easiest way?? What i tried is http://www.codeproject.com/Articles/39016/Form-validation-with-ASP-NET-MVC(But it doesn't works)
my view has two forms
Therefore you need two view models because I suppose that the validation rules are different for those 2 forms. So you will have a SearchCompanyViewModel where the Name field will not be required and a NewCompanyViewModel where the Name field will be required.