1

I'm newbie in Android develop. I use Robobinding (MVVM framework) to develop Android application and I didn't found any solution to create validation in presentation model (not in activity). Has anyone encountered a similar problem? Which approach is chosen? I need somthing like this:

public class LoginPM extends AbstractPresentationModel {
        private String login;
        public String getLogin() { return login; }
        public void setLogin(String value)
        {
            if (!StringComparator.IsEquals(this.login, value))
            {
                if(TextUtils.isEmpty(value))
                {
                 setError("login", "Field cannot be left blank.");
                 return;
                }
                this.login = value;
                firePropertyChange("login");
            }
        }
  }

Sample

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91
Peter Barbanyaga
  • 496
  • 7
  • 16

2 Answers2

1

Sorry for the late reply. I did not notice the question. Can you have a LoginView interface between your LoginActivity and LoginPM? In that way, you can do something like below:

public void login() {
  if(isInvalid(loginInfo)) {
    loginView.setLoginError("error info");
  } else {
    loginService.login(loginInfo);
  }
}

Alternatively, you can implement OnTextChange event for TextView, which is fairly easy to do. You can refer to text attribute binding implementation of TextView. And you can register a method to listen to the event. Once the event is fired, you can get hold of TextView from the event object.

Also, you can implement error binding attribute for TextView and update error info accordingly.

If you like, you can post into the RoboBinding google group to get a quick response.

Hope this helps, Cheng

Cheng
  • 572
  • 8
  • 12
  • *Also, you can implement error binding attribute for TextView and update error info accordingly.* Is that really possible? I think that TextView and EditText have no accessible error attribute. Only the method setError("MSG") is possible. – Hollerweger Nov 06 '14 at 10:23
  • @Holla, It is surely possible. You simply implement a binding attribute for TextView.error like any other binding attributes. Have a look the docs and source code, if you are interested. It is quite easy to do. – Cheng Nov 07 '14 at 11:29
  • Okay i will try that, i thought it's not accessible because it's not in the list of XML Attributes: http://developer.android.com/reference/android/widget/TextView.html – Hollerweger Nov 07 '14 at 15:15
  • No, it doesn't need to be in the list of Android XML attributes. – Cheng Nov 08 '14 at 21:51
  • Does not work for me: `org.robobinding.binder.ViewHierarchyInflationErrorsException: -------------------------EditText(1 errors)----------------------- error: Unrecognized attribute 'error' -------------------------The first error stack trace----------------------- error: Unrecognized attribute 'error' at org.robobinding.PendingAttributesForViewImpl.getResolutionErrors(PendingAttributesForViewImpl.java:43) at org.robobinding.binder.BindingAttributeResolver.resolve(BindingAttributeResolver.java:38)` – Hollerweger Nov 12 '14 at 08:55
  • Hi Holla, as mentioned in the above comment, you will need to implement error binding attribute, as it is not implemented, which is quite easy - this is [EnabledAttribute for View](https://github.com/RoboBinding/RoboBinding/blob/develop/framework/src/main/java/org/robobinding/widget/view/EnabledAttribute.java). In the next version, you will not require to implement these simple binding attributes. With introduction of code generation, the framework will be able to generate them for you. I am on the task at the moment. – Cheng Nov 13 '14 at 11:08
0

I have resolved my task using reflection to getting user controls from binding-objects maps (Robobindings). https://github.com/Barbanyaga/RobobindingValidation/blob/master/BasePresentationModel.java

Use like this:

public class LoginPM extends BasePresentationModel {
        private String login;
        public String getLogin() { return login; }
        public void setLogin(String value)
        {
            if (!StringComparator.IsEquals(this.login, value))
            {
                if(TextUtils.isEmpty(value))
                {
                 setError("login", "Field cannot be left blank.");
                 return;
                }
                this.login = value;
                firePropertyChange("login");
            }
        }
  }
Peter Barbanyaga
  • 496
  • 7
  • 16