react-validation icon indicating copy to clipboard operation
react-validation copied to clipboard

Form and target component missing from validation rule

Open cfpinto opened this issue 8 years ago • 0 comments

Why is the target input not available in the validation rule method? Having all the forms components in there might be of value but I feel like having the target component and the actual form is a lot more valuable.

See following example

Validation rules

export default function () {
Object.assign(Validation.rules, {
    pattern: {
        rule: (value, component, form) => {
            return (new RegExp(component.props.pattern)).test(value);
        },
        hint: (value, component) => {
            return <div className="error">{vaue} doesn't match {component.props.placeholder}</div>
        }
    },
    api: {
        rule: (value, component, form) => {
            if (value) {
                //ajax request
                ajaxlib.post('/some/end/point', {value: value})
                    .then(response => {
                        form.showError(component.props.name, 'api')
                    });
            }
            return true;
        },
        hint: (value, component, form) => {
            return <div className="error">Failed to do remote validation</div>
        }
    }
}
)

View

<Validation.components.Form>
    <Validation.components.Input name="remote"  />
    <Validation.components.Input name="pattern" pattern="/[0-9]{2}\/[0-9]{2}/" placeholder="MM/YY"   />
</Validation.components.Form>

cfpinto avatar Oct 28 '16 09:10 cfpinto