Android MVP doubts about validating
Asked Answered
K

5

6

I´m starting to implement the MVP pattern on an Android project and I have some doubts about where I should validate the fields before doing any action.

For example, If I have to send a form with three fields (name, email, text). Should I validate the fields in the activity or I should send them to the Presenter for being validated?

I'm not 100% sure yet if the comunication with the presenter has to be only with the right data already validated or not.

Kaunas answered 22/4, 2016 at 7:35 Comment(0)
G
12

It really depends, my recommendation is that (And what I normally do):

  • If the field can be validated without access to database or complex operations, I'd do it in the activity. Examples of such fields would be: Password (Passwords need to contain at least 7 characters), Age (Age must be numeric)
  • If the field needs to be validated by accessing the database (or by web service) or the operation requires complex logic and resource, do it in the presenter. Examples of such fields would be: Username (To check if it is a duplicated username by accessing the database)

Think of it as a front-end and back-end of a website, although not completely same, it does help you to clarify confusing concepts.

Graft answered 22/4, 2016 at 7:45 Comment(4)
Thanks for the recommendation! I'll try to use the approach of "front-end & back-end".Kaunas
Although at first it feels tempting to do some minor validation logic in the view layer I believe that even such logic belongs in the presentation layer. It's easier testable in the presenter layer there's and there's a clear distinction and reason why you have 2 layers. This way the view layer stays pretty if not utterly stupid.Mushroom
I agree with @JonasGeiregat. The validation belongs to the Presenter. In addition, I would inject the validation conditions (password length, min age, etc..) to the presenter, so it will be event more flexible.Husking
When speaking of mobile apps, we usually don't use backend for data validation, and things like password length, username, etc, are part of business layer, View is only for passing events to Presenter, and to display info to user.Spline
O
4

View should never decide to do things by itself, presenter keeps waiting by events notified by view and presenter decides what to do then, view only keeps waiting orders from presenter.

So, no, validation is a presenter task, even if it is a very simple task such as validating a field.

Offcolor answered 22/3, 2018 at 0:36 Comment(0)
U
1

You can do like this in activity:

private Presenter mPrensenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            mPrensenter.load(name,email,text);
        }
    });
}

@Override
public void onRightDataValidated(){

}

then there is two interface MainView and Prensenter:

public interface MainView{
    void onRightDataValidated();
}

public interface Presenter{
    void load(String name,String email,String text);
}

int the impl of the Presenter,when the data need to be invalidate in load method,u can use MainView.onRightDataValidated to callback , u can find more in my github MVP Demo

Unused answered 22/4, 2016 at 7:55 Comment(0)
O
1

Part of the point of MVP is to make testing easier. If you approach questions like these asking, "What if I never tested the view," then that gives the right perspective on what logic should or should not go there. The presenter should lend itself to fast JUnit testing and relieve the developer from needing to write Android instrumentation tests.

Bottom line, you're going to want to test your validation logic to be sure it is sound and if you put that in the Presenter, it makes life easier.

Oscillator answered 20/2, 2019 at 14:46 Comment(0)
B
0

Well I believe you should do the validation in activity. And Simply presenter will call the validation method to check if the validation passes then it will complete the action otherwise show the error.! In one of my client's project, There is detail page and on click of submit button it should check if detailed page filled then it will save the order with the detail otherwise show the error. And this is how i have implemented-- enter image description here

Here you can see the isDetailFilledOut() is a validation method and it will return true if validation passes otherwise false. If it returns true it checks if internet also available then it saves the order by calling model's saveOrder method otherwise shows the fill out detail warning.

Brooklynese answered 20/7, 2016 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.