【问题标题】:Make Select box required in Drupal 8在 Drupal 8 中使选择框成为必需
【发布时间】:2019-01-20 11:32:15
【问题描述】:

我在 Drupal 8 中创建了一个 List(text) 类型的字段 "Cars",其中包含这些值

                        0|Lambo
                        1|MER
                        2|BMW
                        3|Aston
                        4|Range Rover
                        5|Limo 

我已选择 "- None -" 作为默认值,并且我没有通过单击 drupal 编辑设置中的复选框使该字段成为必需,我想通过 JQuery 使该字段成为必需。

我已经尝试过 Jquery 代码:-

        jQuery('#edit-field-secondary-waste-type').prop('required', true);

预期的结果是该字段是必需的,但实际上它不起作用,并且表单正在以 "None" 值保存,因为它被设置为默认值。

【问题讨论】:

  • 需要使用 jQuery 而不是通过 UI 选择的默认 Drupal 的任何特殊原因?
  • 是的,因为他的领域是次要的。默认情况下是隐藏的,在某些特定条件下它会变得可见,所以我无法使用 drupal UI 使其成为必需。
  • 好吧,这听起来像是可以用conditional fields 处理的。如果您需要更多自定义解决方案,可以使用hook_form_alter 和表单states 完成。我猜想使用 jQuery 方法“无”仍然是一个值(它不像用户没有选择任何东西)并且因为它传递了表单并且字段保存了默认值。有关更详细的答案,我应该有更多信息。
  • 是的,你和我完全一致。文本字段可以用jquery方法设置,但是文件和选择字段需要在“表单验证”功能中进行验证。

标签: javascript php jquery html drupal


【解决方案1】:

这是答案:-

重要提示-我们没有在字段设置中使用 Drupal8 内置的必填复选框字段,因为这里我们讨论的是辅助表单,它默认隐藏并在某些情况下显示。

所以,

在 Drupal8 中,可以使用 JQuery 方法使 Text 字段成为必需,但“选择”字段和“上传文件”与 jQuery 不兼容。

这是解决方案之一:-

您可以让他们在您的“验证”功能中检查验证

这是我的代码:-

在要与您的字段名称一起放置的代码字段名称中,例如,我使用了随机值

public static function node_content_type_xyz_form_validate(array $form, FormStateInterface $form_state){



   $primary =  $form_state->getValue('field_name')[0]['value'];

   $reset_text_fields=[];

//in my case this was the condition**

   if($primary < 4){
      $form_state->setValue('field_secondary_waste_type',$reset_text_fields);
            }



    // here is the validation**



      else{

        $empty=[];
        $value_of_secondary_waste_type = $form_state->getValue('field_secondary_waste_type')[0]['value'];

        if($value_of_secondary_waste_type == null){

          $form_state->setErrorByName('field_secondary_waste_type', t('Secondary Waste Type is required.'));

        }


        if(($value_of_secondary_waste_type == 1) || ($value_of_secondary_waste_type == 2) || ($value_of_secondary_waste_type == 3 ) || ($value_of_secondary_waste_type == 5)){

          $value_of_pcpg = $form_state->getValue('field_secondary_pcpg_facility_ty')[0]['value'];

            if($value_of_pcpg == null) {
               $form_state->setErrorByName('field_secondary_pcpg_facility_ty', t('Secondary Facility Type is required.'));
            }



        }

        if($value_of_secondary_waste_type == 0){

            $value_of_residual = $form_state->getValue('field_secondary_residual_facilit')[0]['value'];

            if($value_of_residual == null ){
               $form_state->setErrorByName('field_secondary_residual_facilit', t('Secondary Facility Type is required.'));
            }

        }

        if($value_of_secondary_waste_type == 4){
          $metal = $form_state->getValue('field_secondary_metal_facility_t')[0]['value'];

          if($metal == null){
            $form_state->setErrorByName('field_secondary_metal_facility_t', t('Secondary Facility Type is required.'));
          }


        }


        $variable= $form_state->getValue('field_secondary_permit_pdf')[0]['fids'];
        if($value_of_secondary_upload_permit == $empty){
          $form_state->setErrorByName('field_xyz', t('Secondary Permit PDF is required.'));
        }

   }



  } 

谢谢 库沙尔·阿格拉瓦尔 kushalagrawal.1996@gmail.com

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-10
    • 2014-09-14
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多