001package votorola.a.web.wic.authen; // Copyright 2008-2012, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import java.util.*;
004import org.apache.wicket.markup.html.form.*;
005import org.apache.wicket.validation.*;
006import votorola.a.web.wic.*;
007import votorola.g.lang.*;
008import votorola.g.locale.*;
009
010
011/** A validator to ensure that exactly one of the form's user identifier fields contains
012  * input.
013  */
014abstract @ThreadRestricted("wicket") class IDFieldValidator
015  extends org.apache.wicket.markup.html.form.validation.AbstractFormValidator
016{
017
018
019    /** The fields for user identifiers, one for each of the authentication options
020      * offered by the form.
021      */
022    abstract List<TextField<?>> idFields();
023
024
025
026   // - I - F o r m - V a l i d a t o r --------------------------------------------------
027
028
029    public FormComponent<?>[] getDependentFormComponents() { return dependentFormComponents; }
030
031
032     // private final FormComponent<?>[] dependentFormComponents = new FormComponent<?>[] { openidField, emailField };
033     /// not actually needed, so just:
034        private static final FormComponent<?>[] dependentFormComponents = new FormComponent<?>[] {};
035
036
037
038    public void validate( final Form<?> form )
039    {
040        final List<TextField<?>> idFields = idFields();
041        final ArrayList<TextField<?>> inputs = new ArrayList<TextField<?>>( /*initial capacity*/2 );
042        for( final TextField<?> field: idFields )
043        {
044            if( field.getConvertedInput() != null ) inputs.add( field );
045        }
046        final int iN = inputs.size();
047        if( iN == 1 ) return; // valid
048
049        if( iN == 0 )
050        {
051            idFields.get(0).error( (IValidationError)new ValidationError().setMessage(
052              VRequestCycle.get().bunW().l( "a.web.wic.authen.WP_Login.Required" )));
053        }
054        else if( iN > 1 )
055        {
056            idFields.get(0).error( (IValidationError)new ValidationError().setMessage(
057              VRequestCycle.get().bunW().l( "a.web.wic.authen.WP_Login.single" )));
058        }
059        else assert false;
060        for( int f = idFields.size() - 1; f > 0; --f ) // render others invalid too
061        {
062            idFields.get(f).error( (IValidationError)new ValidationError().setMessage(
063              WC_Feedback.INVISIBLE_MESSAGE_STRING )); // so rendered as invalid
064        }
065    }
066
067
068
069}