Forcing all controls on MVC2 form to validate using JQuery - asp.net-mvc-2

Is there a way (using JQuery or Java Script) to force an MVC2 form to perform validation on it's fields with Data Annotation validation without posting back to the server?
I have a MVC2 form that is quite complex. Many of the fields are hidden or displayed depending on other selections. Given this, some of the fields are validated using Data Annotations and some are validated using custom JQuery.
In the case that one of the fields with custom validation fails it's validation I wish to prevent the form from posting back however this stops any of the fields with Data Annotation Validation from working.
Thanks.

IMHO mixing jquery validate with MSAjax is a bad thing. For complex forms with many custom validation rules I would recommend using only jquery validate and on the server a more powerful validation framework than DataAnnotations such as FluentValidation.NET which works nicely with ASP.NET MVC.

Related

Typo3 Forms framework and frontend overlay

I was browsing the typo3 core Forms framework documentation but with no relevant answer to my requirements which are:
The form has to be displayed in a frontend overlay.
The filling process involves multiple steps where the user would be able to go back and forth.
The form fields must still be editable by a redactor.
I'm not sure about how the form framework behaves, so far I remember I think that multiple steps are configurable from the backend module but I don't know if it sends request to the controller after each step or if it sends everything only on submit.
I have an idea about how to implement it though, it's based on this question how to get a typo3 form framework html via ajax. Which would just let me provide the whole html content to the frontender and let him split the whole form into steps. The separation would be based on the addition of some special tags via the editor that would surround the fields you want in each step.
What do you think about that approach?
The form framework proceeds each form step seperately. So without developing your own form runtime, you have to keep proceeding every step seperate.
I see two possibilities:
1. Send each form step from frontend to the form controller and replace the response (html form) in the frontend.
That is the fast and easy way, as you use the existing form runtime.
Prepare a page which returns the rendered form as html
Fetch this page by JavaScript
Send the form data back to the given form action
The form controller proceeds the form with all its validators, rules and finishers and returns the next step, previous step, the current step with existing errors or the finishers response on success
Replace your form in the frontend with the already rendered html response of the form framework
The advantage of this way: Less effort and you can rely on the already existing validators, as you get an already validated response.
The disadvantage of this way --> it is more difficult to implement frontend validation, as you have a mix between frontend and server side validation.
2. Make the form framework kind of headless and work json based
In my opinion the better approach, but with a lot more effort to take.
You have to extend / overwrite the controller and the form runtime. This allows you more flexibility in handling the form by JavaScript and e.g. return the errors in a json object. It makes life easier when you want the form render and handle with a JS framework like react or vue.
To your question:
What do you think about that approach?
If I got it right, you want to keep ONE form step in the backend, but let the editor divide this form step into multiple steps by adding tags? You can try, but I don't see any real advantage in keeping the original form steps and proceed every step by sending the step to the controller and handle the response (like mentioned in 1.)
Summary:
In the past, I was thinking a lot about handling forms by JavaScript and came to the conclusion:
Keep the form framework's behaviour completely untouched with server side processing or make it frontend driven, with an own runtime. All mixtures between client and server side rendering will sooner or later run into bigger problems or at least a high effort. The form framework is pretty complex with a lot of possibilities, hook driven behaviour, etc. From my experience, you have to know it pretty good to develop without loosing control. In smaller projects with just one or two basic forms, I would try to avoid special cases with lots of JS. In bigger projects (with more budget), I would definitely go with my second mentioned approach (currently, I'm developing vue.js based rendering and handling of the form frontend). But these are just my five cents...

Angular4 automatic forms validation messages

I'm diving into Angular4 forms validation. I was wondering if there is any library that introduce validation of controls in a template driven form.
What I'm looking for is a library that insert automatically the "Required." text field for every form element tagged as required, and so on for the other standard html5 validation attributes.
There are several good libraries for form validation available,
for example Angular2 Form Utils. It's pretty popular and powerful, and it also assists with error handling.
If this isn't what your looking for, npm has several others

Symfony2 And Single Webpage Applications using a framework like AngularJS

(If this is not the right place to post this kind of question I'd happily post it somewhere else)
I'm trying to build an interactive web application to manage company resources.
I have experience with Symfony2 but I kind of hit a wall with this new application.
I'd like to make this application quite interactive on the client side. Almost a full single webpage application.
My previous web applications would normally just use a typical MVC pattern with CRUD pages.
In those simple applications I would have
/employees/
/employees/create
/employees/detail/45
/employees/update/45
/employees/delete/45
Using symfony in this kind of application would give me a lot of advantages:
Routing
Security (CSRF tokens)
FormTypes and Form handling
Validation
Integration with Doctrine
Twig
Especially functionality like this in Twig was very refreshing (since my models were build as Doctrine entities):
<p>{{ employee.getCurrentTask().description }}</p>
The problem I'm facing now is that I feel like Symfony2 isn't really build for single webpage applications. As soon as I try to add some Ajax functionality I'm faced with these problems:
CSRF tokens invalid
Too much non reusable view/presentation logic in jQuery
Adding data-attributes in html to get id's etc...
I then looked into Knockout.js and Angularjs but then I feel like lose all of the advantages of Doctrine and Twig. I have to rebuild my models on the client side anyway and have to maintain them in two different locations then.
So I came up with this idea:
Use Symfony2 models and controllers to persist data to the database but let controllers in symfony just send out JSON and receive JSON (FOSRestBundle maybe?)
Use a framework like AngularJS or KnockoutJS to rebuild that JSON data on the client side to use 2-way binding.
But then how would I tackle the issues like Doctrine2 Relationships, Form Validation, CSRF which Symfony already solved but are unusable if I use a frontend js framework?
All suggestions are welcome!
Some words about JSON, Serialization and Models
Simon, I faced exactly the same questions and problems. First like ken already mentioned. You don't need to rebuild any model. Better use FosRestBundle and/or JMS Serializer. It turns you entities with relations into JSON objects. This objects are transferred via api to your frontend and you can work with them just like in twig, when you use angular.js like this
{[{ user.username }]}
is as same as in twig. But remember that you have to set custom brackets for angular because by default it uses the same as twig.
Routing
You talk of a single page application, so symfony's routing is kept on a low level to have few page refresh. Instead you have to use routing of your frontend framework, because I am only familiar with angular.js, I give an angular example:
app.config(function($routeProvider, $interpolateProvider) {
//here you go, custom brackets
$interpolateProvider.startSymbol('{[{');
$interpolateProvider.endSymbol('}]}');
$routeProvider.when('/user', {
controller: UserController,
templateUrl: Routing.generate('suser_list')
}).when('/ticket', {
controller: TicketController,
templateUrl: Routing.generate('ticket_list')
});
});
When you hit a link like
Go to tickets
AngularJs will know which frontend controller to trigger. Pretty great stuff, without page reload. Also have a look at FosJSRoutingBundle. It allows you to generate symfony routes in javascript, I use them to link js controllers with html templates where data is pushed in.
FormTypes, Form handling, Validation
Well, when you use a frontend framework like angularjs, your symfony form types are pretty useless. But I am not sure. Remember data is pushed and pulled via api as json, I think this would be a hard job for form types to handle this kind of compexity.
For validation you can use angular's live validation or have you symfony's validation in the backend, no problem. It might be a good thing to use both, client and server side validation.
Twig
Twig is out of the race. All data is rendered on the client side, and not pre rendered on server side like with twig. But that is only the case if your application is really a single page application. Of course you can use twig, but it will only refresh if you reload the entire page.
Integration with Doctrine
You can still use doctrine in the backend. Do you have a specific question regarding doctrine and SPA?
You don't need to rebuild the model in client. I normally just create a service in angularjs that provides json data. Data manipulation still happens server side using ajax.
For forms that requires csrf, I normally just send the html rendered by twig via json. Or you can serialize $form->createView() with jms serializer. However you will need some client script to transform the json data to actual form controls.

Is there a way to allow a user to submit html content while still enabling model validation?

I need to allow users to submit a form value containing html in their text inputs. This is an internally-facing application so it's reasonably safe to do so. I have succesfully used the
[ValidateInput(false)]
attribute on the method in question, but this inhibits all model validation for the method/view model in question, but I only want to allow html in one of the TextBoxes and do not necessarily wish to write my own guard clauses for every other piece of model validation in the same method/view model, when I could would prefer to continue using Data Annotations for all of the other properties in the view model.
It's too bad I cannot apply the [ValidateInput(false)] to only a single property of my viewmodel. I would assume that I need to override mvc's default model validation, but I cannot find any documentation on how to do so. Every search yields results describing only how to write my own custom validation attributes, which isn't correct for the problem I'm trying to solve.
Thanks!
You would have to upgrade your aplication to ASP.NET MVC 3. There you have AllowHtmlAttribute, which you can use to disable input validation on property level. ASP.NET MVC 3 is backward compactible with ASP.NET MVC 2 so the upgrade should be easy.

ASP.NET MVC 2 EnableClientValidation : validation group

I'm using ASP.NET MVC 2, DataAnnotation and MicrosoftMvcValidation.js for validation. I have two forms in my view..is there a way to use Validation Group for client side validation (like in ASP.NET Webforms).
thanks
The example I've seen with the nerd dinner (http://www.asp.net/mvc/videos/what-is-aspnet-mvc-80-minute-technical-video-for-developers-building-nerddinner) uses a partial class to modify the dinner model by adding validation to specific elements. This way validation can be handled at the server level but without server controls.
But... you can still use the validation controls with asp.net mvc if that's what you mean.
Did you mean to show summary of error(s)? If so you can use HTML helper for validation summary, like Html.ValidationSummary(...).
Hope this helps!