wiki.techinc.nl/includes/htmlform/HTMLHiddenField.php
Brad Jorsch 8a58e69b46 HTMLForm: Improve hidden field handling
HTMLHiddenField is unable to output a non-default value, which makes it
unsuitable for actually persisting changes across multiple form
submissions. To preserve compatibility with forms that aren't expecting
persistence, fixing this requires a new parameter in the field
definition.

Also, due to the unusual way that hidden fields are added to the HTML
output, they are not being correctly handled by HTMLFormFieldCloner.
Special-case that.

Change-Id: I1fde7372401299b4432d28ac61982d47d5f3bbea
2014-10-09 13:40:04 -04:00

58 lines
1.2 KiB
PHP

<?php
class HTMLHiddenField extends HTMLFormField {
protected $outputAsDefault = true;
public function __construct( $params ) {
parent::__construct( $params );
if ( isset( $this->mParams['output-as-default'] ) ) {
$this->outputAsDefault = (bool)$this->mParams['output-as-default'];
}
# Per HTML5 spec, hidden fields cannot be 'required'
# http://www.w3.org/TR/html5/forms.html#hidden-state-%28type=hidden%29
unset( $this->mParams['required'] );
}
public function getHiddenFieldData( $value ) {
$params = array();
if ( $this->mID ) {
$params['id'] = $this->mID;
}
if ( $this->outputAsDefault ) {
$value = $this->mDefault;
}
return array( $this->mName, $value, $params );
}
public function getTableRow( $value ) {
list( $name, $value, $params ) = $this->getHiddenFieldData( $value );
$this->mParent->addHiddenField( $name, $value, $params );
return '';
}
/**
* @param string $value
* @return string
* @since 1.20
*/
public function getDiv( $value ) {
return $this->getTableRow( $value );
}
/**
* @param string $value
* @return string
* @since 1.20
*/
public function getRaw( $value ) {
return $this->getTableRow( $value );
}
public function getInputHTML( $value ) {
return '';
}
}