Skip to content

Instantly share code, notes, and snippets.

@k4zuki02h4t4
Last active May 10, 2024 16:35
Show Gist options
  • Save k4zuki02h4t4/07b2d57566c0b092bfe8216e8186e0a0 to your computer and use it in GitHub Desktop.
Save k4zuki02h4t4/07b2d57566c0b092bfe8216e8186e0a0 to your computer and use it in GitHub Desktop.
MW WP Form に chatwork ID 用のバリデーションルールを追加する
<?php
/**
* Validation rule chatwork.
*
* @param array $validation_rules
*/
function mwform_validation_rule_chatwork( $validation_rules ) {
if ( ! class_exists("MW_Validation_Rule_Chatwork") ) {
class MW_Validation_Rule_Chatwork extends MW_WP_Form_Abstract_Validation_Rule {
/**
* バリデーションルール名を指定
*
* @var string
*/
protected $name = 'chatwork';
/**
* バリデーションチェック
*
* @param string $key name属性
* @param array $option
*
* @return string エラーメッセージ
*/
public function rule( $key, array $options = array() ) {
$value = $this->Data->get( $key );
if ( ! MWF_Functions::is_empty( $value ) ) {
if ( ! preg_match( '/^[a-zA-Z0-9\-_]+$/', $value ) ) {
$defaults = array(
'message' => __( 'Please enter with alphanumeric characters (A-Z, 0-9) or a hyphen ("-") & underscore ("_").', 'mw-wp-form' ),
);
$options = array_merge( $defaults, $options );
return $options['message'];
}
}
}
/**
* 設定パネルに追加
*
* @param int $key バリデーションルールセットの識別番号
* @param array $value バリデーションルールセットの内容
*/
public function admin( $key, $value ) {
?>
<label>
<input type="checkbox" <?php checked( $value[ $this->getName() ], 1 ); ?> name="<?php echo MWF_Config::NAME; ?>[validation][<?php echo $key; ?>][<?php echo esc_attr( $this->getName() ); ?>]" value="1" />
<?php esc_html_e( 'chatwork ID', 'mw-wp-form' ); ?>
</label>
<?php
}
}
}
$instance = new MW_Validation_Rule_Chatwork();
$validation_rules[$instance->getName()] = $instance;
return $validation_rules;
}
add_filter( 'mwform_validation_rules', 'mwform_validation_rule_chatwork' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment