I want to update my callback function:
const getSchema = React.useCallback(
() => {
const schema = twSchema(
labels,
isInitialWarehouseActive ? initialWarehouse.id : undefined,
operationTypeDisabled ? initialWarehouse.operation_type : undefined
);
schema.addValidator((model, _schema) => {
if (model.dateRangeMode && (!model.timeRangeMode || model.hasRampHours) && !model.dateInpu.to) {
_schema.setModelError('dateInput.to', labels.fieldIsRequired);
...
...
});
return schema;
},
[initialStore]
);
where twSchema:
const twSchema = (labels, initialStoreId, storeOperationType) => new Schema({...
And use case of my getSchema
:
<Form
key="time-window-form"
ctx="time-window-form"
eventsEmitter={eventsEmitter}
model={model}
onError={onError}
onSubmit={(data) => {
...
}).then(() => {
...
})
.catch(hint => displayMessageAndHighlightValidatedComponent(hint));
}}
schema={getSchema()}
>
I use this value (getSchema
) to my form (I have to set schema to my form).
Depending on the error that can occur I'd like to add some validator to my schema BUT I CAN'T:
const displayMessageAndHighlightValidatedComponent = (hint) => {
getSchema().addValidator((model, schema) => {
//this code is not executed!!!
console.log(schema);
console.log('SCHEMA');
schema.setModelError('dateInputField', labels.createNextTimeWindow);
});
return onFailed();
};
The question is why? Why I can't update my object/function? I have to remove useCallback
to be able to add validator dynamically...