2

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?

Veaceslav Gaidarji
  • 4,261
  • 3
  • 38
  • 61

1 Answers1

4

In my humble opinion you are complicating yourself work and waste time on this issue. If you have exact number of EditTexts corresponding to JSON fields then there is no point to figure out some sophisticated implementation. At the end it could make your code event more difficult to read.

You have to keep references to all your EditText views anyway, right? Alternative with HashMap does not give you any benefit. The only thing you could do is creating method for checking EditText and error value.

private void setEditTextAndValidate(EditText editText, String errorMsg) {
    if(!TextUtils.isEmpty(errorMsg)){
            editText.setError(errorMsg);
            editText.requestFocus();
    }
}

and then call

setEditTextAndValidate(mEditTextUserName, validationResponse.getUserNameError());
setEditTextAndValidate(mEditTextPassword, validationResponse.getPasswordError());

Once again, it's just my opinion that I would like to share. Maybe there are frameworks that will do it for you, but is it worth to you, I doubt.

Damian Petla
  • 8,963
  • 5
  • 44
  • 47
  • it's a good one. I'm looking for more clear solution, where I can process validation response and pass errors to EditText's in a for-each loop for ex. – Veaceslav Gaidarji Aug 25 '14 at 14:52
  • I would understand this approach if you iterate through list of responses and take just username and apply to different EditTexts. But you have to explicitly call response methods and assign to specific views. It has to be coded somewhere. Looking for some generic code with binding configuration will create lots of complicated code. Maybe you will separate it somewhere(moving to package) but it's not worth it. – Damian Petla Aug 25 '14 at 14:58
  • Thanks this worked for me. @Loop please do you think you could give me your opinion on this question http://stackoverflow.com/questions/25598696/recommended-way-order-to-read-data-from-a-webservice-parse-that-data-and-inse – Axel Sep 02 '14 at 03:33