Import Yii2 validators in angular forms - angularjs

I am developing an application that uses the yii2 framework to handle data models and angularJS for the frontend counterpart. In some views, I need to handle the input fields with angular forms (i.e., I cannot use Yii2 ActiveForms) but, in this way, I cannot take advantage from the client side validators that the yii2 frameworks offers (the ones that are automatically generated by the ActiveRecords rules). Is there a way to get the Yii2 client side validators and attach them to the angular forms? Any suggestions?

Related

Integrate .NET Core 3.1 Web API with React App as MVC Application

I have two isolated applications one as a Web API using .Net core 3.1 and another one for Client-side using React and the two apps contact each other by HTTP requests now the challenge is I wanna integrate both of them as MVC so what is the recommended steps to do the integration.
If you'd like to migrate your existing WebAPI backend and React frontend apps to ASP.NET Core MVC, you may need to implement these functionalities from scratch in your new ASP.NET Core MVC project.
You can check each React component and the data passed/handled in that component, then create corresponding controller action and view page to implement same in MVC.
Note: normally WebAPI backend response and return data to React frontend, then we handle data on frontend and render it to expected html. In MVC, controller action usually return view result and pass view model data to view for rendering the web page.
For more information about "Passing data to views", please check this doc:
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/overview?view=aspnetcore-5.0#passing-data-to-views
Besides, partial view and view component in ASP.NET Core enables us to implement a reusable and modular unit, we can implement reusable rendering logic as partial view or view component, then invoke it in other view.
For details about partial view and view component, please check these docs:
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-5.0
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-5.0

Can I use Data Annotations Validation on my POCOs and jQuery validation if I develop the front end in Angular 1 instead of Razor?

Normally when developing in ASP.Net MVC, we put our Data Annotions on our POCOs and use isModel.Valid on the server side and unobtrusive jQuery validation on the front end so we don't have to duplicate and specify our validation requirements twice.
But I am about to try writing my first app with an Angular/Rest SPA architecture (ASP.Net Web API of course, more specifically, ASP.Net MVC Core).
Is there a way to get the Angular Forms to validate against the POCO validation attribute rules and keep it DRY? Or am I going to have to maintain it in both places? Client and Server.
What is the best practice here?
Can someone point me to an example if there is a correct way?

what does AngularJS has to offer to an ASP.NET MVC project? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I heard a lot about how AngularJS is so much better than jQuery for communicating with server.
But all i found was replacing C# controllers and cshtml views with purely client-side javascript files. And i have no interest in that.
is anything that I'm missing?
I am currently using jQuery Ajax for communication between client and server through JSON. I was looking forward for a better solution for this purpose.
Once a controller returns a view, what is sent to the browser is pure HTML. Razor is rendered server side as HTML. Or your action can return pure JSON.
Therefore, whether you want to use jquery, angularjs, or any other JS/HTML based library, there is really no restriction there imposed by MVC. From the perspective of client side code, controller actions are just URLs that return return HTML or JSON.
Additionally, MVC controller actions which return JSON, or WebAPI controllers, are perfect RESTful endpoints for your AngularJS to interact with. If you need HTML fragments for refreshing dynamic areas of a page, then a controller action returning a partial view will come down to the client as an HTML fragment. So your interactive requests through Angular can still leverage your backend architecture of Controller/Business/Data layers.
What Angular Offers MVC
AngularJS offers an MVC application the same thing it offers any web applicaion. It is a great way to abstract the presentation of UI elements from the interactivity of those elements with the backend. The problem with common approaches with jquery is you react to interaction with specific HTML elements via selectors such as $('.someClass').on(... and then in the handler collect information from the element about what was clicked and submit that in an ajax request, and then take the response and update specific elements $('.areaToUpdate).html(response)`.
So you have three concerns not properly separated:
What element responds to interaction(button click, drop down change, item drag/drop)
The interaction with the server to compile a request, submit it, and parse response.
Updating the UI based on the server's response
Often times because of the way we try to make this code less fragile, you are restricted to a logical hierarchy of objects that also matches your UI's HTML element hierarchy. Long story short, the lack of proper separation of concerns means changing any one will impact a chunk of code that has multiple purposes, and thus changes are more challenging because you have certain restrictions imposed due to lack of decoupling.
AngularJS provides a client side separation of these concerns by using a client side model as the intermediary between these concerns. UI interactions update the model, other code responds to changes to the model and makes server requests which update the model in response, and yet other code can respond to those changes and update the UI in response to the model changing. You are free to modify the UI's structure, and have a much more smaller set of code to update to get the UI changes wired up.
The fact that AngularJS is modeled from an MVC pattern makes it seem as though it would be mutually exclusive with ASP.NET MVC. However, they serve two different purposes. ASP.NET MVC provides separation of concerns for the server side querying of data, population of server side model, and rendering of server side HTML. It also provides a great way to support the rendering of JSON or HTML fragments(partial views) that might be leveraged by a client side interactive framework. AngularJS provides a model appropriate for writing clean client side code for highly interactive pages.
Personally, I think they are very complimentary.
Trying to do highly interactive pages with jquery+ASP.NET MVC is doable and can be efficient, but AngularJS adds a lot to the maintainability of the client side code. ASP.NET MVC provides a good framework for your server side logic. Additionally, if you have simple pages that fit a more traditionally PRG pattern or simply rendering HTML content, then ASP.NET MVC gives you an efficient way to quickly implement these pages without the unnecessary boilerplate code you'd need with Angular. In other words, you can choose to use AngularJS only on the pages that will really benefit from it.
You can mix MVC and angularjs with out completely removing MVC controllers.E.g if you have 5 modules on your app create that parent routes on MVC routing.(E.g Students,Teachers,Attendance etc..)
initially render your first view say student view renders through asp.net MVC then include all your JS files(including states related to that module) on that view(e.g students.cshtml)
.JS files related to this module may be
studentModule.js,studentController.js , studentService.js etc.
Do all your CRUD operations on angular JS.
Here your studentService will interact with MVC controller for all your db operations.
Benefit you are getting with this is
you are interacting with your server asynchronously and you can use
all angularjs features like(Directives,resources etc)
You can authorize all your MVC controller actions with asp.net
identity.
-Also to render any html template on state you can call your MVC controller action from templateUrl.return csHtml from MVC controller
action.
There are lot of applications on live using both asp.net mvc and angular js. here are few things i know.you can check some tutorials for better understanding.
.
Angular and ASP.net are both MVC frameworks. They are in a sense at odds with each other - I work on an Angular / ASP.net project where we serve and initial ASP.net view and then let Angular SPA routing take over, but its kind of a hack and I sometimes which we separated the two and had a plain WebApi implementation with Angular front end.
Using AngularJS just for its Http library is like using a sledge hammer when a hammer will do. You are wasting bandwidth in downloading large amounts of unused JS.
I would look to a pure JS library to handle your Http communication if you are unhappy with JQuerys implementation.
Axios is an amazing example of one of these libraries and uses Javascript promises just like Angular - which make the whole process really neat and easy.
Good luck!

Can AngularJS work in Java EE MVC Architecture : JSP->Servlet->EJB3.1->Business Object->DAO

I have a quick question on considering AngularJS (current stable version - 1.3.9) for an upcoming application that we are building on an existing framework. The current framework has a Java EE MVC architecture and here are the current components mentioned in sequential order in which they get invoked:
View - JSPs: This layer gets as response Java Objects and we use jsp:useBean to access its properties and display on screen.
If any modifications are done on the page, it goes through a ControllerServlet i.e a Java file which has code to access the HttpRequest and HttpSession related information. The controller also does a lookup in JNDI to find the name of the bean to invoke based on the HttpRequest parameter name, e.g. PageId
Once the EJBBean lookup is returned, the controller invokes an EJB 3.1 "no-interview" view - These are Stateless Beans annotated with #Stateless
EJBBean classes then invoke BusinessObject classes, we call them "BO" which internally gets referenced by the DAO interface
A DAO implementation class is the one which is responsible for CRUD operations
Our Problems as of now:
The view is tightly coupled to Java Objects that are returned from DAOs and since the response is not converted to JSON, a lot of scriptlet code is used to display their value (I know scriplets are oldskool, but being a legacy solution there is no choice)
jQuery is used to manipulate the DOM before sending it to the controller layer
View is not the official record of whats happening on the screen, unlike AngularJS where I could easily understand
Developers write custom CSS for different browsers manually
Proposed Solution
View shall be designed for the new application using AngularJS
Take advantage of Bootstrap css classes which has readily available CSS which can be combined with AngularJS
Each request goes to ControllerServlet using $http service to ensure we use existing MVC architecture i.e routing every request through Controller
EJB Layer to be RESTFul to return data in JSON Format
Viewport specific css code for responsive web design - i.e same screen should render on multiple devices and platforms
Questions:
Is the proposed solution feasible? What are the downsides?
Is it a good practice to reference EJB Bean classes as RESTFul services?
Do we get access to all the Java EE objects / interfaces using AngularJS? For e.g. HttpRequest, HttpSession, etc.
Will it help in performing better by switching to this architecture?
Question from Management - Why not stick with jQuery! - Probably the hardest of all the questions to convince the management of Angular's benefits
Hope you guys can help throw some suggestions
i'm using spring 4.0.1 , hibernate 4.3.5 ,jackson 1.9.2 , I'm creating a RESTful webservice that returns a data in JSON format and angularjs in front-end which is loosely coupled with back-end.
concerning your questions;
the solution is feasible of course , until now there no downsides
except session management is tricky because Restful ws is stateless
.
it's provide a very elegant feature that you can access all your
functionality via any application web-client-side, desktop or
mobile.
angular app is fully isolated from back-end app you can maintain
requests and session in the server and respond with what you like in
json format to angular
same as 2
i hope it's helpful

ASP.net Web API: Include validation rules when retrieving models with Data Annotations

I'm currently building a Backbone.js/ASP.net MVC/Web API application and everything is going very smooth :)
When sending data to the server I know I can use ModelState and a ValidationFilter to validate there. However, when retrieving a model from the server, I'd like to include any validation rules that come from Data Annotations so that I can hook them into JQuery Validate or whatever I decide on. In ASP.net SPA you can call dataSource.getEntityValidationRules() from javascript to do exactly that.
I was wondering if there was a way to include these rules whenever I get a model with data annotations from Web API without the use of ASP.net SPA and its javascript libraries?
The benefit of backbone models is that those are are dynamic, so you can create a parser to read the validations form the server and add those validations to the backbone model in the way your pluggin need it, its kind of easy if you only use: required, minlength, max length and a regex but gets harder with ranges or some other kind of validations.
As I said plug those validation requires a some work to build this parser as I think there is no plugging or library for that.

Resources