Add custom error message in all rules for view extension methods
See original GitHub issueIn 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 View
s.
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:
- Created 5 years ago
- Comments:8 (8 by maintainers)
Top 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 >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
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.@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.