微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何制作下拉框和描述框验证字段?

如何解决如何制作下拉框和描述框验证字段?

我正在为我的网站使用 Dokan 插件。我需要验证我网站的下拉框和描述框。我使用了一些代码来验证产品名称、价格、商品代码,它工作正常(检查 screenshot 2)。如果您查看屏幕截图 2,您会看到当我没有在产品类别下拉列表中选择值时,它会显示一条错误消息(错误和下拉列表来自 Dokan 插件)。我需要像这样制作我的下拉菜单。我在 PHP 中通过硬代码添加了位置下拉框。我找不到验证这些下拉列表和描述框的方法(选中 screenshot 1)。

这是我用来验证其他字段的代码,位于子主题/功能.PHP

 function dokan_can_add_product_validation_customized( $errors ) {
 $postdata = wp_unslash( $_POST );



$featured_image = absint( sanitize_text_field( $postdata['feat_image_id'] ) );
  $_regular_price = absint( sanitize_text_field( $postdata['_regular_price'] ) );
  $item_code= absint( sanitize_text_field( $postdata['item_code'] ) );

  
$phone_number= absint( sanitize_text_field( $postdata['phone_number'] ) );

  if ( empty( $featured_image ) && ! in_array( 'Please upload a product cover image',$errors ) ) {
      $errors[] = 'Please upload a product cover image';
  }
  if ( empty( $_regular_price ) && ! in_array( 'Please insert product price',$errors ) ) {
      $errors[] = 'Please insert product price';
  }
  if ( empty( $item_code) && ! in_array( 'Please insert item code',$errors ) ) {
      $errors[] = 'Please insert item code';
  }

 if ( empty( $phone_number) && ! in_array( 'Please insert contact number',$errors ) ) {
      $errors[] = 'Please insert contact number';
  }


  return $errors;
}
add_filter( 'dokan_can_add_product','dokan_can_add_product_validation_customized',35,1 );
add_filter( 'dokan_can_edit_product',1 );
function dokan_new_product_popup_validation_customized( $errors,$data ) {
  if ( ! $data['_regular_price'] ) {
    return new WP_Error( 'no-price',__( 'Please insert product price','dokan-lite' ) );
  }
  if ( ! $data['phone_number'] ) {
    return new WP_Error( 'no-price',__( 'Please insert Please insert contact number','dokan-lite' ) );
  }
 if ( ! $data['item_code'] ) {
    return new WP_Error( 'no-price',__( 'Please insert item code','dokan-lite' ) );
  }
 

  if ( ! $data['feat_image_id'] ) {
    return new WP_Error( 'no-image',__( 'Please select AT LEAST ONE Picture','dokan-lite' ) );
  }
}
add_filter( 'dokan_new_product_popup_args','dokan_new_product_popup_validation_customized',2 );

解决方法

我找到了一种验证下拉列表的方法,这是代码

    if(isset($_REQUEST['plocation']) && $_REQUEST['plocation'] == '0') { 
 
$errors[] = 'Please select a location'; 
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。