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.

Add custom error message in all rules for view extension methods

See original GitHub issue

In the view_ktx package, there are few classes for view extensions such as EditTextKtx.kt, TextViewKtx.kt etc. These classes contains kotlin extension methods of validation rules for respective Views.

For example, EditTextKtx.kt for NonEmptyRule check, we have two methods (one normal, and other with callback) like:

fun EditText.nonEmpty() : Boolean
{
    return validator().nonEmpty().check()
}

fun EditText.nonEmpty(callback: (message: String) -> Unit) : Boolean
{
    return validator().nonEmpty()
            .addErrorCallback {
                callback.invoke(it)
            }
            .check()
}

Task The goal of this task is to add a Nullable parameter with name errorMsg in each of these methods and pass it in the validation check in the method definition like this:

fun EditText.nonEmpty(errorMsg:String? = null) : Boolean
{
    return validator().nonEmpty(errorMsg).check()
}

fun EditText.nonEmpty(callback: (message: String) -> Unit, errorMsg:String? = null) : Boolean
{
    return validator().nonEmpty(errorMsg)
            .addErrorCallback {
                callback.invoke(it)
            }
            .check()
}

Please note that how I have passed the errorMsg paramter in the checks of validator() inside the method definitions.

This should be done for all methods of 5 classes inside view_ktx package:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
emaiaxcommented, Oct 30, 2018

Also, I would like to have all validations checked at once instead of checking if all fields are validated one by one, since the validate method instantly breaks the loop if the first check returns false.

What a failFast: Boolean = true option? It makes sense @wajahatkarim3? I’d be happy to contribute for that.

0reactions
wajahatkarim3commented, Nov 1, 2018

@emaiax I have fixed the compile errors and also finished this issue. So, I am closing this issue and also issue #29 as that was also part of this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to make a custom error message with extension method ...
Extension method for Name property which returns IRuleBuilder type: ... Then we can use WithMessage method to override error description:
Read more >
Custom errors, extending Error - The Modern JavaScript Tutorial
JavaScript allows to use throw with any argument, so technically our custom error classes don't need to inherit from Error .
Read more >
How to implement and call a custom extension method - C# ...
This topic shows how to implement your own extension methods for any .NET type. Client code can use your extension methods by adding...
Read more >
Error handling - Apollo GraphQL Docs
When Apollo Server formats an error in a response, it sets the code extension to this value if no other code is set....
Read more >
Validation - Laravel - The PHP Framework For Web Artisans
Basic Usage; Working With Error Messages; Error Messages & Views; Available Validation Rules; Conditionally Adding Rules; Custom Error Messages ...
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