Validate form

Asked

Viewed 309 times

0

I’m developing a mobile application in android studio and I’m having difficulty implementing the validation of form input fields.

Can someone explain to me why I can’t validate?

Form class:

public class FormPalhetasActivity extends Activity implements 
AdapterView.OnItemSelectedListener, LocationListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );

    setContentView( R.layout.activity_form_palhetas );
    frua = (EditText) findViewById(R.id.rua);
    fbairro = (EditText) findViewById(R.id.bairro);
    fcompl = (EditText) findViewById(R.id.compl);
    fcidade = (EditText) findViewById(R.id.cidade);
    festado = (EditText) findViewById(R.id.estado);
    fpais = (EditText) findViewById(R.id.pais);
    fobs = (EditText) findViewById(R.id.observacao);
    fcod = (EditText) findViewById(R.id.cod);

    // Spinner element
    Spinner spinner = (Spinner) findViewById(R.id.spinner);

    // Spinner click listener
    spinner.setOnItemSelectedListener(this);

    // Spinner Drop down elements
    List<String> categories = new ArrayList<String>();
    categories.add("Terreno baldio");
    categories.add("Esgoto a ceu aberto");
    categories.add("Lixo");
    categories.add("Piscina");
    categories.add("Construção");

    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinner.setAdapter(dataAdapter);

    readMyCurrentCoordinates();
    Button enviar = (Button) findViewById( R.id.enviar );


    enviar.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(valid() == true){
                registerForms();
            }

        }
    } );
}

public static boolean validateNotNull(View pView, String pMessage) {
    if (pView instanceof EditText) {
        EditText edText = (EditText) pView;
        Editable text = edText.getText();
        if (text != null) {
            String strText = text.toString();
            if (!TextUtils.isEmpty(strText)) {
                return true;
            }
        }
        // em qualquer outra condição é gerado um erro
        edText.setError(pMessage);
        edText.setFocusable(true);
        edText.requestFocus();
        return false;
    }
    return false;
}

public boolean valid(){
    if(validateNotNull(frua,"Preencha o campo rua")) {
        return false;
    }if(validateNotNull(fcompl,"Preencha o campo complemento")) {
        return false;
    }if(validateNotNull(fbairro,"Preencha o campo bairro")) {
        return false;
    }if(validateNotNull(fcidade,"Preencha o campo cidade")) {
        return false;
    }if(validateNotNull(festado,"Preencha o campo estado")) {
        return false;
    }if(validateNotNull(fobs,"Preencha o campo observação")) {
        return false;
    }if(validateNotNull(fpais,"Preencha o campo país")) {
        return false;
    }if(validateNotNull(fpais,"Preencha o campo país")) {
        return false;
    }else{
        return true;
    }
}

Output: no notification appears that the field is empty.

1 answer

1


I think you should change the method valid() in order to return false if one of validateNotNull() for false

public boolean valid(){
    boolean isValid = true;
    if(!validateNotNull(frua,"Preencha o campo rua")) {
        isValid = false;
    }if(!validateNotNull(fcompl,"Preencha o campo complemento")) {
        isValid = false;
    }if(!validateNotNull(fbairro,"Preencha o campo bairro")) {
        isValid = false;
    }if(!validateNotNull(fcidade,"Preencha o campo cidade")) {
        isValid = false;
    }if(!validateNotNull(festado,"Preencha o campo estado")) {
        isValid = false;
    }if(!validateNotNull(fobs,"Preencha o campo observação")) {
        isValid = false;
    }if(!validateNotNull(fpais,"Preencha o campo país")) {
        isValid = false;
    }if(!validateNotNull(fpais,"Preencha o campo país")) {
        isValid = false;
    }
    return isValid;

}
  • Same way, it does not display the notification to fill the field, but does not allow registration

  • With what you suggested he is making only a conforming and aimgem that I posted now, but now he leaves register, even with blank fields. The strange thing is that he only showed of a single field.

  • Shows only one because it returns once it finds a blank one. If you want it to show everyone you have to change the method. See the new edit to the reply.

  • right, but he displays one and still registered

  • perfect @ramaral, now it worked out as wanted. Mto obgdo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.