Classes

File builder/form/constraint/FormConstraintFieldsDifferenceInferior.class.php

File builder/form/constraint/FormConstraintFieldsDifferenceInferior.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: 84: 
<?php
/**
 * @package     Builder
 * @subpackage  Form\constraint
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Julien BRISWALTER <j1.seth@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2016 02 11
 * @since       PHPBoost 4.1 - 2015 11 09
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/

class FormConstraintFieldsDifferenceInferior implements FormConstraint
{
    private $js_message;
    /**
     * @var FormField
     */
    private $first_field;
    /**
     * @var FormField
     */
    private $second_field;

    public function __construct(FormField $first_field, FormField $second_field, $js_message = '')
    {
        if (!empty($js_message))
        {
            $this->js_message = $js_message;
        }
        else
        {
            $this->js_message = LangLoader::get_message('form.first_field_must_be_inferior_to_second_field', 'status-messages-common');
        }

        $this->first_field = $first_field;
        $this->second_field = $second_field;

        $this->second_field->add_form_constraint($this);
    }

    public function validate()
    {
        $first_value = $this->first_field->get_value();
        $second_value = $this->second_field->get_value();
        if ($first_value !== null && $second_value !== null)
        {
            if ($first_value instanceof Date && $second_value instanceof Date)
            {
                if ($second_value->get_timestamp() < $first_value->get_timestamp())
                {
                    return true;
                }
            }
            else
            {
                if ($second_value < $first_value)
                {
                    return true;
                }
            }
        }
        return false;
    }

    public function get_js_validation()
    {
        return "";
    }

    public function get_validation_error_message()
    {
        return StringVars::replace_vars($this->js_message,
            array('field1' => $this->first_field->get_label(), 'field2' => $this->second_field->get_label()));
    }

    public function get_related_fields()
    {
        return array($this->first_field, $this->second_field);
    }
}

?>