You are here

class ClientsideValidationDemoForm in Clientside Validation 2.0.x

Same name and namespace in other branches
  1. 8.2 clientside_validation_demo/src/Form/ClientsideValidationDemoForm.php \Drupal\clientside_validation_demo\Form\ClientsideValidationDemoForm
  2. 8 clientside_validation_demo/src/Form/ClientsideValidationDemoForm.php \Drupal\clientside_validation_demo\Form\ClientsideValidationDemoForm
  3. 3.0.x clientside_validation_demo/src/Form/ClientsideValidationDemoForm.php \Drupal\clientside_validation_demo\Form\ClientsideValidationDemoForm

Class ClientsideValidationDemoForm.

Hierarchy

Expanded class hierarchy of ClientsideValidationDemoForm

1 string reference to 'ClientsideValidationDemoForm'
clientside_validation_demo.routing.yml in clientside_validation_demo/clientside_validation_demo.routing.yml
clientside_validation_demo/clientside_validation_demo.routing.yml

File

clientside_validation_demo/src/Form/ClientsideValidationDemoForm.php, line 12

Namespace

Drupal\clientside_validation_demo\Form
View source
class ClientsideValidationDemoForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'clientside_validation_demo_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['text_1'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Text 1'),
      '#description' => $this
        ->t('Simple required text field.'),
      '#required' => TRUE,
    ];
    $form['text_2'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Text 2'),
      '#description' => $this
        ->t('Required text field with custom required_error message.'),
      '#required' => TRUE,
      '#required_error' => $this
        ->t('This message is coming from #required_error.'),
    ];
    $form['text_3'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Text 3'),
      '#description' => $this
        ->t('Required text field with max length.'),
      '#required' => FALSE,
      '#maxlength' => 10,
    ];
    $form['text_4'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Text 4'),
      '#description' => $this
        ->t('Conditionally required text field.'),
      '#required_error' => $this
        ->t('This message is coming from #required_error with #states.'),
      '#states' => [
        'required' => [
          ':input[name="text_1"]' => [
            'filled' => FALSE,
          ],
        ],
      ],
    ];
    $form['email_1'] = [
      '#type' => 'email',
      '#title' => $this
        ->t('E-Mail 1'),
      '#description' => $this
        ->t('Required E-Mail field.'),
      '#required' => TRUE,
    ];
    $form['email_2'] = [
      '#type' => 'email',
      '#title' => $this
        ->t('E-Mail 2'),
      '#description' => $this
        ->t('E-Mail field.'),
      '#required' => FALSE,
    ];
    $form['number_1'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Number 1'),
      '#description' => $this
        ->t('Number field.'),
      '#required' => FALSE,
    ];
    $form['number_2'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Number 2'),
      '#description' => $this
        ->t('Number field with max.'),
      '#max' => 100,
      '#required' => FALSE,
    ];
    $form['number_3'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Number 3'),
      '#description' => $this
        ->t('Number field with min.'),
      '#min' => 100,
      '#required' => FALSE,
    ];
    $form['number_4'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Number 4'),
      '#description' => $this
        ->t('Number field with min and max.'),
      '#min' => 100,
      '#max' => 200,
      '#required' => FALSE,
    ];
    $form['number_5'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Number 5'),
      '#description' => $this
        ->t('Number field with min, max and step.'),
      '#min' => 100,
      '#max' => 200,
      '#step' => 5,
      '#required' => FALSE,
    ];
    $form['url'] = [
      '#type' => 'url',
      '#title' => $this
        ->t('URL'),
      '#description' => $this
        ->t('URL field.'),
      '#required' => FALSE,
    ];
    $form['phone_1'] = [
      '#type' => 'textfield',
      '#title' => t('Phone Number'),
      '#size' => 60,
      '#maxlength' => 14,
      '#pattern' => "[789][0-9]{9}",
      '#required' => TRUE,
      '#placeholder' => t('Enter Phone Number - [789][0-9]{9}'),
    ];
    $form['text_equal_1'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Equal To'),
      '#description' => $this
        ->t('Field equal to another field.'),
      '#required' => TRUE,
    ];
    $form['text_equal_2'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Equal to check - default message'),
      '#description' => $this
        ->t('Field equal to another field.'),
      '#equal_to' => 'text_equal_1',
      '#required' => TRUE,
    ];
    $form['text_equal_3'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Equal to check - custom message'),
      '#description' => $this
        ->t('Field equal to another field.'),
      '#equal_to' => 'text_equal_1',
      '#required' => TRUE,
      '#equal_to_error' => $this
        ->t('Text should match value in Equal To.'),
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Submit'),
    ];
    $form['submit_ajax'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Submit AJAX'),
      '#ajax' => [
        'callback' => '::submitAjax',
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('All form validations passed.'));
  }

  /**
   * Ajax submit callback.
   */
  public function submitAjax(array $form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    return $response;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClientsideValidationDemoForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
ClientsideValidationDemoForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ClientsideValidationDemoForm::submitAjax public function Ajax submit callback.
ClientsideValidationDemoForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ClientsideValidationDemoForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 105
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.