Classes

File builder/form/field/constraint/FormFieldConstraintRegex.class.php

File builder/form/field/constraint/FormFieldConstraintRegex.class.php

 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 
<?php
/**
 * @package     Builder
 * @subpackage  Form\field\constraint
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Regis VIARRE <crowkait@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2016 10 28
 * @since       PHPBoost 3.0 - 2009 12 19
 * @contributor Loic ROUCHON <horn@phpboost.com>
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class FormFieldConstraintRegex extends AbstractFormFieldConstraint
{
    private $error_message;
    private $php_regex;
    private $js_regex;
    private $js_options;

    public function __construct($php_regex, $js_regex = '', $error_message = '')
    {
        if (empty($js_regex))
        {
            $js_regex = $php_regex;
        }
        $this->parse_js_regex($js_regex);
        $this->php_regex = $php_regex;

        if (empty($error_message))
        {
            $error_message = LangLoader::get_message('form.doesnt_match_regex', 'status-messages-common');
        }
        $this->set_validation_error_message($error_message);
        $this->error_message = $error_message;

    }

    private function parse_js_regex($regex)
    {
        $delimiter = $regex[0];
        $end_delimiter_position = TextHelper::strrpos($regex, $delimiter);
        $js_regex = TextHelper::substr($regex, 1, $end_delimiter_position - 1);

        $js_options_chars = str_split(TextHelper::substr($regex, $end_delimiter_position + 1));
        $js_options = '';
        foreach ($js_options_chars as $option)
        {
            if (in_array($option, array('i', 'm', 'g')))
                $js_options .= $option;
        }

        $this->js_regex = str_replace('\.', '\\\.', $js_regex);
        $this->js_regex = '\'' . str_replace('\'', '\\\'', $this->js_regex) . '\'';
        $this->js_options = '\'' . str_replace('\'', '\\\'', $js_options) . '\'';
    }

    public function validate(FormField $field)
    {
        $value = $field->get_value();
        $is_required = $field->is_required();

        if ($value instanceof Date) {
            $value = $value->format(Date::FORMAT_ISO_DAY_MONTH_YEAR);
        }

        if (!empty($value) || $is_required)
        {
            return preg_match($this->php_regex, $value) > 0;
        }
        return true;
    }

    public function get_js_validation(FormField $field)
    {
        return 'regexFormFieldValidator(' . TextHelper::to_js_string($field->get_id()) .
            ', ' . $this->js_regex . ', ' . $this->js_options . ', ' . TextHelper::to_js_string($this->error_message) . ')';
    }
}

?>