question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get error messages of rules from String or Resources instead of hardcoded ones

See original GitHub issue

Every 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:closed
  • Created 5 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
wajahatkarim3commented, Oct 10, 2018

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 👍

1reaction
wajahatkarim3commented, Oct 10, 2018

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 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found