Ticket #196 (assigned enhancement)

Opened 8 years ago

Last modified 8 years ago

Feature request: Rule ConditionalNotRequired

Reported by: bjoernkraus@… Owned by: schst
Priority: normal Milestone: v1.0.0
Component: patForms Version: SVN-HEAD
Severity: normal Keywords:
Cc:

Description (last modified by schst) (diff)

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;
	}
}
?>

Change History

comment:1 Changed 8 years ago by schst

  • Owner changed from argh to schst
  • Severity changed from normal to enhancement
  • Description modified (diff)
  • Milestone changed from none to v1.0.0

I'll add this right after 0.9.0 is released and the feature freeze is over.

comment:2 Changed 8 years ago by schst

  • Status changed from new to assigned

comment:3 Changed 8 years ago by bjoernkraus@…

I just recognized that the rule needs further modification: the required value of the required fields should only be changed if one of the "flag" elements has the specified value.

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;

}

}

if( $required == 'no' ) {

foreach( $this->_requiredFields as $field ) {

$el = &$form->getElement( $field ); $el->setAttribute( 'required', $required );

}

}

return true;

}

Note: See TracTickets for help on using tickets.