*/ class LSformRule_numberOfValues extends LSformRule { /** * Validate values one by one or all together * @see LSformRule::validate_values() * @var boolean */ const validate_one_by_one = False; // CLI parameters autocompleters protected static $cli_params_autocompleters = array( 'min' => array('LScli', 'autocomplete_int'), 'max' => array('LScli', 'autocomplete_int'), ); /** * Validate form element value * * @param mixed $value The value to validate * @param array $options Validation options * @param LSformElement &$formElement The related LSformElement object * * @return boolean True if value is valid, False otherwise */ public static function validate($value, $options, &$formElement) { $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)'))); return false; } if (!is_null($max_values) && !is_null($min_values) && $max_values < $min_values) { LSerror :: addErrorCode('LSformRule_numberOfValues_01'); return false; } $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.") );