Javascript 在Drupal 8中设置所需的选择框
我在Drupal8中创建了一个类型为List(text)的字段“Cars”,其中包含这些值Javascript 在Drupal 8中设置所需的选择框,javascript,php,jquery,html,drupal,Javascript,Php,Jquery,Html,Drupal,我在Drupal8中创建了一个类型为List(text)的字段“Cars”,其中包含这些值 0|Lambo 1|MER 2|BMW 3|Aston 4|Range Rover 5|Limo 我已经选择了“-N
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”值,因为它被设置为默认值。以下是答案:-
重要提示-我们没有在字段设置中使用Drupal8内置的required checkbox字段,因为这里我们讨论的是二级表单,默认情况下它是隐藏的,并且在某些条件下会显示
所以
在Drupal8中,可以使用JQuery方法使文本字段成为必需字段,但是“选择”字段和“上传文件”与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.'));
}
}
}
公共静态功能节点\u内容\u类型\u xyz\u表单\u验证(数组$form,表单状态接口$form\u状态){
$primary=$form_state->getValue('field_name')[0]['value'];
$reset_text_fields=[];
//我的情况就是这样**
如果($primary<4){
$form\u state->setValue($field\u secondary\u waste\u type',$reset\u text\u fields);
}
//这是验证**
否则{
$empty=[];
$value_of_secondary_waste_type=$form_state->getValue('field_secondary_waste_type')[0]['value'];
if($value\u of\u secondary\u waste\u type==null){
$form_state->setErrorByName('field_secondary_waste_type',t('secondary waste type is required');
}
如果($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\u of\u pcpg==null){
$form_state->setErrorByName('field_secondary_pcpg_facility_ty',t('secondary facility Type is required');
}
}
如果($value\u of\u secondary\u waste\u type==0){
$value_of_resistant=$form_state->getValue('field_secondary_resistant_facilit')[0]['value'];
if($value\u of_resident==null){
$form_state->setErrorByName('field_secondary_resident_facilit',t('secondary Facility Type is required');
}
}
如果($二次废物类型的值==4){
$metal=$form_state->getValue('field_secondary_metal_facility_t')[0]['value'];
如果($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'];
如果($value_of_secondary_upload_permit==$empty){
$form_state->setErrorByName('field_xyz',t('Secondary Permit PDF是必需的');
}
}
}
谢谢
库沙尔·阿格拉瓦尔
库沙拉格拉瓦尔。1996@gmail.com使用jQuery required而不是通过UI选择的默认Drupal required有什么特殊原因吗?是的,因为他的字段是次要的。默认情况下是隐藏的,在某些特定条件下它会变得可见,所以我不能让drupal UI需要它。如果您需要更多自定义解决方案,可以使用和表单完成。我猜,使用jQuery方法,“None”仍然是一个值(这不像用户没有选择任何内容),因此表单被传递,字段保存默认值。对于更详细的回答,我应该有更多的信息。是的,你和我完全在同一页上。使用jquery方法可以使文本字段成为必填字段,但需要在“表单验证”功能中验证文件和选择字段。