Closed benmonro closed 9 years ago
I'll 2nd @benmonro 's question, and also like to ask why not getValidationOptions() returns the form's option ?
return {
path: this.props.ctx.path,
context: this.props.context || this.props.ctx.context,
option: this.props.ctx.options
};
What is the intent of this context object?
From the README
validate(value, type, [options]) -> ValidationResult
value
the value to validatetype
a type defined with the tcomb libraryoptions
(optional) is an object with the following keys
path
path prefix for validationcontext
passed to getValidationErrorMessage
(useful for i18n)Relevant: https://github.com/gcanti/tcomb-validation/issues/27
@gcanti I think @benmonro want tcomb-form
to automatically pass form's options as context to getValidationErrorMessage
.
@benmonro But how would you pass extra info there then? For example, current user's locale to translate error messages?
wants tcomb-form to automatically pass form's options as context to getValidationErrorMessage
Yep, I'm thinking about a solution without breaking changes...
without breaking changes
One option is merging context
with options
:
var Type = t.struct({
name: t.String
});
t.String.getValidationErrorMessage = function (value, path, context) {
console.log(context); // => {locale: 'it-IT', options: {label: 'My label'}}
};
var options = {
fields: {
name: {
label: 'My label'
}
}
};
var App = React.createClass({
onSubmit(evt) {
evt.preventDefault();
var value = this.refs.form.getValue();
if (value) {
console.log(value);
}
},
render() {
return (
<form onSubmit={this.onSubmit}>
<t.form.Form
ref="form"
type={Type}
options={options}
context={{locale: 'it-IT'}}
/>
<div className="form-group">
<button type="submit" className="btn btn-primary">Save</button>
</div>
</form>
);
}
});
@gcanti , i'd like the idea of merging context
with options
. Which is also what I ask about getValidationOptions()
@gcanti Maybe pass the whole form component instance then? In this case it will be possible to call some of it's helper methods, for example form.getOptions()
etc.
@th0r I agree, probably it's the best bet
Closing in favour of https://github.com/gcanti/tcomb-form/issues/222
I'm using tcomb-form which of course uses tcomb-validation. I have a custom type which I've defined like this:
The question I'm wondering about is with regards to the context passed into the validate() method. What is the intent of this context object? I noticed in the tcomb-form that it's passing in an object with {path:this.props.ctx.path, context:this.props.context} which seems a bit strange to me because now my form basically looks like this:
Our requirement is to display the label associated w/ the field in the error message, not the path (they often differ in our schema). The reason that feels strange to me is that I'm already giving the form a reference to this.state.options... am I misusing this property?