Skip to content

Add prop to overide validator with custom one. #58

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
22 changes: 20 additions & 2 deletions __tests__/RenderTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import Form from '../src/Form';
describe('React bootstrap validation compilation test', () => {
var React = require('react');
var TestUtils = require('react-addons-test-utils');
var validator = require('validator');

beforeEach(function() {

});

it('Renders Form component correctly.', () => {
Expand All @@ -19,5 +20,22 @@ describe('React bootstrap validation compilation test', () => {
// Do some work with the validation outcomes
}
} />);
});
});

it('Renders Form with validation library as prop.', () => {
// Assemble
var form = TestUtils.renderIntoDocument(
<Form validator={validator} onValidSubmit={ function(event) {return true;} } />
);
});

it('Form validation has custom validation rule.', () => {
// Assemble
var form = TestUtils.renderIntoDocument(
<Form validator={validator} onValidSubmit={ function(event) {return true;} } />
);
validator.extend('isTrue', val => { return true; });

expect(form.props.validator.isTrue()).toBe(true);
});
});
6 changes: 5 additions & 1 deletion src/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default class Form extends InputContainer {

if (typeof this.props.validateOne === 'function') {
result = this.props.validateOne(iptName, value, context, result);
}
}
// if result is !== true, it is considered an error
// it can be either bool or string error
if (result !== true) {
Expand Down Expand Up @@ -261,6 +261,9 @@ export default class Form extends InputContainer {
});

let validator = (input.props && input.props.type) === 'file' ? FileValidator : Validator;
if (input.props && input.props.validator) {
validator = input.props.validator;
}

return val => {
let result = true;
Expand Down Expand Up @@ -331,6 +334,7 @@ Form.propTypes = {
method : React.PropTypes.oneOf(['get', 'post']),
onValidSubmit : React.PropTypes.func.isRequired,
onInvalidSubmit: React.PropTypes.func,
validator : React.PropTypes.object,
validateOne : React.PropTypes.func,
validateAll : React.PropTypes.func,
validationEvent: React.PropTypes.oneOf([
Expand Down