Skip to content

Start and obtain form data

Benjamin Mwalimu edited this page May 10, 2021 · 1 revision

Starting the form

Neat Form parses the JSON form provided from the Android Assets directory into a model that it uses to render the views. The supported views are registered first before the form is built. Once the views have been created as children to the the RootView they are then passed to the FormBuilder.

You can override the supported views with your own implementation(s) if the features provided out of the box by neat form do not suffice. However one thing to note is that you can only extend the classes that implement ViewBuilder but not the NFormView classes.

Starting the form

Code snippet for starting up a form

    public void startForm(int jsonFormActivityRequestCode, String formName, String entityId, boolean translate) throws Exception {

        String currentLocationId = "Kenya";


        JSONObject jsonForm = getFormJson(formName);
        if (jsonForm != null) {
            jsonForm.getJSONObject("metadata").put("encounter_location", currentLocationId);
            switch (formName) {
                case "rules_engine_demo": {
                    Intent intent = new Intent(this, JsonWizardFormActivity.class);
                    intent.putExtra("json", jsonForm.toString());
                    Log.d(getClass().getName(), "form is " + jsonForm.toString());

                    Form form = new Form();
                    form.setName("Rules engine demo");
                    form.setWizard(true);
                    form.setNextLabel(getString(R.string.next));
                    form.setPreviousLabel(getString(R.string.previous));
                    form.setSaveLabel(getString(R.string.save));
                    form.setActionBarBackground(R.color.customAppThemeBlue);
                    form.setNavigationBackground(R.color.button_navy_blue);
                    form.setHideSaveLabel(true);
                    intent.putExtra(JsonFormConstants.JSON_FORM_KEY.FORM, form);
                    intent.putExtra(JsonFormConstants.PERFORM_FORM_TRANSLATION, translate); // Used to trigger MLS 2.0 on the forms
                    startActivityForResult(intent, jsonFormActivityRequestCode);
                    break;
                }
                default: {
                    break;
                }
            }

        }

    }

Obtaining data from a form

Code snippet to obtain data from the form.

    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            String jsonString = data.getStringExtra("json");
            Log.i(getClass().getName(), "Result json String !!!! " + jsonString);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    

After you have he JSON string then you can manipulate it to either save the data from the form or use the data however you feel fit.

NOTE: You cannot retrieve data from the form when the form is invalid (when any field is invalid or when a required field is missing); an error dialog will be displayed instead.