Re: Form Validation with multi-lingual fields ?

[email protected]
Newsgroups gmane.comp.cms.jahia.devel
Message-ID <OFC24AE456.C358EC16-ONC12570DD.00284C20-C12570DD.003061B4@commaro.com>
Sébastien Landeau <[email protected]> wrote on 19.12.2005 
18:06:33:

> Is there a modification planned in the roadmad to do a global (in 
> all languages) check of the content before submission ? 

There is already a check called requiredMandatoryLang, which does a global 
"in all mandatory languages" check before submission, but this is not what 
you need, because you don't simply want to set the field for all mandatory 
languages, but you want to set it mandatory only for those languages, 
where you have an image. 

As far as I understand the standard validwhen check from Struts, it just 
checks the current language of the referred field, so this is also not 
enough. 

You could try this: 

In addition to the current validwhen check you could add a new validwhen 
and a method to your bean like this:

< field property = "imageAlt" depends = "validwhen" >
    < msg name = "validwhen" key = "errors.mandlang" /> 
    < arg0 key = "ImageAlt" resource = "false" /> 
    < var > 
        < var-name > test </ var-name >    
        < var-value > imageAltMissingLang == null </ var-value > 
    </ var > 
</ field > 

new method:

  public String getImageAltMissingLang() {
        StringBuffer missingLanguages = new StringBuffer();

        JahiaMltHelper imageMlt = getJahiaMultiLanguageField("Image");
        JahiaMltHelper imageAltMlt = 
getJahiaMultiLanguageField("ImageAlt");

        String[] imageTexts = imageMlt.getText();
        String[] imageLanguages = imageMlt.getLanguage();

        for (int i = 0; i < imageTexts.length; i++) {
            if (imageTexts[i] != null && imageTexts[i].length() > 0) {
                String imageAlt = imageAltMlt
                        .getText(imageLanguages[i]);
                if (imageAlt == null || imageAlt.length() == 0) {
                    missingLanguages.append(imageLanguages[i]);
                    missingLanguages.append(" ");
                }
            }
        }
        return missingLanguages.toString();
    }

I have not tested this yet, but I hope you understand the principal. You 
iterate through all languages of the image field and if it is set you 
check if the ImageAlt is set for the language. If it is not set you add 
the language to the returned String. If the returned String is not empty 
the message under the key "errors.mandlang" is displayed. You could also 
define a more fancy message and the missing languages as parameter to the 
message.

Let me know if it works, otherwise I will take a closer look and try to 
reproduce.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.