Client side validation of alias max length

This commit is contained in:
Valere 2021-09-22 16:57:10 +02:00 committed by Benoit Marty
parent e0a6e82661
commit 3da5641e2b
6 changed files with 20 additions and 1 deletions

1
changelog.d/3934.bugfix Normal file
View File

@ -0,0 +1 @@
Validate public space address length when user clicks away from

View File

@ -165,6 +165,8 @@ object MatrixPatterns {
fun candidateAliasFromRoomName(name: String): String {
return Regex("\\s").replace(name.lowercase(), "_").let {
"[^a-z0-9._%#@=+-]".toRegex().replace(it, "")
}.let { alias ->
if (alias.length > 255) alias.substring(0, 255) else alias
}
}

View File

@ -17,6 +17,7 @@
package im.vector.app.features.form
import android.text.Editable
import android.text.InputFilter
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
@ -77,6 +78,9 @@ abstract class FormEditTextItem : VectorEpoxyModel<FormEditTextItem.Holder>() {
@EpoxyAttribute
var suffixText: String? = null
@EpoxyAttribute
var maxLength: Int? = null
private val onTextChangeListener = object : SimpleTextWatcher() {
override fun afterTextChanged(s: Editable) {
onTextChange?.invoke(s.toString())
@ -109,6 +113,15 @@ abstract class FormEditTextItem : VectorEpoxyModel<FormEditTextItem.Holder>() {
holder.textInputEditText.addTextChangedListenerOnce(onTextChangeListener)
holder.textInputEditText.setOnEditorActionListener(editorActionListener)
holder.textInputEditText.onFocusChangeListener = onFocusChangedListener
if (maxLength != null) {
holder.textInputEditText.filters = arrayOf(InputFilter.LengthFilter(maxLength!!))
holder.textInputLayout.isCounterEnabled = true
holder.textInputLayout.counterMaxLength = maxLength!!
} else {
holder.textInputEditText.filters = arrayOf()
holder.textInputLayout.isCounterEnabled = false
}
}
override fun shouldSaveViewState(): Boolean {

View File

@ -141,6 +141,7 @@ class CreateRoomController @Inject constructor(
value(viewState.aliasLocalPart)
suffixText(":" + viewState.homeServerName)
prefixText("#")
maxLength(255)
hint(host.stringProvider.getString(R.string.room_alias_address_hint))
errorMessage(
host.roomAliasErrorFormatter.format(

View File

@ -56,7 +56,7 @@ import timber.log.Timber
class CreateRoomViewModel @AssistedInject constructor(@Assisted private val initialState: CreateRoomViewState,
private val session: Session,
private val rawService: RawService,
private val vectorPreferences: VectorPreferences
vectorPreferences: VectorPreferences
) : VectorViewModel<CreateRoomViewState, CreateRoomAction, CreateRoomViewEvents>(initialState) {
@AssistedFactory

View File

@ -94,6 +94,8 @@ class SpaceDetailEpoxyController @Inject constructor(
hint(host.stringProvider.getString(R.string.create_space_alias_hint))
suffixText(":" + data.homeServerName)
prefixText("#")
// spaces alias are limited to 255
maxLength(255)
onFocusChange { hasFocus ->
host.aliasTextIsFocused = hasFocus
}