Comments (7)
you are right! want to send us a PR to fix it?
from documentation.
@bluebill1049 sorry I planned to actually create a PR but somehow it went forgotten
there is still an issue with your commit, there is still
if (!errors.name) return null;
which imho is still wrong
cheers
from documentation.
please send a PR to help @jossnaz :)
from documentation.
please send a PR to help @jossnaz :)
I will gladly help. The problem I have, and that's me, is that I don't think the recommended solution is complete. Even with the tipo fixed.
The question I have, how do you use your library for error components?
because what I ended up doing is maybe not optimal.
What I realized is that I need access to the error state on different levels of the component tree, otherwise it's bad. So I exposed the function to access my error message... I read about the FormContext in the comment, but a google search auto corrected my search to formviks solution, so I didn't find the actual formcontext referenced in the code, so i quickly gave up on that formcontext (...)
So maybe even a little hint and with link. And I honestly don't think it's good to hide that component in advanced usage at all. That should be, honestly, the last step of the getting started.
What I have written so far, and I am only using the required error so far, is this:
Example component making use of it:
import React from 'react';
import PropTypes from 'prop-types';
import myappInput from "./myappInput";
import ErrorMessage, {getErrorMessage} from "../ErrorMessage";
const myappGDPRagree = React.forwardRef((props, ref) => {
return (
<>
<myappInput id="gdpr_agree_el" type="checkbox" name="GDPR_Agreement__c" ref={ref}/>
<label
className={"myapp-input-checkbox-label " + (getErrorMessage(props.errors, "GDPR_Agreement__c") ? ' myapp-error ' : '')}
htmlFor="gdpr_agree_el">
<span>I consent to having MYAPP store my submitted information and sending me future communications on phishing threats and solutions</span>
</label>
<ErrorMessage errors={props.errors} name="GDPR_Agreement__c"/>
</>
)
});
myappGDPRagree.propTypes = {
errors: PropTypes.object,
};
export default myappGDPRagree;
Basic error component:
import React from 'react';
const messages = {
test: {
min: "This field required minLength of 8"
},
required: {
message: '* This field is required.'
}
};
export const getErrorMessage = (errors, name) => {
if(!errors){
return null;
}
if(errors[name] && messages[errors[name].type] && messages[errors[name].type].message){
return messages[errors[name].type].message;
}else{
return null;
}
};
const ErrorMessage = ({ errors, name }) => {
// Note: if you are using FormContext, then you can use Errors without props eg:
// const { errors } = useFormContext();
if (!errors || !errors[name]) return null;
if(errors[name] && messages[errors[name].type] && messages[errors[name].type].message){
return <span className="myapp-field-error">{messages[errors[name].type].message}</span>;
}else{
return null;
}
};
export default ErrorMessage;
from documentation.
The question I have, how do you use your library for error components?
What's the confusion in here? I thought our errors object is flat? you just need the name of your input.
https://react-hook-form.com/api#errors
from documentation.
What's the confusion in here? I thought our errors object is flat? you just need the name of your input.
yes, the question basically was whether and if yes, how do you wrap the error message? Don't you create an 'errormessage' component? because right, error messages should look basically the same over the whole app, and as well style the inputs depending on whether there is an error or not. Like, make a red ! and wrap it in a red border or something the like.... Or maybe I'm over complicating things? what would be the best approach in your opinion to do that? because how I do it is using the ErrorMessage component from that docs, but that isn't enough. I have to style the parent to get the right design. That's why I added a function getErrorMessage
which I can use to peak if there is an error, then add a class to the parent to get the error styling right for the parent components. But I'm not entirely convinced, that that is the best way.
from documentation.
It's really up to you on how to handle the errors.
at work, we accept error as a prop and display the error with style and message (which inside the error object). so basically baked inside the input/select component.
from documentation.
Related Issues (20)
- issue: error in docs
- Issue: Documentation Discrepancy: Importing Material-UI Input in React Hook Form HOT 1
- Issue: Documentation link incorrect HOT 1
- Typo: "recommend" instead of "recommended"
- issue: Application error: a client-side exception has occurred (see the browser console for more information).
- Improve documentation for useWatch exact
- Update twitter icon HOT 2
- issue: useFieldArray Unable to track newly added elements When "shouldUnregister" is enabled HOT 1
- The `RegisterOptions` type does not match the documentation
- Document watch callback type parameter
- issue: In the specified sandbox environment, when the onChange event handler is invoked with undefined as its argument, the Component does not receive the updated value. However, despite this, the form data is successfully updated. HOT 4
- issue: maxLength does not limit input field to length HOT 1
- Two different documentation versions
- issue: Website appears as a blank background HOT 2
- Documentation: keepIsSubmitSuccessful missing from documentation
- 🥇 Features : Scrollspy or scrolling navigation on documentation site HOT 2
- Feature Request: Implement Scrollspy for Dynamic Navigation
- issue: typo/grammar-issues in /docs/useform/seterror HOT 2
- form builder: "input creator" form state not updated correctly when switching between inputs
- Beekai.com outbound connection on homepage? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from documentation.