id	summary	reporter	owner	description	type	status	priority	milestone	component	version	severity	resolution	keywords	cc
196	Feature request: Rule ConditionalNotRequired	bjoernkraus@…	schst	"In some cases you'll need a form flag which, if it's set, remarks other form elements as NOT required. The following file is a very small modification if the original ConditionalRequired rule:
{{{
<?php
/**
 * patForms Rule ConditionalNotRequired
 *
 * $Id: $
 *
 * @package		patForms
 * @subpackage	Rules
 */

/**
 * patForms Rule ConditionalNotRequired
 *
 * This rule can be used to set the status of
 * some elements to NOT required depending on the value
 * of another element.
 *
 * It has to be applied prior to validating the form.
 *
 * @package		patForms
 * @subpackage	Rules
 * @author		Stephan Schmidt <schst@php-tools.net>
 * @author		Bjoern Kraus <krausbn@php-tools.net>
 * @license		LGPL, see license.txt for details
 * @link		http://www.php-tools.net
 */
class patForms_Rule_ConditionalNotRequired extends patForms_Rule
{
   /**
	* fields that will be required
	* @access	private
	* @var		array
	*/
	var $_requiredFields	=	array();

   /**
	* conditions
	* @access	private
	* @var		array
	*/
	var $_conditions		=	array();

   /**
	* set the names of the fields that will be required
	*
	* @access	public
	* @param	array	required fields
	*/
	function setRequiredFields( $fields )
	{
		$this->_requiredFields	=	$fields;
	}

   /**
	* add a condition
	*
	* @access	public
	* @param	string	condition field name
	* @param	mixed	condition value
	*/
	function addCondition( $field, $value )
	{
		$this->_conditions[$field]	=	$value;
	}

   /**
	* method called by patForms or any patForms_Element to validate the
	* element or the form.
	*
	* @access	public
	* @param	object patForms	form object
	*/
	function applyRule( &$form, $type = PATFORMS_RULE_BEFORE_VALIDATION )
	{
		$required	=	'yes';
		foreach( $this->_conditions as $field => $value )
		{
			$el		=	&$form->getElement( $field );
			$val	=	$el->getValue();
			if( $val == $value )
			{
				$required	=	'no';
				break;
			}
		}

		foreach( $this->_requiredFields as $field )
		{
			$el	=	&$form->getElement( $field );
			$el->setAttribute( 'required', $required );
		}
		
		return	true;
	}
}
?>
}}}"	enhancement	assigned	normal	v1.0.0	patForms	SVN-HEAD	normal			
