Form API의 숫자 필드 유형


12

FAPI를 사용하여 양식에 "숫자"필드 유형을 추가하려고합니다.

$form['phone_number']['areacode'] = array(
  '#type' => 'textfield',
  '#title' => '---',
  '#width' => '30%',
  '#align' => 'center',
  '#required' => true,
  '#maxlength' => 3
);

TYPE을 "number"로 변경하면 필드가 전혀 생성되지 않습니다. 숫자 모듈이 활성화되었습니다. 다음 테마 기능을 구현했습니다.

  • MYTHEME_form_element
  • MYTHEME_textfield
  • MYTHEME_container

#type = number또는 #type = number_integer등을 사용할 때이 필드 뒤에 무엇이 표시되지 않을 수 있습니까?

이것은 그것과 관련이있을 수 있습니다 :

사용자 정의 양식 코드에서 숫자 필드 (정수 및 10 진수)를 수동으로 작성

그러나 실제로 스마트 폰에 숫자 다이얼러가 표시되도록 HTML에서 유형을 "숫자"로 렌더링하려고합니다.

어떤 아이디어?

답변:


13
$form['my_element'] = array(
    '#type' => 'textfield',
    '#attributes' => array(
        ' type' => 'number', // insert space before attribute name :)
    ),
    '#title' => 'My number',
    '#required' => true,
    '#maxlength' => 3
);

확실하지 않은 것은 무엇입니까? "양식 API의 숫자 필드 유형" 이 필요합니까? #attributes 배열에서 숫자, 날짜, 색상 등 원하는 값으로 속성 "type"( 공백 포함 )을 정의하십시오 . dirty hack
Arthur

최대 길이는 작동하지 않습니다
logeshvaran

1
왜 공간이 있습니까?
fritzmg

이로 인해 잘못된 HTML btw가 발생합니다.<input type="number" type="text">
fritzmg

5

모듈 요소 를 설치하고 사용할 수 있습니다 .


이것이 HTML5 숫자 필드를 얻는 올바른 방법입니다.
Decipher

1
#type="numberfield"이 모듈을 사용할 때 필드를 조정하는 올바른 방법입니다.
YPCrumble

1
이 모듈은 Drupal 8의 코어에 이미 포함되어 있습니다. 숫자 필드를 작성하는 올바른 방법 은 렌더 배열에서 '#type' => 'number'(not 'numberfield')을 사용하는 것입니다.
fritzmg

2

나는 파티에 조금 늦었다. 문제는 theme_textfield (D7 : includes/form.inc, line 3924ff) 하드 코드 $element['#attributes']['type'] = 'text';입니다.

해당 통화를 포장 if (!isset($element['#attributes']['type'])) { ... }하거나 https://www.drupal.org/node/2545318 패치 가 적용될 때까지 기다릴 수 있습니다.


1

이것은 아래 코드입니다 ..

$form['phone_number']['areacode'] = array(
'#type' => 'textfield',
'#title' => '---',
'#width' => '30%',
'#align' => 'center',
'#required' => true,
'#maxlength' => 3
);

[ 'phone_number'], 컨테이너인지 여부 또는 양식에 존재하는지 여부를 알려주십시오. 양식에 사용자 정의 필드를 작성하려면 hook_form_alter hook_form_alter 를 사용해야합니다

처럼

  function mymodule_form_alter($form_id, &$form) {
 if ($form_id == 'form_id') {

$form['field_name'] = array(
 '#type' => 'textfield',     
 '#title' => t('title'),
 '#description' => t('Description.'),
 '#width' => '30%',
 '#align' => 'center',
 '#required' => true,
 '#maxlength' => 3
 );
  }
   }

도움이 되길 바랍니다 ..


0

#type 필드에 유효하지 않은 값으로 작동하는 것을 보지 못했지만 다음과 같이 할 수 있습니다.

양식 요소 :

$form['my_element'] = array(
    '#type' => 'textfield',
    '#attributes' => array(
        'data-type' => 'number',
    ),
    '#title' => '---',
    '#width' => '30%',
    '#align' => 'center',
    '#required' => true,
    '#maxlength' => 3
);

그리고 theme_textfield 구현

if(isset($element['#attributes']['data-type']) && $element['#attributes']['data-type'] === 'number'){
    $output = '<input type="number" />';
}else{
    $output = '<input' . drupal_attributes($element['#attributes']) . ' />';
}

솔루션에 대해 똑같은 생각을하고 있었지만 잘못된 #value로 작동하지 않는다는 것은 무엇입니까 ???
Alex.Barylski

#type에 유효한 값을 입력하지 않으면 앞에서 설명한대로 양식 요소가 전혀 렌더링되지 않습니다.
빅터 라 조프
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.