Last active
September 18, 2020 14:05
-
-
Save cyberlex404/305095d3a37a47035b88aecacca134e6 to your computer and use it in GitHub Desktop.
Drupal 8 Form API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(array $form, FormStateInterface $form_state) { | |
// todo: add to schema. | |
$form['sync'] = [ | |
'#type' => 'details', | |
'#title' => $this->t('Synchronization settings'), | |
'#open' => TRUE, | |
]; | |
$form['sync']['rules'] = [ | |
'#type' => 'details', | |
'#title' => $this->t('Rules'), | |
'#required' => TRUE, | |
'#open' => TRUE, | |
'#tree' => TRUE, | |
]; | |
$form['sync']['rules']['import'] = [ | |
'#type' => 'textarea', | |
'#title' => $this->t('Import rule'), | |
'#default_value' => $this->config('pim.sync')->get('rules.import'), | |
'#rows' => 10, | |
]; | |
$form['sync']['rules']['publication'] = [ | |
'#type' => 'textarea', | |
'#title' => $this->t('Publication rule'), | |
'#default_value' => $this->config('pim.sync')->get('rules.publication'), | |
'#rows' => 10, | |
]; | |
return parent::buildForm($form, $form_state); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function validateForm(array &$form, FormStateInterface $form_state) { | |
$expressionLanguage = new ExpressionLanguage(); | |
$import = $form_state->getValue(['rules', 'import']); | |
if ($expressionLanguage->evaluate($import)) { | |
$form_state->setErrorByName('rules][import', $this->t('The phone number is too short. Please enter a full phone number.')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment