NaingAungLuu / form-conductor

A declarative form validation for Jetpack Compose
https://formconductor.naingaungluu.me
MIT License
64 stars 5 forks source link

Can we use it in custom input? #53

Open rifkyhaekal opened 1 year ago

rifkyhaekal commented 1 year ago

I have my custom text field input, like below: `form(RegisterRequestDto::class) { Column( modifier = Modifier .fillMaxWidth() .constrainAs(contentColumn) { top.linkTo(parent.top) bottom.linkTo(button.top, margin = (200).dp) start.linkTo(parent.start) end.linkTo(parent.end) } ) { field(fieldClass = RegisterRequestDto::name) { InputTextField( labelValue = stringResource(id = R.string.text_name), onTextChanged = { registerViewModel.onTriggerEvent(RegisterEvent.NameChanged(it)) } ) } LargeSpacer() field(fieldClass = RegisterRequestDto::email) { InputTextField( labelValue = stringResource(id = R.string.text_email), onTextChanged = { registerViewModel.onTriggerEvent(RegisterEvent.EmailChanged(it)) } ) } LargeSpacer() field(fieldClass = RegisterRequestDto::password) { PasswordTextField( labelValue = stringResource(id = R.string.text_password), onTextChanged = { registerViewModel.onTriggerEvent(RegisterEvent.PasswordChanged(it)) } ) } LargeSpacer() field(fieldClass = RegisterRequestDto::confirmPassword) { PasswordTextField( labelValue = stringResource(id = R.string.text_confirm_password), onTextChanged = { registerViewModel.onTriggerEvent( RegisterEvent.ConfirmPasswordChanged( it ) ) } ) } }

            ButtonView(
                value = stringResource(id = R.string.text_register),
                onButtonClicked = {
                    registerViewModel.onTriggerEvent(RegisterEvent.DoRegister)
                    navigator.openLogin()
                },
                isEnabled = this.formState.value is FormResult.Success,
                modifier = Modifier
                    .constrainAs(button) {
                        bottom.linkTo(parent.bottom)
                        start.linkTo(parent.start)
                        end.linkTo(parent.end)
                    }
            )
        }
    }`

Is i'm doing it correctly? Because the validation is not working

NaingAungLuu commented 3 weeks ago

Hello @rifkyhaekal! Apologies for a late reply!

I just checked the code you used and seems like it's missing the part where we actually update the field's value when input changes.

By design, the implementation should look something like this:

// ...
field(fieldClass = RegisterRequestDto::name) {
    InputTextField(
        labelValue = stringResource(id = R.string.text_name),
        onTextChanged = { 
            setField(it)
            registerViewModel.onTriggerEvent(RegisterEvent.NameChanged(it)) 
        }
    )
}
// ...

This will allow the form/field's state to be updated and validated.

You can check the details in the documentation here!

Hope that helps xD