Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Flux form is a helper for managing forms with flux
import fluxForm from 'flux-form'
import { Dispatcher } from 'flux'
const dispatcher = new Dispatcher()
const form = fluxForm('MyForm', dispatcher, {
fields: [
'name',
'email',
],
state: UserStore.getState(),
})
fluxForm
will return a form object which contains all the fields you defined,
a save action, a cancel action, and the current form's state.
class SampleFormComponent extends React.Component {
render() {
return (
<div>
<label>
Name
<input {...form.props.name} />
</label>
<label>
Email
<input {...form.props.email} />
</label>
<input type="button" onClick={form.save} />
</div>
)
}
}
...form.props.name
will add all of the props for the field name
onto the
input. The two important props are value
which contains the current value,
and onChange
which is a function handler that sets the value.
Flux Form is meant to be bare bones and generic in that you can build adapters on top of it so it works with your favorite flavor of flux.
For example, we can have an Alt specific form utility which creates a store that you can connect your React component to.
You can provide basic validation that can be triggered manually and also runs automatically whenever the form is saved.
const form = fluxForm('MyForm', dispatcher, {
fields: [
'username',
'password',
],
validators: {
// you can asynchronously validate a field by returning a promise
// for example, you can validate that the username is not taken here
// by making an API call and rejecting the Promise
username(data) {
return new Promise((resolve, reject) {
xhr.post('/validateUsername', { user: data }, (res) => {
if (res.statusCode === 200) {
resolve()
} else {
reject(res.responseText)
}
})
})
},
// you throw an error if it does not pass validation
password(data) {
if (data.length <= 6) {
throw new Error('Password must be longer than 6 characters')
}
},
},
})
The validation can be triggered and it returns a Promise. You can also pass in a node-style callback if you prefer.
form.validate().then((state) => {
// the successful state of the form
}, (errors) => {
// a key value pair of errors
// for example:
// {
// password: 'Password must be longer than 6 characters',
// }
})
You can also validate specific fields by passing in an Array to validate.
form.validate(function (err, state) {
// do something with err or state
}, ['username'])
Form fields are normalized either manually or automatically when saving.
const form = fluxForm('MyForm', dispatcher, {
fields: [
'phone',
],
output: {
// some really crappy normalization lol
phone(data) {
// take the phone number and format it so it is without dashes and parens
// and then add a +1 at the beginning
return '+1' + String(data).replace(/[ |(|)|-]/g, '')
},
},
})
To trigger a normalization
form.normalize()
focus and blur actions are called whenever an element receives focus or is blurred.
In order to save your form state which will trigger validation and normalization you can call save on your form object.
const form = fluxForm('MyForm', dispatcher, {
fields: [
'address',
'city',
'country',
],
})
form.save().then((state) => {
})
Save returns a promise but you can also pass it a node-style callback.
Flux form works by firing off flux actions with the dispatcher you've provided. The actions are dispatched in FSA format.
The following actions are provided
You use getActionCreators
in order to get the action creators. Action creators
are functions which return an FSA compliant payload ready to be dispatched.
Each action creator has an id
field which is the string that you can use in your store
in order to handle the dispatch.
import { getActionCreators } from 'flux-form'
const { saved } = getActionCreators('MyForm')
// sample usage
alt.dispatch(saved('foobar')) // { type: 'MyForm/saved', payload: 'foobar' }
Flux form will automatically dispatch change events, saved, focus, etc, which you can then listen to yourself in order to keep track of state separately or in order to sync items.
FAQs
Manage your forms with flux, any flux
The npm package flux-form receives a total of 0 weekly downloads. As such, flux-form popularity was classified as not popular.
We found that flux-form demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.