Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextInputLayout validation doesn't work. #35

Closed
khadkarajesh opened this issue Aug 21, 2017 · 12 comments
Closed

TextInputLayout validation doesn't work. #35

khadkarajesh opened this issue Aug 21, 2017 · 12 comments

Comments

@khadkarajesh
Copy link

khadkarajesh commented Aug 21, 2017

It doesn't work with the TextInputLayout validation.
Implementation:
`
Java Code

AwesomeValidation awesomeValidation = new AwesomeValidation(ValidationStyle.TEXT_INPUT_LAYOUT);

awesomeValidation.addValidation(getActivity(), R.id.test_til_email,Patterns.EMAIL_ADDRESS, R.string.error_email);

 awesomeValidation.validate()

*XML

<android.support.design.widget.TextInputLayout
        android:id="@+id/test_til_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="enter email" />
    </android.support.design.widget.TextInputLayout>
@thyrlian
Copy link
Owner

Could you please provide more details? E.g. layout xml, code, screenshot? Thanks.

@khadkarajesh
Copy link
Author

@thyrlian I have updated above. Please have a look above.

@thyrlian
Copy link
Owner

Thanks @rajeshkumarkhadka. Could you please tell me what's exactly wrong with the behavior? Any log? Any screenshot? Does the getActivity() return the right activity back? How do you trigger the awesomeValidation.validate() (in some onClickListener)?

@obernal
Copy link

obernal commented Aug 23, 2017

I saw this problem too while testing validation with TextInputLayout. getActivity() returns the right activity as it is the Fragment's getActivity() method. Perhaps the problem is using the library from inside a fragment?

As to what are the symptoms of it not working, basically the problem is mAwesomeValidation.validate() is always true despite having added the proper validation so obviously no error message is displayed.

I've tried the following:
On Fragment's onCreateView()

mAwesomeValidation = new AwesomeValidation(ValidationStyle.TEXT_INPUT_LAYOUT);
mAwesomeValidation.addValidation(getActivity(), R.id.usernameWrapper, Patterns.EMAIL_ADDRESS, R.string.err_email);

The Fragment implements OnClickListener and checks whether mAwesomeValidation.validate() == true whenever a button inside the fragment is clicked. The result is always true. Hope that helps!

    @Override
    public void onClick(View v) {
        if (mAwesomeValidation.validate() == true) {
            //It's always executing this code even though the fields have validation issues.
        }
    }

@thyrlian
Copy link
Owner

Thanks @obernal for sharing your ideas.

  • Actually fragment is not a problem, I use it also in the demo app
  • Good to know that the activity passed in is the right one, when not sure, better check with debugger
  • I remember now that someone else also had a similar problem - always return true on validate(). That was because of placing the addValidation in the wrong lifecycle call. Please try with some point after onCreateView to see if the problem still exists or not.

@khadkarajesh
Copy link
Author

@thyrlian I have used fragment's onCreateView method. I didn't find any logs related to the library.

@thyrlian
Copy link
Owner

thyrlian commented Aug 23, 2017

Thanks @rajeshkumarkhadka. Would you please try to move the code to another place other than onCreateView (something later)?

@khadkarajesh
Copy link
Author

@thyrlian works on onActivityCreated 👍 .

@thyrlian
Copy link
Owner

@rajeshkumarkhadka cool, glad to hear it's working now. As said before, if the activity is OK, most likely the view to be validated is not available yet at the given lifecycle call.

@pixma
Copy link

pixma commented Sep 20, 2017

I am trying to use this library with -

support.design.widget.CoordinatorLayout as Root Layout.

EditText is wrapped by TextInputLayout and there is no fragment implemented.
Its just in One simple activity and its crashing at "validate()" method

Msg monitor shows:
"invoke virtual method 'void android.support.design.widget.TextInputLayout.setError(java.lang.CharSequence)' on a null object reference"

I guess Still not fixed or This library is meant to be used with Fragments only?

@thyrlian
Copy link
Owner

@pixma

  • From the README's sample code, you'll find below instruction:
// to validate TextInputLayout, pass the TextInputLayout, not the embedded EditText
AwesomeValidation mAwesomeValidation = new AwesomeValidation(TEXT_INPUT_LAYOUT);
mAwesomeValidation.addValidation(activity, R.id.til_email, Patterns.EMAIL_ADDRESS, R.string.err_email);
  • From the demo app, this library works perfectly with both activity and fragment.
  • According to your log, it seems like the TextInputLayout you passed in is somehow null. Please try to debug about this object.

@lokendra45
Copy link

Cannot resolve method 'addValidation(androidx.fragment.app.FragmentActivity, int, int)' . Error Throw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants