2014-06-10 16:50:44 +02:00
|
|
|
<?php
|
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (C) 2007 Easter-eggs
|
2021-04-13 18:04:19 +02:00
|
|
|
* https://ldapsaisie.org
|
2014-06-10 16:50:44 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2022-02-17 10:38:41 +01:00
|
|
|
* LSform check rule from a list of possible values
|
2014-06-10 16:50:44 +02:00
|
|
|
*
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*/
|
|
|
|
class LSformRule_inarray extends LSformRule {
|
2020-02-18 12:31:13 +01:00
|
|
|
|
2021-07-21 16:34:46 +02:00
|
|
|
// CLI parameters autocompleters
|
2023-01-03 12:51:45 +01:00
|
|
|
protected static $cli_params_autocompleters = array(
|
2021-07-21 16:34:46 +02:00
|
|
|
'possible_values' => null,
|
2022-02-17 10:38:41 +01:00
|
|
|
'reverse' => array('LScli', 'autocomplete_bool'),
|
2021-07-21 16:34:46 +02:00
|
|
|
);
|
|
|
|
|
2014-06-10 16:50:44 +02:00
|
|
|
/**
|
2022-02-17 10:38:41 +01:00
|
|
|
* Check the value in configured list
|
2014-06-10 16:50:44 +02:00
|
|
|
*
|
2022-12-31 21:15:19 +01:00
|
|
|
* @param string $value The value to check
|
2022-02-17 10:38:41 +01:00
|
|
|
* @param array $options Check options:
|
|
|
|
* - possible_values: the list of valid values (required)
|
|
|
|
* - reverse: if true, reverse the logic of the check
|
|
|
|
* (list values are prohibited, optional, default: False)
|
2022-12-31 21:15:19 +01:00
|
|
|
* @param LSformElement &$formElement The related LSformElement object
|
2014-06-10 16:50:44 +02:00
|
|
|
*
|
2022-02-17 10:38:41 +01:00
|
|
|
* @return boolean true if the value is valid, false otherwise
|
2020-02-18 12:31:13 +01:00
|
|
|
*/
|
2022-12-31 02:31:21 +01:00
|
|
|
public static function validate($value, $options, &$formElement) {
|
2020-02-18 12:31:13 +01:00
|
|
|
$possible_values = LSconfig :: get('params.possible_values', null, null, $options);
|
2022-02-17 10:38:41 +01:00
|
|
|
$reverse = LSconfig :: get('params.reverse', false, 'bool', $options);
|
2020-02-18 12:31:13 +01:00
|
|
|
if (!is_array($possible_values)) {
|
2014-06-10 16:50:44 +02:00
|
|
|
LSerror :: addErrorCode('LSformRule_inarray_01');
|
2023-01-02 01:17:46 +01:00
|
|
|
return false;
|
2014-06-10 16:50:44 +02:00
|
|
|
}
|
2020-02-18 12:31:13 +01:00
|
|
|
if (!in_array($value, $possible_values))
|
2022-02-17 10:38:41 +01:00
|
|
|
return $reverse;
|
|
|
|
return !$reverse;
|
2014-06-10 16:50:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Error Codes
|
|
|
|
*/
|
|
|
|
LSerror :: defineError('LSformRule_inarray_01',
|
2020-08-25 17:31:50 +02:00
|
|
|
___("LSformRule_inarray : Possible values has not been configured to validate data.")
|
2014-06-10 16:50:44 +02:00
|
|
|
);
|