package 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. import java.util.*; import org.apache.wicket.markup.html.form.*; import org.apache.wicket.validation.*; import votorola.a.web.wic.*; import votorola.g.lang.*; import votorola.g.locale.*; /** A validator to ensure that exactly one of the form's user identifier fields contains * input. */ abstract @ThreadRestricted("wicket") class IDFieldValidator extends org.apache.wicket.markup.html.form.validation.AbstractFormValidator { /** The fields for user identifiers, one for each of the authentication options * offered by the form. */ abstract List> idFields(); // - I - F o r m - V a l i d a t o r -------------------------------------------------- public FormComponent[] getDependentFormComponents() { return dependentFormComponents; } // private final FormComponent[] dependentFormComponents = new FormComponent[] { openidField, emailField }; /// not actually needed, so just: private static final FormComponent[] dependentFormComponents = new FormComponent[] {}; public void validate( final Form form ) { final List> idFields = idFields(); final ArrayList> inputs = new ArrayList>( /*initial capacity*/2 ); for( final TextField field: idFields ) { if( field.getConvertedInput() != null ) inputs.add( field ); } final int iN = inputs.size(); if( iN == 1 ) return; // valid if( iN == 0 ) { idFields.get(0).error( (IValidationError)new ValidationError().setMessage( VRequestCycle.get().bunW().l( "a.web.wic.authen.WP_Login.Required" ))); } else if( iN > 1 ) { idFields.get(0).error( (IValidationError)new ValidationError().setMessage( VRequestCycle.get().bunW().l( "a.web.wic.authen.WP_Login.single" ))); } else assert false; for( int f = idFields.size() - 1; f > 0; --f ) // render others invalid too { idFields.get(f).error( (IValidationError)new ValidationError().setMessage( WC_Feedback.INVISIBLE_MESSAGE_STRING )); // so rendered as invalid } } }