Best way of doing form validation in React Js - reactjs

In angular2, if we want to implement form validation we will use Directives.
In React, how can we achieve this validations ???

There are several ways:
Create your own validation from scratch with plain javascript
Use browser APIs
Or you can use 3rd party plugins like 'ract-validation': https://github.com/Lesha-spr/react-validation

After struggling with too many libraries on web, here is the one I have found more helpful in entire react world. It can make developers more productive by simplifying validation job.
Try using : Formik
npm install formik --save
Why you should use :
It will saves you writing too much boggy JSX (involving annoying
properties) and dealing directly with underline handling (writing own
onBlur() onChange() event handlers) just to achieve
validation.
Formik is a small library that helps you with the 3 most annoying parts:
Getting values in and out of form state
Validation and error messages
Handling form submission
By colocating all of the above in one place, Formik will keep things organized--making testing, refactoring, and reasoning about your forms a breeze.

You should try Formik.
I am currently working with it in a project and it goes quite well.
As per their readme.md in github they say:
"Formik is a small library that helps you with the 3 most annoying parts:
Getting values in and out of form state
Validation and error messages
Handling form submission
By colocating all of the above in one place, Formik will keep things organized--making testing, refactoring, and reasoning about your forms a breeze."

Related

FormContext returns 'null' inside SimpleForm on local

Hoping someone can point me in the right direction to try to debug this issue.
I'm refactoring an application built with react-admin to version 4.0.0.beta-3. But none of my custom forms are working correctly. Namely, when I call any of the new react-hook-form methods, like "useFormContext", I get back null. When I try to get the form values using "useWatch", I get back an error saying 'Cannot read properties of null (reading 'control')'
According to react-hook-form's docs, this may indicate that the element is not being provided with FormContext. However, when I built a codesandbox to verify the issue, useWatch works correctly and returns the form values. (Here is that codesandbox: https://codesandbox.io/s/crazy-violet-qk7uyh?file=/src/test-resource/TestResourceCreate.tsx )
If I copy the code nearly line-for-line into my local environment, it breaks, looking for "control" when I call useWatch.
I've tried deleting node_modules and reinstalling, I've installed react-hook-form at 7.27.0 (the stated version react-admin is using), but I have not found a solution.
Any suggestions for what to do/try next, or what could be wrong with my local, would be very much appreciated. Thanks!
EDIT: As an addendum: When I use the react-admin form component "FormDataConsumer", the form values are correctly passed in the prop "formData".
Answering my own question for posterity:
After refactoring my application to avoid having to flag --legacy-peer-deps on running "npm install", I was able to use useFormContext and all other react-hook-form methods as expected inside my react-admin custom forms.
The only package I'm aware of that required a legacy peer dependency was installing React at 16.9.0 while the rest of my application relied on 17.0.2. I'm unsure if that was source of my issue or if some other legacy peer was being installed that resulted in an incorrect import somewhere else. If anyone with more experience wants to share some insight into what could have been occurring to as to helper future searchers, be my guest.

submitting a django form via django-rest-framework and angularjs

I am confused about how to process my form(s) using django, django-rest-framework, angularjs, and django-angular.
Using standard Django techniques I can create a model form in my view and pass it to my template.
Using django-angular, I can ensure that the form has lots of pretty bootstrap3 styling and integrates well w/ angularjs.
Using angularjs, I can bind the form to javascript models and gets loads of cool interactive functionality.
Using django-rest-framework, I can load the initial form data via a RESTful API which returns JSON.
This all works great. I am just not sure what to do when submitting my form though...
Should I submit using an angular function that calls my RESTful API? Or should I submit using normal Django methods (ie: if form.is_valid(): form.save()?
I am refactoring this code from a pure Django app and the forms have some extremely complex custom validation methods. I am not sure that I can (or should) replicate that in angular.
Is there a "best practice" out there? Given that I can GET and POST via my RESTful API, what is the advantage to still doing it via Django?
note: I just thought that custom serialization validation should let me have the same level of complexity going through django-rest-framework as I previously had in pure Django. Whether this is a good idea or not is still a valid question.
note2: angularjs & django-rest-framework sure does seem faster.
It is common practice to submit the data through the API and let the serializer do most of the validation for you. You can then do custom validation if needed. You can take a look here for more information on custom validation if you need it. But it does a pretty good job and I rarely have to write any custom validation.
Some of the problems you may run into with trying to do it through pure django, is that it will mess up your SPA. Doing a POST request will require you to "leave the SPA" and return back to it. Other issues such as CSRF tokens are also a pain in the butt to try and figure out. Best to just use your REST API.

how to validate dynamically added controls in react js

I'm new to react framework and I'm creating the components dynamically (textbox,checkbox & select) and I would like to validate it before submitting the
Form. Can anyone suggest ideas?
React Forms might help you with front end form validation.
There's also Formsy-React:
https://github.com/christianalfoni/formsy-react
It lets you specify your form validation logic without using JSX, which is a plus if you want framework-neutral validation.

Angular.js vs React.js with php mvc (Laravel)

I know what angular.js is and I even had a question about it #Why use AngularJs in frontend if Laravel is already used as backend?.
but recently I started to read about React.js and from its site (its the V in the MVC) which is exactly what am after "handling the view and nothing else".
for me, I think Angular.js as an MVC framework was made to be used with something that is built with JavaScript from start to end like Node.js
and it seems like an overkill when used with something like Larval, where I simply need something to handle the frontend and nothing else + Angular have 2 main drawbacks
with the latest news about a new version that won't have back compatibility with the current version makes me even feared to start learning it just to find that more or less every project out there is using the old version which mostly is true.
angular renders the whole dom if anything got changed which again is an issue for big projects.
so based on the above, plz note that I want to learn/use JS solely to enhance the user experience not to build another Gmail or Facebook and so my question is,
could React.js be used with Laravel to handle the view and do everything Angular was going to give, or I have to use Angular liked or not?
could React.js be used with Laravel to handle the view and do everything Angular was going to give?
No
React is just for views. React components are stateful components with some really clever rendering stuff happening behind the scenes. To build a fully functional front-end app, you'd need to tie in some other code (or write it yourself).
React works well with Facebook's Flux architecture. I would suggest looking into that to learn how to manage the state of your react components.
What's key to understand here is that Flux and React are not parts of a large front-end framework. React is a library and Flux (as provided by Facebook) only provides a Dispatcher. The rest is up to you to implement. There are some pre-existing implementations you can look at if you need some help to get started.
What I like about flux is that it allows me implement things the way that fits my application best. React takes care of the heavy DOM lifting and is very easy to learn. Compared to Angular, I don't have to learn arbitrary conventions and gigantic APIs of a huge framework.

Page object design pattern in Angular's e2e tests

Can page object design pattern be implemented in angular e2e or does it follow some other design pattern ?
As far as I'm concerned there is this GitHub project called Astrolabe (by stuplum) which is an extension for protractor that adds page objects to your functional/e2e tests.
https://github.com/stuplum/astrolabe
However, at the time of writing this it has some bugs to fix and it seems to be a WIP project.
You can find here a question regarding Page Object pattern alternatives:
Page Object pattern and alternatives
The protractor getting started page now has a section about it.
Note protractor is now the AngularJS team preferred way for e2e testing.
Regarding Astrolabe i don't believe that's the way to go, see for example this leaking abstraction while using sendKeys on their readme
signInPage.username.sendKeys('a username'); // will fill the username input with the text 'a username'
While the proper way would be more like
signInPage.setUserName('a username');
I prefer to create my own page objects without the need of any extension like Andres shows here

Resources