Search code examples
validationuser-interfaceandroid-annotationssaripaar

Android : Saripaar Validation Library. Need to set error message from strings.xml


I am using the the Saripaar validation library https://github.com/ragunathjawahar/android-saripaar to validate one of the forms. The library is pretty useful as it avoids a lot of boilerplate code for validating a form. However, the library uses annontations on widgets. Below is an example:

@Password(order = 1)
@TextRule(order = 2, minLength = 6, message = "Enter at least 6 characters.")
private EditText passwordEditText;

The order attribute is the one that specifies the order in which the validations are going to take place. Here is my problem: the message attribute takes a constant expression. I need to set this from string Resource file. So that error messages can be internationalized later. I have already tried below:

message = getResources().getString(R.string.err_msg)

But it doesnt seem to work as it needs compile time constants.

Can anyone help me with the same or guide me through a workaround?


Solution

  • Use this syntax :

    //private variable in the class declaration section
    private EditText txtFirstName=null; 
    @Required(order = 2,  messageResId  = R.string.err_required_field)
    

    It's in the lib's documentation (and the lib itself is an excellent one).