Skip to content

Instantly share code, notes, and snippets.

@jackbravo
Created December 11, 2017 21:33
Show Gist options
  • Save jackbravo/a5d8dc47df2d89ca7a8542ec1dcff385 to your computer and use it in GitHub Desktop.
Save jackbravo/a5d8dc47df2d89ca7a8542ec1dcff385 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\guia_commerce\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class ExchangeRateForm.
*/
class ExchangeRateForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'guia_commerce.exchangerate',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'exchange_rate';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
#$config = $this->config('guia_commerce.exchangerate');
$config = $this->config('guia_commerce.exchangerate');
$form['exchange_rate_us'] = [
'#type' => 'textfield',
'#element_validate' => array('_element_validate_number'),
'#title' => $this->t('USD Dólar americano a BOB Boliviano'),
'#default_value' => $config->get('exchange_rate_us'),
];
$form['exchange_rate_bo'] = [
'#type' => 'textfield',
'#element_validate' => array('_element_validate_number'),
'#title' => $this->t('BOB Boliviano a USD Dólar americano'),
'#default_value' => $config->get('exchange_rate_bo'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$value_us = $form_state->getValue('exchange_rate_us');
$value_bo = $form_state->getValue('exchange_rate_bo');
if (!is_numeric($value_us)) {
$form_state->setErrorByName('exchange_rate_us', $this->t('El valor debe ser numérico'));
}
if (!is_numeric($value_bo)) {
$form_state->setErrorByName('exchange_rate_bo', $this->t('El valor debe ser numérico'));
}
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this->config('guia_commerce.exchangerate')
->set('exchange_rate_us', $form_state->getValue('exchange_rate_us'))
->set('exchange_rate_bo', $form_state->getValue('exchange_rate_bo'))
->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment