آموزش ماژول نویسی دروپال 8 قسمت 6

drupal

در این اموزش به فرم ها در دروپال ۸ میپردازیم   در ادامه قصد داریم ماژول قبلی را گسترش داده و به فیلد های که جدید در دروپال8 اضافه شده است بپردازیم

آموزش ماژول نویسی دروپال 8 قسمت 5

آموزش ماژول نویسی دروپال 8 قسمت 4

آموزش ماژول نویسی دروپال 8 قسمت 3

آموزش ماژول نویسی دروپال 8 قسمت 2

آموزش ماژول نویسی دروپال 8 قسمت 1

فرم های که به دروپال 8 اضافه شده اند به صوت زیر می باشد

tel

e-mail

number

date

url

search

range

color

در ادامه میخواهیم هر یک از فیلد ها را به مازول خودمون اضافه کنیم

<?php
namespace Drupal\custom\form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;

class CustomForm extends FormBase {

  /**
   * Returns a unique string identifying the form.
   *
   * The returned ID should be a unique string that can be a valid PHP function
   * name, since it's used in hook implementation names such as
   * hook_form_FORM_ID_alter().
   *
   * @return string
   *   The unique string identifying the form.
   */
  public function getFormId() {
    return 'my_custom_form';
  }
  /**
   * Form constructor.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   The form structure.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['description'] = [
      '#type' => 'item',
      '#markup' => $this->t('Please enter
      the title and accept the terms of use of the site.'),
    ];
    $form['title'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Title'),
      '#description' => $this->t('Enter the title of
      the book. Note that the title must be at least 10 characters in length.'),
      '#required' => TRUE,
    ];
    $form['phone'] = array(
      '#type' => 'tel',
      '#title' => t('Your telephone number'),
      '#placeholder' => '+0 (000) 000-0000',
    );
    $form['email'] = array(
      '#type' => 'email',
      '#title' => t('Your e-mail address'),
      '#placeholder' => 'john @example.com',
    );
    $form['stepped_float'] = array(
      '#type' => 'number',
      '#title' => t('This will allow the
       floating point numbers 2, 2.5, 3 and 3.5'),
      '#min' => 2,
      '#step' => 0.5,
      '#max' => 3.5,
    );
    $form['integer'] = array(
      '#type' => 'number',
      '#title' => t('This will allow only
      integers greater or equal to 41'),
      '#min' => 41,
    );
    $form['float'] = array(
      '#type' => 'number',
      '#title' => t('This will allow any
      floating point number smaller or equal to 100'),
      '#max' => 100,
      '#step' => 'any',
    );
    $form['stepped_float'] = array(
      '#type' => 'number',
      '#title' => t('This will allow the
      floating point numbers 2, 2.5, 3 and 3.5'),
      '#min' => 2,
      '#step' => 0.5,
      '#max' => 3.5,
    );
    $form['background_color'] = array(
      '#type' => 'color',
      '#title' => t('Pick a background color'),
    );
    $form['date'] = array(
      '#type' => 'date',
      '#title' => t('date'),
      '#date_data_format' => 'Y-m-d',
    );
    $form['keyword'] = array(
      '#type' => 'search',
      '#title' => t('Search'),
      '#placeholder' => t('Enter a term to look up'),
    );
    $form['accept'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('I accept the terms of use of the site'),
      '#description' => $this->t('Please read and accept the terms of use'),
    );
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Submit'),
    ];

    return $form;

  }
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);

    $title = $form_state->getValue('title');
    $accept = $form_state->getValue('accept');

    if (strlen($title) < 10) {
      // Set an error for the form element with a key of &quottitle&quot.
      $form_state->setErrorByName(
        'title', $this->t(
          'The title must be at least 10 characters long.'));
    }

    if (empty($accept)){
      // Set an error for the form element with a key of &quotaccept&quot.
      $form_state->setErrorByName(
        'accept', $this->t(
          'You must accept the terms of use to continue'));
    }

  }
  public function submitForm(array &$form, FormStateInterface $form_state) {
    drupal_set_message($form_state->getValue('title'));
    return;
  }
}