Classes

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

File builder/form/field/constraint/FormFieldConstraintNotEmpty.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: 
<?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>
*/

class FormFieldConstraintNotEmpty extends AbstractFormFieldConstraint
{
    private $error_message;

    public function __construct($error_message = '')
    {
        if (empty($error_message))
        {
            $error_message = LangLoader::get_message('form.has_to_be_filled', 'status-messages-common');
        }
        $this->error_message = $error_message;
    }

    public function validate(FormField $field)
    {
        $value = $field->get_value();
        $this->set_validation_error_message(StringVars::replace_vars($this->error_message, array('name' => TextHelper::strtolower($field->get_label()))));

        if ($value instanceof FormFieldEnumOption) {
            return $value->get_raw_value() !== null && $value->get_raw_value() != '';
        }

        if ($value instanceof Date) {
            return $value->format(Date::FORMAT_ISO_DAY_MONTH_YEAR) !== null && $value->format(Date::FORMAT_ISO_DAY_MONTH_YEAR) != '';
        }

        return $value == 0 || ($value !== null && $value != '');
    }

    public function get_js_validation(FormField $field)
    {
        return 'notEmptyFormFieldValidator(' . TextHelper::to_js_string($field->get_id()) .
            ', ' . TextHelper::to_js_string(StringVars::replace_vars($this->error_message, array('name' => TextHelper::strtolower($field->get_label())))) .')';
    }
}

?>
Annonces