All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jaxx.runtime.validator.field.RequiredFileFieldValidator Maven / Gradle / Ivy

There is a newer version: 3.0-alpha-6
Show newest version
/*
 * *##% 
 * JAXX Runtime
 * Copyright (C) 2008 - 2009 CodeLutin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * ##%*
 */
package jaxx.runtime.validator.field;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

import java.io.File;

/**
 * 
 * RequiredFileFieldValidator checks that a File field is not null nor have an empty filename.
 * 
 * 

*

* *

    *
  • fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required
  • *
* *

*

*

 * 
 *     <validators>
 *         <!-- Plain-Validator Syntax -->
 *         <validator type="requiredFile">
 *             <param name="fieldName">tmp</param>
 *             <message>tmp is required</message>
 *         </validator>
 * 

* <!-- Field-Validator Syntax --> * <field name="tmp"> * <field-validator type="requiredFile"> * <message>tmp is required</message> * </field-validator> * </field> * </validators> * *

* * @author chemit */ public class RequiredFileFieldValidator extends FieldValidatorSupport { @Override public void validate(Object object) throws ValidationException { String fieldName = getFieldName(); Object value = this.getFieldValue(fieldName, object); if (value == null) { // no value defined addFieldError(fieldName, object); return; } File f; if (value instanceof File) { f = (File) value; } else if (value instanceof String) { f = new File((String) value); } else { addFieldError(fieldName, object); return; } if (f.getPath().trim().isEmpty()) { // f is not a directory nor exists addFieldError(fieldName, object); } } @Override public String getValidatorType() { return "requiredFile"; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy