Classes

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

File builder/form/field/constraint/FormFieldConstraintLengthMax.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: 
<?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 24
 * @since       PHPBoost 3.0 - 2009 12 19
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class FormFieldConstraintLengthMax extends AbstractFormFieldConstraint
{
    private $error_message;
    private $upper_bound;

    public function __construct($upper_bound, $js_message = '')
    {
        if (empty($js_message))
        {
            $js_message = LangLoader::get_message('form.doesnt_match_length_max', 'status-messages-common');
        }
        $this->error_message = StringVars::replace_vars($js_message, array('upper_bound' => $upper_bound));
        $this->set_validation_error_message($this->error_message);
        $this->upper_bound = $upper_bound;
    }

    public function validate(FormField $field)
    {
        $value = TextHelper::strlen($field->get_value());
        $is_required = $field->is_required();
        if (!empty($value) || $is_required)
        {
            return ($value <= $this->upper_bound);
        }
        return true;
    }

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

?>