Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wajahatkarim3 opened this Issue Oct 10, 2018 · 9 comments

Comments

Projects
None yet
3 participants
@wajahatkarim3
Copy link
Owner

wajahatkarim3 commented Oct 10, 2018

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
   }
}
@antercepter

This comment has been minimized.

Copy link

antercepter commented Oct 10, 2018

Hi @wajahatkarim3

I want to contribute and fix this issue. Thanks

@wajahatkarim3

This comment has been minimized.

Copy link
Owner Author

wajahatkarim3 commented Oct 10, 2018

Hello @antercepter
Thanks for your interest. Yes, you can do it. 👍

@emrekose26

This comment has been minimized.

Copy link
Contributor

emrekose26 commented Oct 10, 2018

Hi @wajahatkarim3
I would like to help to fix this issue if you want.

@wajahatkarim3

This comment has been minimized.

Copy link
Owner Author

wajahatkarim3 commented 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 👍

@antercepter

This comment has been minimized.

Copy link

antercepter commented Oct 10, 2018

I'm going to extract hardcode strings to Resources and read them via Resources/Context reference. Do you still need my help or @emrekose26 will do this?

@wajahatkarim3

This comment has been minimized.

Copy link
Owner Author

wajahatkarim3 commented Oct 10, 2018

Well I am little confused about it now. @emrekose26 has closed pull request.

@antercepter Can you please tell me how do you propose to fix this Context reference issue? I would love to have your say on this and merge it into the master code. 😃

@antercepter

This comment has been minimized.

Copy link

antercepter commented Oct 10, 2018

I'm not sure, but you can add Resource nullable reference to Validator constructor

class Validator(val text: String, private val res: Resources? = null)

It can give you possibility to init Validator with real reference in View classes, f.e.

fun EditText.validator() : Validator
{
    return Validator(text.toString(), this.resources)
}

After than you can refactor BaseRule#getErrorMessage and add Resources argument:

fun getErrorMessage(res: Resources?) : String

and read needed string values

override fun getErrorMessage(): String = res?.getString(R.string.needed_string) ?: ""

Only one thing I have doubt that there will be an empty error message, if Rule#check was called outside of any View. Need to think it over and maybe find other solution, since it's not flexible to hold string as static values. One day you'll need multi-language feature and it's hard to implement without keeping strings as resources.

@wajahatkarim3

This comment has been minimized.

Copy link
Owner Author

wajahatkarim3 commented 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 😃

@wajahatkarim3

This comment has been minimized.

Copy link
Owner Author

wajahatkarim3 commented Oct 11, 2018

Fixed in pull request #23 by @emrekose26 😄 . Closing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.