お世話になります。
現在、お問い合わせフォームを「mw wp form」で作成しています。
その中で、複数の「テキスト&テキスト」「テキスト&チェックボックス」「チェックボックス&チェックボックス」でいづれか必須という条件のバリデーションを作りたいと考えています。
以下のサイトを参考に「テキスト&テキスト」「テキスト&チェックボックス」は機能しました。
https://xakuro.com/blog/wordpress/1991/
しかし「チェックボックス&チェックボックス」が機能しませんでした。
コードをどのように修正すれば可能でしょうか?
ご教示のほど、よろしくお願いします。
if ( class_exists( 'MW_WP_Form_Abstract_Validation_Rule' ) ) { class MW_WP_Form_Validation_Rule_AnyRequired extends MW_WP_Form_Abstract_Validation_Rule { protected $name = 'anyrequired'; public function rule( $key, array $options = array() ) { $value = $this->Data->get( $key ); if ( ! is_null( $value ) ) { $targets = array_map( 'trim', explode( ',', $options['target'] ) ); $targets[] = $key; $count = 0; foreach ( $targets as $target ) { $target_value = $this->Data->get( $target ); if ( ! empty( $target_value ) || ! MWF_Functions::is_empty( $target_value ) ) { $count++; } } if ( $count == 0 ) { $defaults = array( 'target' => null, 'message' => sprintf( '%s のいずれも未入力です。', implode( ', ', $targets ) ) ); $options = array_merge( $defaults, $options ); return $options['message']; } } } public function admin( $key, $value ) { $target = ''; if ( is_array( $value[$this->getName()] ) && isset( $value[$this->getName()]['target'] ) ) { $target = $value[$this->getName()]['target']; } ?> <table> <tr> <td>いずれか必須項目</td> <td> <input type="text" value="<?php echo esc_attr( $target ); ?>" name="<?php echo MWF_Config::NAME; ?>[validation][<?php echo $key; ?>][<?php echo esc_attr( $this->getName() ); ?>][target]" /> <span class="mwf_note">(カンマ区切り)</span> </td> </tr> </table> <?php } } function mwform_validation_rule_anyrequired( $validation_rules ) { $instance = new MW_WP_Form_Validation_Rule_AnyRequired(); $validation_rules[$instance->getName()] = $instance; return $validation_rules; } add_filter( 'mwform_validation_rules', 'mwform_validation_rule_anyrequired' ); }
あなたの回答
tips
プレビュー