I have next validation response from server (generated by symfony2
)
{
"code":400,
"message":"Validation Failed",
"errors":{
"children":{
"username":{
"errors": [ "This value should not be blank." ]
}
}
}
}
I can parse this JSON
response, get the object of class ValidationResponse
:
@JsonIgnoreProperties(ignoreUnknown = true)
public class ValidationResponse{
...
@JsonProperty("username")
private String userNameError;
}
Now I have in layout EditText mEditTextUserName
, and userNameError = "This value should not be blank."
I don't wanna check all fields in ValidationResponse
for error messages with tons of if else
constructions
if(!TextUtils.isEmpty(validationResponse.getUserNameError())){
mEditTextUserName.setError(validationResponse.getUserNameError());
mEditTextUserName.requestFocus();
}
This will bring a lot of unnedeed code in my project, for "simple" form fields validation.
As alternative, I can create
HashMap<String, EditText> editFields;
...
editFields.put("username", mEditTextUserName);
...
// send request, parse validation response
((EdiText)editFields.get("username")).setError(validationError.getUserNameError());
But I don't like this solution.
Is there a way to bind validation error message from JSON
from server to EditText
error using annotations
or other language construction?