2021-07-19 16:53:23 +02:00
|
|
|
<?php
|
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (C) 2021 Easter-eggs
|
|
|
|
* https://ldapsaisie.org
|
|
|
|
*
|
|
|
|
* Author: See AUTHORS file in top-level directory.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the number of values of the attribute against min/max limits
|
|
|
|
*
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*/
|
|
|
|
class LSformRule_numberOfValues extends LSformRule {
|
|
|
|
|
2022-12-31 21:15:19 +01:00
|
|
|
/**
|
|
|
|
* Validate values one by one or all together
|
|
|
|
* @see LSformRule::validate_values()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2021-07-19 16:53:23 +02:00
|
|
|
const validate_one_by_one = False;
|
|
|
|
|
2021-07-21 16:34:46 +02:00
|
|
|
// CLI parameters autocompleters
|
2022-12-31 21:15:19 +01:00
|
|
|
protected static array $cli_params_autocompleters = array(
|
2021-07-21 16:34:46 +02:00
|
|
|
'min' => array('LScli', 'autocomplete_int'),
|
|
|
|
'max' => array('LScli', 'autocomplete_int'),
|
|
|
|
);
|
|
|
|
|
2021-07-19 16:53:23 +02:00
|
|
|
/**
|
2022-12-31 21:15:19 +01:00
|
|
|
* Validate form element value
|
2021-07-19 16:53:23 +02:00
|
|
|
*
|
2022-12-31 21:15:19 +01:00
|
|
|
* @param mixed $value The value to validate
|
2021-07-19 16:53:23 +02:00
|
|
|
* @param array $options Validation options
|
2022-12-31 21:15:19 +01:00
|
|
|
* @param LSformElement &$formElement The related LSformElement object
|
2021-07-19 16:53:23 +02:00
|
|
|
*
|
2022-12-31 21:15:19 +01:00
|
|
|
* @return boolean True if value is valid, False otherwise
|
2021-07-19 16:53:23 +02:00
|
|
|
*/
|
2022-12-31 02:31:21 +01:00
|
|
|
public static function validate($value, $options, &$formElement) {
|
2021-07-19 16:53:23 +02:00
|
|
|
$max_values = LSconfig :: get('params.max', null, 'int', $options);
|
|
|
|
$min_values = LSconfig :: get('params.min', null, 'int', $options);
|
|
|
|
if(is_null($max_values) && is_null($min_values)) {
|
|
|
|
LSerror :: addErrorCode('LSformRule_01',array('type' => 'numberOfValues', 'param' => _('max (or min)')));
|
2023-01-02 01:17:46 +01:00
|
|
|
return false;
|
2021-07-19 16:53:23 +02:00
|
|
|
}
|
|
|
|
if (!is_null($max_values) && !is_null($min_values) && $max_values < $min_values) {
|
|
|
|
LSerror :: addErrorCode('LSformRule_numberOfValues_01');
|
2023-01-02 01:17:46 +01:00
|
|
|
return false;
|
2021-07-19 16:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$count = count($value);
|
|
|
|
if (!is_null($min_values) && $count < $min_values)
|
|
|
|
throw new LSformRuleException(
|
|
|
|
getFData(
|
|
|
|
ngettext(
|
|
|
|
'At least one value is required.',
|
|
|
|
'At least %{min} values are required.',
|
|
|
|
$min_values
|
|
|
|
),
|
|
|
|
$count
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!is_null($max_values) && $count > $max_values)
|
|
|
|
throw new LSformRuleException(
|
|
|
|
getFData(
|
|
|
|
ngettext(
|
|
|
|
'Maximum one value is allowed.',
|
|
|
|
'Maximum %{max} values are allowed.',
|
|
|
|
$count
|
|
|
|
),
|
|
|
|
$max_values
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error Codes
|
|
|
|
**/
|
|
|
|
LSerror :: defineError('LSformRule_numberOfValues_01',
|
|
|
|
___("LSformRule_numberOfValues: Parameter max could not be lower than parameter min.")
|
|
|
|
);
|