简体   繁体   English

在 Drupal 8 中需要选择框

[英]Make Select box required in Drupal 8

I have created a field "Cars" of type List(text) in Drupal 8 which has these values我在 Drupal 8 中创建了一个 List(text) 类型的字段“Cars” ,它具有这些值

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

I have selected "- None -" as default value and i have not made this field required by clicking checkbox in drupal edit settings, i want to make this field required through JQuery.我选择了“- None -”作为默认值,并且我没有通过单击 drupal 编辑设置中的复选框来要求此字段,我想通过 JQuery 使该字段成为必需。

I have tried Jquery code:-我试过 Jquery 代码:-

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

Expected result is that the field gets required but actually it is not working and form is saving with "None" value as it was set as default value.预期结果是该字段是必需的,但实际上它不起作用,并且表单以“无”值保存,因为它被设置为默认值。

Here is the answer :-这是答案:-

Important Note- We are not using Drupal8's in built required checkbox field in field settings because here we are discussing about secondary form, which is hidden by default and shows up under some condition.重要说明 -我们没有在字段设置中的内置必填复选框字段中使用 Drupal8,因为这里我们讨论的是辅助表单,默认情况下隐藏并在某些条件下显示。

So,所以,

In Drupal8, Text fields can be made required using JQuery approach, but the "select" fields and "upload file" do not get along with jQuery.在 Drupal8 中,可以使用 JQuery 方法使文本字段成为必需的,但“选择”字段和“上传文件”与 jQuery 不兼容。

Here is one of the solution :-这是解决方案之一:-

You can make them check for validation in your "Validate" function您可以让他们在“验证”功能中检查验证

Here is my Code :-这是我的代码:-

in code field names to be placed with your field names i have used random values for example在要与您的字段名称一起放置的代码字段名称中,我使用了随机值,例如

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.'));
        }

   }



  } 

Thanks Kushal Agrawal kushalagrawal.1996@gmail.com感谢 Kushal Agrawal kushalagrawal.1996@gmail.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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