Skip to content

Commit

Permalink
Merge pull request #52 from jkuatdsc/develop
Browse files Browse the repository at this point in the history
README update
  • Loading branch information
LinusMuema authored Dec 1, 2022
2 parents 8859b13 + 3f4c837 commit d9ead88
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ TextFieldState(
)
```

### Demos

**Demo 1** : In this demo, the validations are executed before the next screen is presented.

https://user-images.githubusercontent.com/47350130/204373150-86c89b3a-8ab2-4c52-8429-b778d0307845.mp4

**Demo 2** : In this demo, the validations are run at the end of the survey when submitting data.

https://user-images.githubusercontent.com/47350130/204372977-e8b66f71-0b61-4d93-a2c9-d8de04876785.mp4

You can find the sample apk [here](https://drive.google.com/file/d/1tMtDtJwuDZoQnxluiAPNC0dYs7aqXUjt/view?usp=sharing)

For more details about the library, check out the [references](https://jkuatdsc.github.io/form-builder/).

### <a id="links" href="#links">Further Reading</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.dsc.formbuilder.screens.survey

import android.content.Intent
import android.os.Bundle
import android.widget.Space
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
Expand All @@ -11,7 +10,6 @@ import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.dsc.form_builder.FormState
import com.dsc.formbuilder.screens.ExitActivity
import com.dsc.formbuilder.screens.survey.components.*
import com.dsc.formbuilder.theme.FormBuilderTheme
Expand Down Expand Up @@ -55,9 +53,10 @@ class SurveyActivity : ComponentActivity() {


// Navigate to exit screen if everything is ok
val finish by remember { viewmodel.finish }
if (finish){
val processDone by remember { viewmodel.finish }
if (processDone){
startActivity(Intent(this, ExitActivity::class.java))
finish()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class SurveyViewmodel : ViewModel() {
TextFieldState(
name = "username",
validators = listOf(
Validators.Required(),
Validators.Min(
limit = 4,
message = "Username should have more than 4 characters"
)
),
Validators.Required()
)
),
TextFieldState(
Expand All @@ -38,54 +38,54 @@ class SurveyViewmodel : ViewModel() {
name = "number",
validators = listOf(
Validators.Phone(),
Validators.Required()
Validators.Required(),
)
),
SelectState(
name = "platform",
validators = listOf(
Validators.Required(
message = "pick at least one platform"
message = "Select at least one platform"
)
)
),
SelectState(
name = "language",
validators = listOf(
Validators.Required(
message = "pick at least one language"
message = "Select at least one language"
)
)
),
SelectState(
name = "ide",
validators = listOf(
Validators.Required(
message = "pick at least one IDE"
message = "Select at least one IDE"
)
)
),
ChoiceState(
name = "gender",
validators = listOf(
Validators.Required(
message = "please select your gender"
message = "Select your gender"
)
)
),
ChoiceState(
name = "experience",
validators = listOf(
Validators.Required(
message = "please select your experience"
message = "Select your experience"
)
)
),
ChoiceState(
name = "os",
validators = listOf(
Validators.Required(
message = "please select one system"
message = "Select one system"
)
)
)
Expand All @@ -98,17 +98,17 @@ class SurveyViewmodel : ViewModel() {

fun validateSurvey() {
val pages: List<List<Int>> = (0..5).chunked(3)
if (!formState.validate()) {
if (!formState.validate()){
val position = formState.fields.indexOfFirst { it.hasError }
_screen.value = pages.indexOfFirst { it.contains(position) }
} else _finish.value = true
}

fun validateScreen(screen: Int) {
val fields: List<BaseState<*>> = formState.fields.chunked(3)[screen]
if (fields.map { it.validate() }.all { it }) { // map is used so we can execute validate() on all fields in that screen
if (fields.map { it.validate() }.all { it }){ // map is used so we can execute validate() on all fields in that screen
if (screen == 2) _finish.value = true
_screen.value += 1
}
}
}
}
10 changes: 5 additions & 5 deletions form-builder/src/main/java/com/dsc/form_builder/Validators.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.dsc.form_builder

private const val EMAIL_MESSAGE = "invalid email address"
private const val REQUIRED_MESSAGE = "this field is required"
private const val PHONE_MESSAGE = "invalid phone number"
private const val WEB_URL_MESSAGE = "invalid web url"
private const val CARD_NUMBER_MESSAGE = "invalid card number"
private const val EMAIL_MESSAGE = "Invalid email address"
private const val REQUIRED_MESSAGE = "This field is required"
private const val PHONE_MESSAGE = "Invalid phone number"
private const val WEB_URL_MESSAGE = "Invalid web url"
private const val CARD_NUMBER_MESSAGE = "Invalid card number"

/**
*
Expand Down

0 comments on commit d9ead88

Please sign in to comment.