0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

mw wp form自作バリデーションの追加

Last updated at Posted at 2017-04-17

自作バリデーション追加

プラグイン自体を触る良くないけど、
自作バリデーションの作り方なかなか見つからないので
メモとして残しとく。

プラグインフォルダに/classes/validation-rules/*.php中に追加すればよい

class.myrule.php
<?php

class MW_WP_Form_Validation_Rule_MyRule extends MW_WP_Form_Abstract_Validation_Rule {
  /**
   * バリデーションルール名を指定
   * @var string
   */
  protected $name = 'myrule';
  /**
   * バリデーションチェック
   *
   * @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 ) ) {
        $defaults = array(
          'message' => __( 'Please enter with a half-width alphabetic character.', 'mw-wp-form' )
          );
        $options = array_merge( $defaults, $options ); // 重複の削除
        return $options['message']; // エラーメッセージの返す
    }
  }

  public function admin( $key, $value ) {
    return false;
  }

}
0
1
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?