android-saripaar icon indicating copy to clipboard operation
android-saripaar copied to clipboard

How to use Saripaar with DatabindingUtils in android

Open ManishPatiyal opened this issue 8 years ago • 5 comments

As with DatabindlingUtils we avoid declaring view elements in our java code but for saripaar to work we need to add annotations, so do we need to declare view elements and annotate them or is there something we can do in xml only to make saripaar work.

ManishPatiyal avatar Jul 27 '16 12:07 ManishPatiyal

Hi has anyone found the solution as how can we use saaripar with data binding ?

xrnd avatar Jan 28 '17 09:01 xrnd

In my case I created my own Rules, even rules exists in API, for example to @NotEmpty I created a NotEmptyRule with the same logic: code of @NotEmpty annotation saripaar

public class NotEmptyRule extends ContextualAnnotationRule<NotEmpty, String> {

    protected NotEmptyRule(final ValidationContext validationContext, final NotEmpty notEmpty) {
        super(validationContext, notEmpty);
    }

    @Override
    public boolean isValid(final String data) {
        boolean isEmpty = false;
        if (data != null) {
            String text = mRuleAnnotation.trim() ? data.trim() : data;

            Context context = mValidationContext.getContext();
            String emptyText = mRuleAnnotation.emptyTextResId() != -1
                    ? context.getString(mRuleAnnotation.emptyTextResId())
                    : mRuleAnnotation.emptyText();

            isEmpty = emptyText.equals(text) || "".equals(text);
        }

        return !isEmpty;
    }
}

and this is my new rule:

public class NotEmptyRule extends QuickRule<EditText> {
    private boolean useTrim = false;
    private boolean allowAttention = true;

    public NotEmptyRule() {
        //Contructor por defecto
    }

    public NotEmptyRule(boolean trim) {
        useTrim = trim;
    }

    public NotEmptyRule(boolean trim, boolean allowAttention) {
        useTrim = trim;
        this.allowAttention = allowAttention;
    }

    @Override
    public boolean isValid(EditText editText) {
        String data = editText.getText().toString();
        boolean isEmpty = false;
        if (data != null) {
            String text = useTrim ? data.trim() : data;
            isEmpty = "".equals(text);
        }

        return !isEmpty || !allowAttention;
    }

    @Override
    public String getMessage(Context context) {
        return context.getString(R.string.message_not_empty);
    }
}

then, I addes my rule to validator:

validator.put(binding.editNumeroServicioMedico, new NotEmptyRule(true));

I hope this can help you.

byronsliger avatar Mar 14 '17 15:03 byronsliger

I like @byronsliger approach. I am thinking about using reflection to access the binding object and retrieve all the Field that are instanceof View and then add the validators iterating the array.

mdumrauf avatar Aug 01 '17 02:08 mdumrauf

@ragunathjawahar how about "annotating" the xml with custom pre-defined attributes? Pretty much like what Calligraphy or Android-Iconics do.

I imagine something like:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="MissingPrefix">

    <data>

        <variable
            name="foo"
            type="com.bar.Foo"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{foo.somethingNotEditable}"/>

        <EditText
            notEmpty="true"
            password="{ min: 6, scheme: Password.Scheme.ALPHA_NUMERIC_MIXED_CASE_SYMBOLS }"
            android:inputType="textPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@={foo.somethingEditable}"/>

    </LinearLayout>
</layout>

mdumrauf avatar Aug 14 '17 06:08 mdumrauf