ragunathjawahar / android-saripaar

UI form validation library for Android
Apache License 2.0
3.22k stars 460 forks source link

wrong adapter assigned to validator in onFocusChange #150

Closed kizzlord closed 7 years ago

kizzlord commented 8 years ago

Here's what I wrote:

public class SomeFragment extends Fragment implements Validator.ValidationListener {

    Validator validator;

    @NotEmpty(emptyTextResId = R.string.warning_text1_required)
    EditText text1;

    @NotEmpty(emptyTextResId = R.string.warning_text2_required)
    EditText text2;

    @Optional
    @Min(value = 0, messageResId = R.string.warning_int1_format)
    EditText int1;

    @Optional
    @Min(value = 0, messageResId = R.string.warning_int2_format)
    EditText int2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        validator = new Validator(this);
        text1 = (EditText) rootView.findViewById(R.id.product_editor_text1);

        ....

        text1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    validator.validate();
                }
            }
        } );

        // same for the remaining views
        validator.setValidationListener(this);
    }
}

And the log reads:

com.mobsandgeeks.saripaar.exception.ConversionException: Expected an integer, but was 
    at com.mobsandgeeks.saripaar.adapter.TextViewIntegerAdapter.getData(TextViewIntegerAdapter.java:36)

This would be correct if this wouldn't be for the text1 EditText but for one of the ints.

So what am I missing? It seems the validator is using the Adapter that was last registered, which might be the TextViewIntegerAdapter so I tried to explicitly register different ViewDataAdapters for different validators like this:

intValidator = new EditIntValidator(this);

intValidator.registerAdapter(EditText.class, new ViewDataAdapter<EditText, Integer>() {
    @Override
    public Integer getData(EditText view) throws ConversionException {
        return Integer.parseInt(view.getText().toString().trim());
    }
});

textValidator = new EditTextValidator(this);
textValidator.registerAdapter(EditText.class, new ViewDataAdapter<EditText, String>() {
    @Override
    public String getData(EditText view) throws ConversionException {
        return view.getText().toString();
    }
});

Anyways, this still brought the above ConversionException for my text editor and it still uses the TextViewIntegerAdapter instead of the explicitly registered one.

Any Ideas?

ragunathjawahar commented 8 years ago

@kizzlord

  1. How did you create a EditIntValidator?
  2. What was your input in the EditText field?