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!
Related
I have EntityField and UsersField(ListBox). When I change Entity to USERS, I need to pass: UsersLookupCall, when I change Entity to Projects, i need to pass ProjectUsersLookupCall to UsersField.
Also if that is possible, how to pass a parameter to that lookup call I am passing to another field?
Thanks
You can exchange the lookup-call on a (Smart-)Field simply by calling the setLookupCall(ILookupCall<T>) method on the field. However, since the value of the field is strongly typed and the type of the lookup must match the type of the field you can only set lookup calls with the same type.
You find a lot of examples on how to react on value changes in the Scout Beginners Guide.
You can set additional parameters on the lookup call by implementing the execPrepareLookup(ILookupCall<T>) method. Note: there are also specialized execPrepare* methods for the three lookup modes: key, text and rec.
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
As a reference, the most similar question already asked is: https://stackoverflow.com/questions/8667849/making-xforms-enforce-the-constraint-and-type-model-item-properties-only-when-fi The difference is that I cannot use the 'relevant' property since I do want the field to be visible and accessible.
I'm attempting to make an XForms form that has the following properties:
It displays a text field named 'information'. (for the example)
This field must not be required, since it may not be necessary to enter data. (Or this data will be entered at a later time.)
However, if data is entered in this field, it must adhere to the specified constraint.
I cannot mark the field as not relevant since this would hide the field and some data may need to be entered in it.
The trouble now is that even though the field has no data in it, the constraint is still enforced (i.e. even though it is not marked as 'required').
I have taken a look at the XForms 1.1 specification, however it does not seem to describe how the properties 'required' and 'constraint' should interact.
The only option I see, is to add a part to the constraint such that an empty value is allowed.
e.g.:
. = '' or <actual-constraint>
However, I don't like this. It feels like a workaround to add this to every such field.
Is there any other way to express that non-required fields should not need to match the constraint for that field? (Am I missing something?)
In XForms 1.1, required serves two purposes:
mark the field as required (implementations can style controls to reflect this, e.g. with a "*")
take part in the validation process
The latter is described in 4.3.3 The xforms-revalidate Event.
An instance node is valid if and only if the following conditions hold:
And one of the conditions is:
the value is non-empty if the required model item property is true
So it is a logical and between all aspects that impact validation.
I can see how things could have been different, e.g. saying required="false()" could disable the rest of the validation. However that's not the approach XForms is taking.
Based on this there is nothing wrong checking for emptiness as part of the constraint.
XForms 2.0 might add custom XPath functions, which might help with reuse of logic:
<bind ref="information" constraint="my:constraint(.)">
Also, if the constraint can be expressed with a type, you may be able to use one of the schema types in the XForms namespace, which allow empty values to be valid. For instance xforms:double considered the empty string and 42 to be valid values, but not gaga.
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.