Revert "[Ivan Diaz] - OS-#10 - Add validations [skip ci]"

This reverts commit 8d7d0a93bf.
This commit is contained in:
Ivan Diaz 2016-05-22 15:36:24 -03:00
parent 8d7d0a93bf
commit 3c22a2e8af
10 changed files with 42 additions and 122 deletions

View File

@ -33,7 +33,7 @@ let MainHomePageLoginWidget = React.createClass({
<Widget className="main-home-page--widget" title="Login">
<Form className="login-widget--form" onSubmit={this.handleLoginFormSubmit}>
<div className="login-widget--inputs">
<Input placeholder="email" name="email" className="login-widget--input" validation="EMAIL"/>
<Input placeholder="email" name="email" className="login-widget--input"/>
<Input placeholder="password" name="password" className="login-widget--input" password/>
<Checkbox name="remember" label="Remember Me" className="login-widget--input"/>
</div>
@ -53,7 +53,7 @@ let MainHomePageLoginWidget = React.createClass({
<Widget className="main-home-page--widget main-home-page--password-widget" title="Password Recovery">
<Form className="login-widget--form" onSubmit={this.handleSubmit}>
<div className="login-widget--inputs">
<Input placeholder="email" name="email" className="login-widget--input" validation="EMAIL"/>
<Input placeholder="email" name="email" className="login-widget--input"/>
</div>
<div className="login-widget--submit-button">
<Button type="primary">Recover my password</Button>

View File

@ -1,40 +1,35 @@
const React = require('react');
const _ = require('lodash');
import React from 'react';
import _ from 'lodash';
const {reactDFS, renderChildrenWithProps} = require('lib-core/react-dfs');
const ValidationFactory = require('lib-app/validations/validations-factory');
import {reactDFS, renderChildrenWithProps} from 'lib-core/react-dfs';
const Input = require('core-components/input');
const Checkbox = require('core-components/checkbox');
import Input from 'core-components/input';
import Checkbox from 'core-components/checkbox';
const Form = React.createClass({
let Form = React.createClass({
validations: {},
getInitialState() {
return {
form: {},
validations: {},
errors: {}
form: {}
}
},
componentDidMount() {
let formState = {};
let validations = {};
reactDFS(this.props.children, function (child) {
reactDFS(this.props.children, (child) => {
if (child.type === Input) {
formState[child.props.name] = child.props.value || '';
validations[child.props.name] = ValidationFactory.getValidator(child.props.validation || 'DEFAULT');
}
else if (child.type === Checkbox) {
formState[child.props.name] = child.props.checked || false;
validations[child.props.name] = ValidationFactory.getValidator(child.props.validation || 'DEFAULT');
}
}.bind(this));
});
this.setState({
form: formState,
validations: validations
form: formState
});
},
@ -60,65 +55,37 @@ const Form = React.createClass({
if (type === Input || type === Checkbox) {
let inputName = props.name;
this.validations[inputName] = props.validation;
additionalProps = {
ref: inputName,
value: this.state.form[inputName] || props.value,
error: this.state.errors[inputName],
onChange: this.handleInputChange.bind(this, inputName, type)
onChange: this.handleInputChange.bind(this, inputName, type),
value: this.state.form[inputName] || props.value
}
}
return additionalProps;
},
handleSubmit(event) {
handleSubmit (event) {
event.preventDefault();
if (this.hasFormErrors()) {
this.focusFirstErrorField();
} else if (this.props.onSubmit) {
if (this.props.onSubmit) {
this.props.onSubmit(this.state.form);
}
},
handleInputChange(inputName, type, event) {
let form = _.clone(this.state.form);
let errors = _.clone(this.state.errors);
let inputValue = event.target.value;
form[inputName] = inputValue;
errors[inputName] = this.state.validations[inputName].validate(inputValue, form);
form[inputName] = event.target.value;
if (type === Checkbox) {
form[inputName] = event.target.checked || false;
}
console.log(errors);
this.setState({
form: form,
errors: errors
form: form
});
},
hasFormErrors() {
return _.some(this.validateAllFields(), (error) => error);
},
focusFirstErrorField() {
let firstErrorField = this.getFirstErrorField();
if (firstErrorField) {
this.refs[firstErrorField].focus();
}
},
getFirstErrorField() {
},
validateAllFields: function () {
}
});

View File

@ -1,12 +1,12 @@
const React = require('react');
const classNames = require('classnames');
const _ = require('lodash');
import React from 'react';
import classNames from 'classnames';
import _ from 'lodash';
const Input = React.createClass({
let Input = React.createClass({
propTypes: {
value: React.PropTypes.string,
validation: React.PropTypes.string,
validation: React.PropTypes.func,
onChange: React.PropTypes.func,
inputType: React.PropTypes.string,
password: React.PropTypes.bool

View File

@ -1,13 +1,15 @@
const englishLanguage = require('data/languages/en');
const spanishLanguage = require('data/languages/es');
import keys from 'data/i18n-keys'
const languages = {
'us': englishLanguage,
'es': spanishLanguage
};
let languages = [
'us',
'es'
];
const i18nData = function (key, lang) {
return languages[lang][key];
let i18nData = function (key, lang) {
let langIndex = languages.indexOf(lang);
return keys[key][langIndex];
};
export default i18nData

View File

@ -0,0 +1,5 @@
export default {
'SUBMIT': ['Submit', 'Enviar'],
'LOG_IN': ['Log in', 'Ingresar'],
'SIGN_UP': ['Sign up', 'Registrarse']
};

View File

@ -1,7 +0,0 @@
export default {
'SUBMIT': 'Submit',
'LOG_IN': 'Log in',
'SIGN_UP': 'Sign up',
'ERROR_EMPTY': 'Invalid value',
'ERROR_EMAIL': 'Invalid email'
};

View File

@ -1,7 +0,0 @@
export default {
'SUBMIT': 'Enviar',
'LOG_IN': 'Ingresar',
'SIGN_UP': 'Registrarse',
'ERROR_EMPTY': 'Valor invalido',
'ERROR_EMAIL': 'Email invalido'
};

View File

@ -1,11 +0,0 @@
const Validator = require('lib-app/validations/validator');
class EmailValidator extends Validator {
validate(value, form) {
if (!value.length) return this.getError('ERROR_EMPTY');
if (value.indexOf('@') === -1) return this.getError('ERROR_EMAIL');
}
}
export default EmailValidator;

View File

@ -1,16 +0,0 @@
const Validator = require('lib-app/validations/validator');
const EmailValidator = require('lib-app/validations/email-validator');
let validators = {
'DEFAULT': new Validator(),
'EMAIL': new EmailValidator()
};
class ValidatorFactory {
static getValidator(validatorKey) {
return validators[validatorKey];
}
}
export default ValidatorFactory;

View File

@ -1,13 +0,0 @@
const i18n = require('lib-app/i18n');
class Validator {
validate(value, form) {
if (!value.length) return this.getError('ERROR_EMPTY');
}
getError(errorKey) {
return i18n(errorKey);
}
}
export default Validator