0

I have created a business upload form using android studio linked to firebase database, I am facing an issue where i could only add a constraint on the (name=empty display "please fill all fields"), but how can i add this to every other EditText field, so when the user does not fill them all the message shows. here is the code

private void addBusiness(){

  String  name = businame.getText().toString().trim();
  String  email = busiemail.getText().toString().trim();
  String  number = businum.getText().toString().trim();
  String  address = busiadd.getText().toString().trim();
  String  info = busiinfo.getText().toString().trim();
  String  type = spinnerType.getSelectedItem().toString();

    if (!TextUtils.isEmpty(name)) {
      String id= databaseBusiness.push().getKey();
      Business business = new Business(id, name, email, number, address, info, type);
      databaseBusiness.child(id).setValue(business);
      Toast.makeText(this,"Upload Done", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this,"Please Fill All Fields",Toast.LENGTH_LONG).show();
    }
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Amr Mixy
  • 83
  • 1
  • 8

2 Answers2

1

A simple solution could look like this:

Map<String, String> userInputByTag = new HashMap<>();
userInputByTag.put("name", businame.getText().toString().trim());
userInputByTag.put("email", busiemail.getText().toString().trim());
...

And now you can iterate the map entries. If you find a map value that is empty, you can put up a message (using the corresponding map key). Not beautiful, but definitely better compared to

 if (!TextUtils.isEmpty(name)) {
  ...
 else if(!TextUtils.isEmpty(email)) {
  ...

In other words: the straight forward but ugly solution would be a cascade of such if/else if/... statements.

Beyond that - I am pretty sure that Android has some framework support for field validation. You should do some research there, instead of re-inventing the wheel. Start reading here or there or that.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

Convenient for the user:

I would use the more convenient way for the user and display beforehand an error directly in the EditText, if it is empty, with using a TextWatcher on every EditText.

TextWatcher

If you detect that the EditText is empty i.e. in onTextChanged() you can call editText.setError(yourString);.

I recommend to use an AppCompatEditText with a surrounded TextInputLayout.

But nevertheless, before submitting, you have to do this for every member again.

Either you have a more user friendly approach with dedicated errors, like:

if (TextUtils.isEmpty(name)) {

//dedicated error message
return;
}

if (TextUtils.isEmpty(email )) {
//dedicated error message 
return;
}

//and so on

(...)
//if every field holds the correct information
databaseBusiness.child(id).setValue(business);
(...)

or you could just check for every member in one if statement and display a generic error message like you already have.

JacksOnF1re
  • 3,336
  • 24
  • 55