Imustech Solutions
Imustech Solutions

Reputation: 31

How can I validate a form inside Android Material Stepper?

I have bee using the stepper library from https://github.com/stepstone-tech/android-material-stepper

I have already created the fragments and adapter for the stepper. It runs with no error. Each fragment has some form elements.

To validate those elements I am using AwesomeValidation library. But when I am running validate() method on validation it does not work. It is not giving any error either.

It will be very helpful if someone guides me with the right way of doing it.

Here is the code:

public class FragmentProfileBasic extends Fragment implements BlockingStep {

    EditText et_fname,et_lname,et_phone,et_address,et_city,et_state,et_profile_bio,et_dob;
    Spinner rel_status;
    RadioGroup radioGroup;
    RadioButton radioButton;
    String relStatus,gender,phone,address,city,state,profile_bio,dob;
    AwesomeValidation awVal;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_profile_basic, container, false);

        et_fname=v.findViewById(R.id.fname);
        et_lname=v.findViewById(R.id.lname);
        et_phone=v.findViewById(R.id.phone);
        //et_address=v.findViewById(R.id.address);
        et_city=v.findViewById(R.id.city);
        et_state=v.findViewById(R.id.state);
        et_profile_bio=v.findViewById(R.id.profile_bio);
        et_dob=v.findViewById(R.id.dob);
        radioGroup = v.findViewById(R.id.radio);

        awVal = new AwesomeValidation(ValidationStyle.BASIC);
        awVal.addValidation(getActivity(), R.id.fname, "[a-zA-Z\\s]+", R.string.fname_error);
        awVal.addValidation(getActivity(), R.id.phone, "[0-9]+", R.string.phone_error);
        awVal.addValidation(getActivity(), R.id.city, "[a-zA-Z\\s]+", R.string.city_error);
        awVal.addValidation(getActivity(), R.id.state, "[a-zA-Z\\s]+", R.string.state_error);
        awVal.addValidation(getActivity(), R.id.profile_bio, "[a-zA-Z0-9!#@.\\s]+", R.string.covertitle_error);
        //awVal.addValidation(this, R.id.profile_bio, "[a-zA-Z0-9!#@.\\s]+", R.string.bio_error);
        awVal.addValidation(getActivity(), R.id.dob, "[0-9/]+", R.string.dob_error);

        return v;
    }

    @Override
    public VerificationError verifyStep() {
        //return null if the user can go to the next step, create a new VerificationError instance otherwise
        return null;
    }

    @Override
    public void onSelected() {
        //update UI when selected
    }

    @Override
    public void onError(@NonNull VerificationError error) {
        //handle error inside of the fragment, e.g. show error on EditText
    }

    @Override
    public void onNextClicked(StepperLayout.OnNextClickedCallback callback) {
        if(awVal.validate()) {
            callback.goToNextStep();
        }
    }

    @Override
    public void onCompleteClicked(StepperLayout.OnCompleteClickedCallback callback) {
        callback.complete();
    }

    @Override
    public void onBackClicked(StepperLayout.OnBackClickedCallback callback) {

    }
}

Upvotes: 1

Views: 945

Answers (1)

Emmanuel Njorodongo
Emmanuel Njorodongo

Reputation: 1292

I am gonna validate with checking if the edit texts have text then you can use your own validations using the template. If you are using tabs for the stepper layout the new VerificationError will be the one displayed on top of the tabs and the icon will change to error icon


@Nullable
@Override
public VerificationError verifyStep() {

               if (et_fname.getText().toString().trim().isEmpty()) {

                    et_fname.requestFocus();

                    return new VerificationError("First name has not been entered");

                } else if (et_lname.getText().toString().trim().isEmpty()) {

                    et_lname.requestFocus();

                    return new VerificationError("Last name has not been entered");


                } else if (et_phone.getText().toString().trim().isEmpty()) {

                    et_phone.requestFocus();

                    return new VerificationError("Phone number has not been entered");


                } else if (et_city.getText().toString().trim().isEmpty()) {

                    et_city.requestFocus();

                    return new VerificationError("City has not been entered");


                } else if (et_state.getText().toString().trim().isEmpty()) {

                    et_state.requestFocus();

                    return new VerificationError("State has not been entered");


                } else {

                      return null;
                }


        return new VerificationError("We encountered an error");

}

Upvotes: 0

Related Questions