You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A field create by Form.reatomField method isn't affected by the validation during the Form.onSubmit call
I took the example from the documentation and written a unit test for it:
// The example from the doc (https://www.reatom.dev/package/form/)import{reatomForm}from'@reatom/form'exportconstloginForm=reatomForm({asynconSubmit(ctx){constuser=awaitapi.login(ctx,{name: ctx.get(nameField),password: ctx.get(passwordField),})},},'loginForm',)exportconstnameField=loginForm.reatomField({initState: ''},'nameField')exportconstpasswordField=loginForm.reatomField({initState: '',validate(ctx,{ state }){if(state.length<6)thrownewError('The password should have at least six characters.')},},'passwordField',)// My unit testsimporttestfrom'node:test';importassertfrom'node:assert';import{loginForm,passwordField}from'../src/index.js';import{createCtx}from'@reatom/framework';test('Login form',()=>{constctx=createCtx();loginForm.onSubmit(ctx);assert.strictEqual(ctx.get(passwordField.validationAtom).error,'The password should have at least six characters.',);});
The test fails. I've figured out the the password field's validate isn't called at all during the submit.
It happens because the internal validation function expects that the field is in the fieldsListAtom,
but a field gets into it only on the first change. The implementation:
I expect that field's validation callback is called even if the field was not changed since the initialization, because it should be possible to see validation messages even if the form was submitted without change fields values.
Field.remove function adds another copy of the field to fieldsListAtom
// https://github.com/artalar/reatom/blob/v3/packages/form/src/reatomForm.ts#L146constreatomFormField: Form['reatomField']=(options,fieldName=options.name??__count(`${typeofoptions.initState}Field`),)=>{// ...atomField.remove=action((ctx)=>{fieldsListAtom(ctx,(list)=>[...list,atomField])// <- it should filter out the atomField},`${fieldName}.remove`)// ...}
Part
Package @reatom/form
Version
3.3.2
What browsers are you seeing the problem on?
No response
Relevant log output
The text was updated successfully, but these errors were encountered:
What happened?
A field create by
Form.reatomField
method isn't affected by the validation during theForm.onSubmit
callI took the example from the documentation and written a unit test for it:
The test fails. I've figured out the the password field's
validate
isn't called at all during the submit.It happens because the internal validation function expects that the field is in the
fieldsListAtom
,but a field gets into it only on the first change. The implementation:
I expect that field's validation callback is called even if the field was not changed since the initialization, because it should be possible to see validation messages even if the form was submitted without change fields values.
Field.remove
function adds another copy of the field tofieldsListAtom
The implementation:
Part
Package @reatom/form
Version
3.3.2
What browsers are you seeing the problem on?
No response
Relevant log output
The text was updated successfully, but these errors were encountered: