Merge "Import: Handle uploads with sha1 starting with 0 properly"
[mediawiki.git] / includes / htmlform / HTMLRadioField.php
blob2d057042ef48c04bb3cfb11334186421d508a5e9
1 <?php
3 /**
4 * Radio checkbox fields.
5 */
6 class HTMLRadioField extends HTMLFormField {
7 function validate( $value, $alldata ) {
8 $p = parent::validate( $value, $alldata );
10 if ( $p !== true ) {
11 return $p;
14 if ( !is_string( $value ) && !is_int( $value ) ) {
15 return false;
18 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
20 if ( in_array( strval( $value ), $validOptions, true ) ) {
21 return true;
22 } else {
23 return $this->msg( 'htmlform-select-badoption' )->parse();
27 /**
28 * This returns a block of all the radio options, in one cell.
29 * @see includes/HTMLFormField#getInputHTML()
31 * @param string $value
33 * @return string
35 function getInputHTML( $value ) {
36 $html = $this->formatOptions( $this->getOptions(), strval( $value ) );
38 return $html;
41 function getInputOOUI( $value ) {
42 $options = array();
43 foreach ( $this->getOptions() as $label => $data ) {
44 $options[] = array(
45 'data' => $data,
46 'label' => $this->mOptionsLabelsNotFromMessage ? new OOUI\HtmlSnippet( $label ) : $label,
50 return new OOUI\RadioSelectInputWidget( array(
51 'name' => $this->mName,
52 'value' => $value,
53 'options' => $options,
54 'classes' => 'mw-htmlform-flatlist-item',
55 ) + $this->getAttributes( array( 'disabled', 'tabindex' ), array( 'tabindex' => 'tabIndex' ) ) );
58 function formatOptions( $options, $value ) {
59 $html = '';
61 $attribs = $this->getAttributes( array( 'disabled', 'tabindex' ) );
62 $elementFunc = array( 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' );
64 # @todo Should this produce an unordered list perhaps?
65 foreach ( $options as $label => $info ) {
66 if ( is_array( $info ) ) {
67 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
68 $html .= $this->formatOptions( $info, $value );
69 } else {
70 $id = Sanitizer::escapeId( $this->mID . "-$info" );
71 $radio = Xml::radio( $this->mName, $info, $info === $value, $attribs + array( 'id' => $id ) );
72 $radio .= '&#160;' . call_user_func( $elementFunc, 'label', array( 'for' => $id ), $label );
74 $html .= ' ' . Html::rawElement(
75 'div',
76 array( 'class' => 'mw-htmlform-flatlist-item mw-ui-radio' ),
77 $radio
82 return $html;
85 protected function needsLabel() {
86 return false;