Skip to content

Fix validation class and example #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix validation class and example
  • Loading branch information
Seth Lemmons committed Jan 18, 2018
commit bae8133051a8b48cf3ea04be6418e827080cf55e
125 changes: 64 additions & 61 deletions 14-form-validation/a-validation/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,83 @@ import React, { Component } from 'react';
import './App.css';
import '../node_modules/purecss/build/base.css';
import '../node_modules/purecss/build/forms.css';

class Field extends Component {
static defaultProps = {
value: '',
type: 'text'
}
state = {
invalidmsg: this.props.invalidmsg,
value: this.props.value
}
render () {
const {
name,
id = name,
label,
onChange
} = this.props;
const { value, invalidmsg } = this.state;
const inputProps = {
...this.props,
id,
value,
onChange: (e) => this.handleChange_onChange(e, onChange)
};
return (
<div className="pure-control-group">
<label htmlFor={id}>{label}</label>
<input {...inputProps} style={{minWidth: '200px'}} />
{
invalidmsg ?
<span className="pure-form-message-inline">{invalidmsg}</span> :
null
}
</div>
);
}
handleChange_onChange (e, onChange) {
this.setState({
value: e.target.value
});
this.validateField(e);
if (onChange && typeof onChange === 'function') {
onChange(e);
}
}
validateField (e) {
const { target } = e,
{ validity } = target,
{ valueMissing, valid } = validity;
let invalidmsg;
if (valueMissing) {
invalidmsg = 'This is a required field';
} else if (!valid) {
invalidmsg = target.validationMessage;
}
this.setState({ invalidmsg });
}
static defaultProps = {
value: '',
type: 'text'
}
state = {
invalidmsg: null,
value: this.props.value
}
render () {
const {
name,
id = name,
label,
onChange
} = this.props;
const { value, invalidmsg } = this.state;
const inputProps = {
...this.props,
id,
value,
onChange: (e) => this.handleChange(e, onChange)
};
return (
<div className="pure-control-group">
<label htmlFor={id}>{label}</label>
<input {...inputProps} style={{minWidth: '200px'}} />
{
invalidmsg ?
<span className="pure-form-message-inline">{invalidmsg}</span> :
null
}
</div>
);
}
handleChange (e, onChange) {
this.setState({
value: e.target.value
});
this.validateField(e);
if (onChange && typeof onChange === 'function') {
onChange(e);
}
}
validateField (e) {
const { target } = e,
{ validity } = target,
{ valueMissing, valid } = validity;
let invalidmsg;

if (valueMissing) {
invalidmsg = 'This is a required field';
} else if (!valid) {
invalidmsg = this.props.invalidmsg || target.validationMessage;
}
this.setState({ invalidmsg });
}
}

class App extends Component {
render() {
return (
<div>
<Field
invalidmsg="Field is required"
required
name="test"
label="test field"
label="required text field "
onChange={(e) => console.log('change', e)}
/><br/>
<Field
invalidmsg="Field is required"
type="number"
max={20}
name="test"
label="test field"
label="number field (20 max) "
onChange={(e) => console.log('change', e)}
/><br/>
/>
</div>
)
}
Expand Down