Get error messages of rules from String or Resources instead of hardcoded ones
See original GitHub issueEvery Rule
class such as EmailRule
has a method called as getErrorMessage()
which returns the error message when validation is failed.
For now, all the rules have this message hard coded strings. These error messages should be dynamic and can be fetched from String
or Resource
.
For example, if we want to change EmailRule
, then we can do something like this:
class EmailRule : BaseRule(error: String = "Invalid Email Address!") // Adding custom error message var
{
override fun validate(text: String): Boolean {
return Validator(text).regex(
"^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]|[\\w-]{2,}))@"
+ "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
+ "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9]))|"
+ "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$"
).check()
}
// Add a setter for error message
fun setError(msg: String) { error = msg }
override fun getErrorMessage(): String {
return error // Return the error message here
}
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
How to solve this issue of Hardcoded string? - Stack Overflow
As a general rule, you should never use hardcoded strings in your layout but always use string resources (which means the all strings...
Read more >New rule proposal: Do not use hardcoded strings · Issue #2152
We need to localize an application where some strings are using resource files and some aren't. ... On one hand we need to...
Read more >Solve/ Fix Hardcoded text should use @string resource
Solve/ Fix Hardcoded text should use @ string resource | Use String Resource | Android Studio.
Read more >CA1054: URI parameters should not be strings - Microsoft Learn
This rule splits the parameter name into tokens based on the camel casing convention and checks whether each token equals "uri", "Uri", "urn",...
Read more >SetTextI18n: TextView Internationalization - Google Samples
Hardcoded text can not be properly translated to other languages. Consider using Android resource strings instead. Do not build messages by concatenating ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello @emrekose26 , Yeah! It looks like @antercepter isn’t active much on Github. Yes, you can work on this and send me the pull request 😄 I would love to merge it 👍
Well I think this is a complicated situation. For now, if user wants to support multi language, then I think he can do so by fetching resource string himself and passing in the rule as string. For now, let’s skip this resources issue. I will create another issue for this and see what kind of proposals we get. Thank you for your suggestions 😃