diff options
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision/form')
6 files changed, 53 insertions, 29 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt index b1e08606..7d429c2e 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt @@ -26,6 +26,7 @@ import kotlinx.serialization.Mapper import kotlinx.serialization.json.JSON import kotlinx.serialization.serializer import pl.treksoft.kvision.form.upload.Upload +import pl.treksoft.kvision.i18n.I18n.trans import pl.treksoft.kvision.types.KDate import pl.treksoft.kvision.types.KFile import pl.treksoft.kvision.utils.getContent @@ -37,6 +38,7 @@ import kotlin.reflect.KProperty1 */ internal data class FieldParams<in F : FormControl>( val required: Boolean = false, + val requiredMessage: String? = null, val validatorMessage: ((F) -> String?)? = null, val validator: ((F) -> Boolean?)? = null ) @@ -51,7 +53,9 @@ private class FormMapWrapper<out V>(private val map: Map<String, V>) : Map<Strin override fun toString(): String = map.toString() override val size: Int get() = map.size override fun isEmpty(): Boolean = map.isEmpty() - override fun containsKey(key: String): Boolean = if (key.indexOf('.') != -1) map.containsKey(key) else !(map.containsKey("$key.time") || map.containsKey("$key.size")) + override fun containsKey(key: String): Boolean = + if (key.indexOf('.') != -1) map.containsKey(key) else !(map.containsKey("$key.time") || map.containsKey("$key.size")) + override fun containsValue(value: @UnsafeVariance V): Boolean = map.containsValue(value) override fun get(key: String): V? = map[key] override val keys: Set<String> get() = map.keys @@ -105,12 +109,12 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali } internal fun <C : FormControl> addInternal( - key: KProperty1<K, *>, control: C, required: Boolean = false, + key: KProperty1<K, *>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): Form<K> { this.fields[key.name] = control - this.fieldsParams[key.name] = FieldParams(required, validatorMessage, validator) + this.fieldsParams[key.name] = FieldParams(required, requiredMessage, validatorMessage, validator) return this } @@ -119,16 +123,17 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali * @param key key identifier of the control * @param control the string form control * @param required determines if the control is required + * @param requiredMessage optional required validation message * @param validatorMessage optional function returning validation message * @param validator optional validation function * @return current form */ fun <C : StringFormControl> add( - key: KProperty1<K, String?>, control: C, required: Boolean = false, + key: KProperty1<K, String?>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): Form<K> { - return addInternal(key, control, required, validatorMessage, validator) + return addInternal(key, control, required, requiredMessage, validatorMessage, validator) } /** @@ -136,16 +141,17 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali * @param key key identifier of the control * @param control the boolean form control * @param required determines if the control is required + * @param requiredMessage optional required validation message * @param validatorMessage optional function returning validation message * @param validator optional validation function * @return current form */ fun <C : BoolFormControl> add( - key: KProperty1<K, Boolean?>, control: C, required: Boolean = false, + key: KProperty1<K, Boolean?>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): Form<K> { - return addInternal(key, control, required, validatorMessage, validator) + return addInternal(key, control, required, requiredMessage, validatorMessage, validator) } /** @@ -153,16 +159,17 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali * @param key key identifier of the control * @param control the number form control * @param required determines if the control is required + * @param requiredMessage optional required validation message * @param validatorMessage optional function returning validation message * @param validator optional validation function * @return current form */ fun <C : NumberFormControl> add( - key: KProperty1<K, Number?>, control: C, required: Boolean = false, + key: KProperty1<K, Number?>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): Form<K> { - return addInternal(key, control, required, validatorMessage, validator) + return addInternal(key, control, required, requiredMessage, validatorMessage, validator) } /** @@ -170,16 +177,17 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali * @param key key identifier of the control * @param control the date form control * @param required determines if the control is required + * @param requiredMessage optional required validation message * @param validatorMessage optional function returning validation message * @param validator optional validation function * @return current form */ fun <C : KDateFormControl> add( - key: KProperty1<K, KDate?>, control: C, required: Boolean = false, + key: KProperty1<K, KDate?>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): Form<K> { - return addInternal(key, control, required, validatorMessage, validator) + return addInternal(key, control, required, requiredMessage, validatorMessage, validator) } /** @@ -187,16 +195,17 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali * @param key key identifier of the control * @param control the files form control * @param required determines if the control is required + * @param requiredMessage optional required validation message * @param validatorMessage optional function returning validation message * @param validator optional validation function * @return current form */ fun <C : KFilesFormControl> add( - key: KProperty1<K, List<KFile>?>, control: C, required: Boolean = false, + key: KProperty1<K, List<KFile>?>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): Form<K> { - return addInternal(key, control, required, validatorMessage, validator) + return addInternal(key, control, required, requiredMessage, validatorMessage, validator) } /** @@ -300,12 +309,12 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali val required = fieldsParams?.required ?: false val requiredError = control.getValue() == null && required if (requiredError) { - control.validatorError = "Value is required" + control.validatorError = trans(fieldsParams?.requiredMessage) ?: "Value is required" true } else { val validatorPassed = fieldsParams?.validator?.invoke(control) ?: true control.validatorError = if (!validatorPassed) { - fieldsParams?.validatorMessage?.invoke(control) ?: "Invalid value" + trans(fieldsParams?.validatorMessage?.invoke(control)) ?: "Invalid value" } else { null } @@ -315,7 +324,7 @@ class Form<K : Any>(private val panel: FormPanel<K>? = null, private val seriali }.find { it } val validatorPassed = validator?.invoke(this) ?: true panel?.validatorError = if (!validatorPassed) { - validatorMessage?.invoke(this) ?: "Invalid form data" + trans(validatorMessage?.invoke(this)) ?: "Invalid form data" } else { null } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt index 88aed36b..a013c076 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt @@ -203,7 +203,7 @@ open class FormPanel<K : Any>( } protected fun <C : FormControl> addInternal( - key: KProperty1<K, *>, control: C, required: Boolean = false, + key: KProperty1<K, *>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): FormPanel<K> { @@ -219,7 +219,7 @@ open class FormPanel<K : Any>( } } super.add(control) - form.addInternal(key, control, required, validatorMessage, validator) + form.addInternal(key, control, required, requiredMessage, validatorMessage, validator) return this } @@ -228,16 +228,17 @@ open class FormPanel<K : Any>( * @param key key identifier of the control * @param control the string form control * @param required determines if the control is required + * @param requiredMessage optional required validation message * @param validatorMessage optional function returning validation message * @param validator optional validation function * @return current form panel */ open fun <C : StringFormControl> add( - key: KProperty1<K, String?>, control: C, required: Boolean = false, + key: KProperty1<K, String?>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): FormPanel<K> { - return addInternal(key, control, required, validatorMessage, validator) + return addInternal(key, control, required, requiredMessage, validatorMessage, validator) } /** @@ -245,16 +246,17 @@ open class FormPanel<K : Any>( * @param key key identifier of the control * @param control the boolean form control * @param required determines if the control is required + * @param requiredMessage optional required validation message * @param validatorMessage optional function returning validation message * @param validator optional validation function * @return current form panel */ open fun <C : BoolFormControl> add( - key: KProperty1<K, Boolean?>, control: C, required: Boolean = false, + key: KProperty1<K, Boolean?>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): FormPanel<K> { - return addInternal(key, control, required, validatorMessage, validator) + return addInternal(key, control, required, requiredMessage, validatorMessage, validator) } /** @@ -262,16 +264,17 @@ open class FormPanel<K : Any>( * @param key key identifier of the control * @param control the number form control * @param required determines if the control is required + * @param requiredMessage optional required validation message * @param validatorMessage optional function returning validation message * @param validator optional validation function * @return current form panel */ open fun <C : NumberFormControl> add( - key: KProperty1<K, Number?>, control: C, required: Boolean = false, + key: KProperty1<K, Number?>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): FormPanel<K> { - return addInternal(key, control, required, validatorMessage, validator) + return addInternal(key, control, required, requiredMessage, validatorMessage, validator) } /** @@ -279,16 +282,17 @@ open class FormPanel<K : Any>( * @param key key identifier of the control * @param control the date form control * @param required determines if the control is required + * @param requiredMessage optional required validation message * @param validatorMessage optional function returning validation message * @param validator optional validation function * @return current form panel */ open fun <C : KDateFormControl> add( - key: KProperty1<K, KDate?>, control: C, required: Boolean = false, + key: KProperty1<K, KDate?>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): FormPanel<K> { - return addInternal(key, control, required, validatorMessage, validator) + return addInternal(key, control, required, requiredMessage, validatorMessage, validator) } /** @@ -296,16 +300,17 @@ open class FormPanel<K : Any>( * @param key key identifier of the control * @param control the files form control * @param required determines if the control is required + * @param requiredMessage optional required validation message * @param validatorMessage optional function returning validation message * @param validator optional validation function * @return current form panel */ open fun <C : KFilesFormControl> add( - key: KProperty1<K, List<KFile>?>, control: C, required: Boolean = false, + key: KProperty1<K, List<KFile>?>, control: C, required: Boolean = false, requiredMessage: String? = null, validatorMessage: ((C) -> String?)? = null, validator: ((C) -> Boolean?)? = null ): FormPanel<K> { - return addInternal(key, control, required, validatorMessage, validator) + return addInternal(key, control, required, requiredMessage, validatorMessage, validator) } /** diff --git a/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt b/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt index 09d2fcdb..d802a111 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/select/AjaxOptions.kt @@ -24,6 +24,7 @@ package pl.treksoft.kvision.form.select import pl.treksoft.jquery.JQueryXHR import pl.treksoft.kvision.KVManager.AJAX_REQUEST_DELAY import pl.treksoft.kvision.KVManager.KVNULL +import pl.treksoft.kvision.i18n.I18n import pl.treksoft.kvision.utils.obj /** @@ -104,6 +105,7 @@ fun AjaxOptions.toJs(emptyOption: Boolean): dynamic { processedData } } + val language = I18n.language return obj { this.ajax = obj { this.url = url @@ -121,5 +123,6 @@ fun AjaxOptions.toJs(emptyOption: Boolean): dynamic { this.preserveSelected = false this.requestDelay = requestDelay this.restoreOnError = restoreOnError + this.langCode = language } } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/text/RichTextInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/text/RichTextInput.kt index 88cb3b86..c5d9a556 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/text/RichTextInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/text/RichTextInput.kt @@ -84,8 +84,9 @@ open class RichTextInput(value: String? = null, classes: Set<String> = setOf()) trixId = this.getElementJQuery()?.attr("trix-id") if (trixId != null) { value?.let { - if (this.getElement().asDynamic().editor != undefined) + if (this.getElement().asDynamic().editor != undefined) { this.getElement().asDynamic().editor.loadHTML(it) + } } } }) diff --git a/src/main/kotlin/pl/treksoft/kvision/form/time/DateTimeInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/time/DateTimeInput.kt index 6ce4d0c3..1df8a082 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/time/DateTimeInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/time/DateTimeInput.kt @@ -28,6 +28,7 @@ import pl.treksoft.kvision.core.StringPair import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.form.FormInput import pl.treksoft.kvision.form.InputSize +import pl.treksoft.kvision.i18n.I18n import pl.treksoft.kvision.types.KDate import pl.treksoft.kvision.types.toJS import pl.treksoft.kvision.types.toKDateF @@ -226,6 +227,7 @@ open class DateTimeInput( val minView = if (format.contains("HH") || format.contains("mm")) 0 else 2 val maxView = if (format.contains("YY") || format.contains("M") || format.contains("D")) MAX_VIEW else 1 val startView = if (maxView < 2) maxView else 2 + val language = I18n.language getElementJQueryD()?.datetimepicker(obj { this.format = datePickerFormat this.startView = startView @@ -239,6 +241,7 @@ open class DateTimeInput( this.showMeridian = showMeridian this.daysOfWeekDisabled = daysOfWeekDisabled this.autoclose = true + this.language = language }) } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/upload/UploadInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/upload/UploadInput.kt index 90dd21c4..0f8aaf4a 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/upload/UploadInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/upload/UploadInput.kt @@ -29,6 +29,7 @@ import pl.treksoft.kvision.core.StringPair import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.form.FormInput import pl.treksoft.kvision.form.InputSize +import pl.treksoft.kvision.i18n.I18n import pl.treksoft.kvision.types.KFile import pl.treksoft.kvision.utils.obj @@ -279,6 +280,7 @@ open class UploadInput(uploadUrl: String? = null, multiple: Boolean = false, cla } private fun getSettingsObj(): dynamic { + val language = I18n.language return obj { this.uploadUrl = uploadUrl this.uploadExtraData = uploadExtraData ?: undefined @@ -295,6 +297,7 @@ open class UploadInput(uploadUrl: String? = null, multiple: Boolean = false, cla this.allowedFileTypes = allowedFileTypes?.toTypedArray() this.allowedFileExtensions = allowedFileExtensions?.toTypedArray() this.dropZoneEnabled = dropZoneEnabled + this.language = language } } |