Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alkacon formgenerator 2.0.1 - Type Select Box & param values #16

Open
SandrineProusteau opened this issue Oct 8, 2012 · 1 comment
Open

Comments

@SandrineProusteau
Copy link

I have created a basic form with some Text input fields and some Selectbox fields.
My Selectbox field 'model' have as Default value a list of options with a default one:
var1*:Val 1|var2:Val 2

When I display my page with the form, and send to it no parameters, the field is displayed with the default value var1. Good.
But when I display my page with the parameter "model=var2" in ther URL, the field is also displayed with the default value var1!! It should be var2!

I found a solution in CmsForm, on function I_CmsField createInputField(
String xPath,
CmsXmlContent content,
CmsJspActionElement jsp,
Locale locale,
CmsMessages messages,
Map fieldTexts,
Map> subFieldPaths,
Map fileUploads,
String subFieldNameSuffix,
boolean initial,
boolean subField)
It's near line 2850. I do a first check with the selectedFormRequest, and use a flag to memore the selected option.

@SandrineProusteau
Copy link
Author

This is my patch (sorry for the comments in french) :

if (field.needsItems()) {
    // create items for checkboxes, radio buttons and selectboxes
    String fieldValue = content.getStringValue(cms, inputFieldPath + NODE_FIELDDEFAULTVALUE, locale);
    if (CmsStringUtil.isNotEmpty(fieldValue)) {
        // get items from String 
        boolean showInRow = false;
        if (fieldValue.startsWith(MACRO_SHOW_ITEMS_IN_ROW)) {
            showInRow = true;
            fieldValue = fieldValue.substring(MACRO_SHOW_ITEMS_IN_ROW.length());
        }
        
        // -------------------PATCH 1 : Traitement de fieldValue au macroResolver
        // 1.   au lieu de tester directement le fieldValue, et de renvoyer une erreur si il est vide,
        //      on le resout avec CmsMacroResolver d'abord.
        String oldFieldValue = fieldValue;
        CmsMacroResolver resolver = CmsMacroResolver.newInstance().setCmsObject(cms).setJspPageContext(jsp.getJspContext());
        fieldValue = resolver.resolveMacros(fieldValue);
        LOG.debug("    PATCH 1 (traitement du FieldValue au CmsMacroResolver) : oldFieldValue = '" + oldFieldValue + "' ... fieldValue is now = '" + fieldValue + "'");
        // -------------------FIN EURELIS 1
        
        if (CmsStringUtil.isNotEmpty(fieldValue)) {
            
            // -------------------PATCH 2 : Recherche option selectionnee par parametre
            // 2.   on fait d'abord un passage sur les options et on regarde si on a une option qui est selectionnee par un parametre en requete.
            //      si oui, on fait le flad "flagFoundSelected" a true.
            boolean flagFoundSelected = false;
            StringTokenizer T = new StringTokenizer(fieldValue, "|");
            List items = new ArrayList(T.countTokens());
            while (T.hasMoreTokens()) {
                String part = T.nextToken();
                // check pre selection of current item
                boolean isPreselected = part.indexOf('*') != -1;
                String value = "";
                //String selected = "";
                int delimPos = part.indexOf(':');
                if (delimPos != -1) {
                    // a special label text is given
                    value = part.substring(0, delimPos);
                } else {
                    // no special label text present, use complete String
                    value = part;
                }
                if (isPreselected) {
                    // remove preselected flag marker from Strings
                    value = CmsStringUtil.substitute(value, "*", "");
                }
                String selectedFromRequest = readSelectedFromRequest(field, value);
                if(selectedFromRequest.equalsIgnoreCase("true")){
                    flagFoundSelected = true;
                }
            }
            LOG.debug("    PATCH 2 (Recherche option selectionnee par parametre) : flagFoundSelected = '" + flagFoundSelected + "'");
            // -------------------FIN EURELIS 2
            T = new StringTokenizer(fieldValue, "|");
            items = new ArrayList(T.countTokens());
            while (T.hasMoreTokens()) {
                String part = T.nextToken();
                // check pre selection of current item
                boolean isPreselected = part.indexOf('*') != -1;
                String value = "";
                String label = "";
                String selected = "";
                int delimPos = part.indexOf(':');
                if (delimPos != -1) {
                    // a special label text is given
                    value = part.substring(0, delimPos);
                    label = part.substring(delimPos + 1);
                } else {
                    // no special label text present, use complete String
                    value = part;
                    label = value;
                }
                if (isPreselected) {
                    // remove preselected flag marker from Strings
                    value = CmsStringUtil.substitute(value, "*", "");
                    label = CmsStringUtil.substitute(label, "*", "");
                }
                // -------------------PATCH 3 : Prise en compte des parametres et du flag du Patch 2
                // 3.   on regarde si l'option est celle passee en parametre (si existence).
                String selectedFromRequest = readSelectedFromRequest(field, value);
                if (initial) {
                    // only fill in values from configuration file if called initially
                    if (isPreselected) {
                        if(!flagFoundSelected){
                            selected = Boolean.toString(true);
                        }else{
                            // get selected flag from request for current item
                            selected = selectedFromRequest;
                        }
                    }else{
                        // get selected flag from request for current item
                        selected = selectedFromRequest;
                    }
                } else {
                    // get selected flag from request for current item
                    selected = selectedFromRequest;
                }
                // -------------------FIN PATCH 3
                // add new item object
                items.add(new CmsFieldItem(value, label, Boolean.valueOf(selected).booleanValue(), showInRow));
                
            }
            field.setItems(items);
        }else{
            // no items specified for checkbox, radio button or selectbox, after CmsMacroResolver
            throw new CmsConfigurationException(Messages.get().container(
                Messages.ERR_INIT_INPUT_FIELD_MISSING_ITEM_2,
                field.getName(),
                field.getType()));
        }
    } else {
        // no items specified for checkbox, radio button or selectbox, before CmsMacroResolver
        throw new CmsConfigurationException(Messages.get().container(
            Messages.ERR_INIT_INPUT_FIELD_MISSING_ITEM_2,
            field.getName(),
            field.getType()));
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant