ragunathjawahar / android-saripaar

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

How to use Saripaar with DatabindingUtils in android #155

Open ManishPatiyal opened 8 years ago

ManishPatiyal commented 8 years ago

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.

xrnd commented 7 years ago

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

byronsliger commented 7 years ago

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.

mdumrauf commented 7 years ago

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 commented 7 years ago

@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>
ParitoshVaidya commented 6 years ago

@mdumrauf convalida validation library is doing the same.

URL: https://github.com/WellingtonCosta/convalida Doc: https://github.com/WellingtonCosta/convalida/wiki/Using-With-Data-Binding