You are here

CardCode.php in Creditfield Form Element 8

File

src/Element/CardCode.php
View source
<?php

namespace Drupal\creditfield\Element;

use Drupal\Core\Render\Element\FormElement;
use Drupal\Core\Render\Element;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a one-line credit card number field form element.
 *
 * @FormElement("creditfield_cardcode")
 */
class CardCode extends FormElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#maxlength' => 4,
      '#autocomplete_route_name' => FALSE,
      '#element_validate' => [
        [
          $class,
          'validateCardCode',
        ],
      ],
      '#process' => [
        [
          $class,
          'processCardCode',
        ],
      ],
      '#pre_render' => [
        [
          $class,
          'preRenderCardCode',
        ],
      ],
      '#theme' => 'input__textfield',
      '#theme_wrappers' => [
        'form_element',
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function processCardCode(&$element, FormStateInterface $form_state, &$complete_form) {
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public static function validateCardCode(&$element, FormStateInterface $form_state, &$complete_form) {
    if (!static::numberIsValid($element['#value'])) {
      $form_state
        ->setError($element, t('Please enter a valid card code.'));
      return;
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    if ($input !== FALSE && $input !== NULL) {

      // Equate $input to the form value to ensure it's marked for
      // validation.
      return str_replace([
        "\r",
        "\n",
      ], '', $input);
    }
  }

  /**
   * Prepares a #type 'creditfield_code' render element for input.html.twig.
   *
   * @param array $element
   *   An associative array containing the properties of the element.
   *   Properties used: #title, #value, #description, #size, #maxlength,
   *   #placeholder, #required, #attributes.
   *
   * @return array
   *   The $element with prepared variables ready for input.html.twig.
   */
  public static function preRenderCardCode($element) {
    $element['#attributes']['type'] = 'text';
    Element::setAttributes($element, [
      'id',
      'name',
      'value',
      'size',
      'maxlength',
      'placeholder',
    ]);
    static::setAttributes($element, [
      'form-text',
    ]);
    return $element;
  }

  /**
   * Validation of the value submitted in the creditfield_cardcode field.
   * @param $value
   * @return bool
   */
  public static function numberIsValid($value) {

    // value is not an integer or is an integer but not between 3 and 4 digits
    return (bool) preg_match('/^\\d{3,4}$/', $value);
  }

}

Classes

Namesort descending Description
CardCode Provides a one-line credit card number field form element.