简体   繁体   中英

How disable fields if have values in a custom form with php? Drupal 8

I have a custom form with several text fields, input, without values and want to validate the fields and when all the inputs have value are disabled or not allowed to modify the information once that values were inserted, the condition is that when everyone has a value at that time desabilitan and can not be mofificar more. This should do it in this form with php necessarily.

I am creating the fields like this:

  $form['general'] = array(
    '#type' => 'details',
    '#open' => true,
    '#title' => $this->t('Configuración General'),
  );
  $form['general']['app_name'] = array(
    '#type' => 'textfield',
    '#title' => $this->t('Nombre'),
    '#description' => $this->t(self::NAME_DESCRIPTION),
    '#default_value' => $config->get('app_name'),
  );
  $form['general']['app_icon'] = array(
      '#type' => 'managed_file',
      '#name' => 'app_icon',
      '#title' => t('Icono'),
      '#default_value' => $config->get('app_icon'),
      '#description' => t("This is the App icon! must be 1024x1024"),
      '#required' => true,
      '#upload_location' => 'public://files/icons/',
'#attributes' => array('class' => array('icons')),
  );
$form['general']['app_logo'] = array(
      '#type' => 'managed_file',
      '#name' => 'app_logo',
      '#title' => t('Logotipo'),
      '#default_value' => $config->get('_app_logo'),
      '#description' => t("This is the app Logo Must be 730x300"),
      '#required' => true,
      '#upload_location' => 'public://app/logos/',
'#attributes' => array('class' => array('icons')),
  );
$form['general']['app_theme'] = array(
    '#type' => 'radios',
    '#title' => $this->t('Apariencia'),
    '#description' => $this->t(self::THEME_DESCRIPTION),
    '#options' => array(
        'trabajo'=>'Trabajo',
        'naturaleza'=>'Naturaleza',
        'viajes'=>'Viajes',
        'oficina'=>'Oficina',
        'plano'=>'Plano',
    ),
    '#multiple' => false,
    '#default_value' => $config->get('app_theme'),
  );

thanks for your help

it´s solved, i did:

if (!empty($config->get('app_name')) && !empty($config->get('app_logo')) && (!empty($config->get('app_theme')))) {
      $desabilitar = true;
    }else {
      $desabilitar = false;
    }

and in the form insert : '#disabled' => $desabilitar,

$form['general'] = array(
      '#type' => 'details',
      '#open' => true,
      '#disabled' => $desabilitar,
      '#title' => $this->t('Configuración General'),
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM