| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Validation\Advanced_Rules; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Classes\Tools; |
| 7 |
use JFB_Modules\Block_Parsers\Field_Data_Parser; |
| 8 |
use JFB_Modules\Validation\Advanced_Rules\Interfaces\Rule_Collect_Error_It; |
| 9 |
use JFB_Modules\Validation\Advanced_Rules\Interfaces\Rule_Field_Dependence_It; |
| 10 |
use JFB_Modules\Validation\Advanced_Rules\Traits\Rule_Collect_Error_Trait; |
| 11 |
use JFB_Modules\Validation\Advanced_Rules\Traits\Rule_Field_Dependence_Trait; |
| 12 |
|
| 13 |
// If this file is called directly, abort. |
| 14 |
if ( ! defined( 'WPINC' ) ) { |
| 15 |
die; |
| 16 |
} |
| 17 |
|
| 18 |
class Match_Regexp_Rule extends Rule implements Rule_Field_Dependence_It, Rule_Collect_Error_It { |
| 19 |
|
| 20 |
use Rule_Field_Dependence_Trait; |
| 21 |
use Rule_Collect_Error_Trait; |
| 22 |
|
| 23 |
public function get_id(): string { |
| 24 |
return 'regexp'; |
| 25 |
} |
| 26 |
|
| 27 |
public function get_label(): string { |
| 28 |
return __( 'Matches regular expression', 'jet-form-builder' ); |
| 29 |
} |
| 30 |
|
| 31 |
public function get_control_label(): string { |
| 32 |
return __( 'Regular expression', 'jet-form-builder' ); |
| 33 |
} |
| 34 |
|
| 35 |
public function validate_field( Field_Data_Parser $parser ) { |
| 36 |
$regexp = Tools::to_string( $this->get_field_value( $parser ) ); |
| 37 |
$field_value = Tools::to_string( $parser->get_value() ); |
| 38 |
|
| 39 |
if ( $regexp && |
| 40 |
preg_match( |
| 41 |
sprintf( '/%s/', $regexp ), |
| 42 |
$field_value |
| 43 |
) |
| 44 |
) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
$this->collect_error( $parser ); |
| 49 |
} |
| 50 |
} |
| 51 |
|