package julianfalcionelli.magicform.helper;

import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class ValidationHelper
{
	public static final String TAG = ValidationHelper.class.getSimpleName();

	public static void cleanError(View view)
	{
		if (view instanceof EditText)
		{
			cleanError((EditText) view);
			return;
		}

		if (view instanceof TextView)
		{
			((TextView) view).setError(null);
			return;
		}

	}

	private static void cleanError(EditText editText)
	{
		try
		{
			((TextInputLayout) editText.getParent()).setError(null);
			((TextInputLayout) editText.getParent()).setErrorEnabled(false);
		} catch (ClassCastException e)
		{
			editText.setError(null);
		}
	}

	public static void setError(View view, String error)
	{
		if (view instanceof EditText)
		{
			setError((EditText) view, error);
			return;
		}

		if (view instanceof TextView)
		{
			((TextView) view).setError(error);
			return;
		}
	}

	private static void setError(EditText editText, String error)
	{
		try
		{
			((TextInputLayout) editText.getParent()).setErrorEnabled(true);
			((TextInputLayout) editText.getParent()).setError(error);
		} catch (ClassCastException e)
		{
			editText.setError(error);
		}
	}
}

@@ -38,16 +38,103 @@
 elper;%0A%0A
+import android.support.annotation.NonNull;%0Aimport android.support.annotation.Nullable;%0A
 import a
@@ -205,16 +205,48 @@
 w.View;%0A
+import android.view.ViewParent;%0A
 import a
@@ -691,59 +691,126 @@
 %7B%0A%09%09
-try%0A%09%09%7B%0A%09%09%09((TextInputLayout) editText.getParent())
+TextInputLayout textInputLayout = getTextInputLayout(editText);%0A%0A%09%09if (textInputLayout != null)%0A%09%09%7B%0A%09%09%09textInputLayout
 .set
@@ -821,27 +821,25 @@
 r(null);%0A%09%09%09
-((T
+t
 extInputLayo
@@ -840,39 +840,16 @@
 utLayout
-) editText.getParent())
 .setErro
@@ -872,37 +872,15 @@
 %0A%09%09%7D
- catch (ClassCastException e)
+%0A%09%09else
 %0A%09%09%7B
@@ -1227,21 +1227,45 @@
 %7B%0A%09%09
-try%0A%09%09%7B%0A%09%09%09((
+TextInputLayout textInputLayout = get
 Text
@@ -1271,34 +1271,33 @@
 tInputLayout
-) 
+(
 editText
 .getParent()
@@ -1280,37 +1280,81 @@
 out(editText
-.getParent())
+);%0A%0A%09%09if (textInputLayout != null)%0A%09%09%7B%0A%09%09%09textInputLayout
 .setErrorEna
@@ -1368,19 +1368,17 @@
 ue);%0A%09%09%09
-((T
+t
 extInput
@@ -1387,118 +1387,445 @@
 yout
-) editText.getParent()).setError(error);%0A%09%09%7D catch (ClassCastException e)%0A%09%09%7B%0A%09%09%09editText.setError(error);%0A%09%09%7D
+.setError(error);%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09editText.setError(error);%0A%09%09%7D%0A%09%7D%0A%0A%09@Nullable%0A%09private static TextInputLayout getTextInputLayout(@NonNull EditText editText)%0A%09%7B%0A%09%09View currentView = editText;%0A%0A%09%09for (int i = 0; i %3C 2; i++)%0A%09%09%7B%0A%09%09%09ViewParent parent = currentView.getParent();%0A%0A%09%09%09if (parent instanceof TextInputLayout)%0A%09%09%09%7B%0A%09%09%09%09return (TextInputLayout) parent;%0A%09%09%09%7D%0A%09%09%09else%0A%09%09%09%7B%0A%09%09%09%09currentView = (View) parent;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return null;
 %0A%09%7D%0A

Fix on retrieve TextInputLayout -- on support 24+ the input layout is not directly the parent of the edit text