<commit_before><?php
namespace wcf\system\form\builder\field;
use wcf\system\form\builder\field\validation\FormFieldValidationError;

/**
 * Implementation of a form field for selecting multiple values.
 * 
 * @author	<NAME>
 * @copyright	2001-2018 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\System\Form\Builder\Field
 * @since	3.2
 */
class MultipleSelectionFormField extends AbstractFormField implements INullableFormField, ISelectionFormField {
	use TNullableFormField;
	use TSelectionFormField;
	
	/**
	 * @inheritDoc
	 */
	protected $templateName = '__multipleSelectionFormField';
	
	/**
	 * @inheritDoc
	 */
	public function readValue() {
		if ($this->getDocument()->hasRequestData($this->getPrefixedId())) {
			$value = $this->getDocument()->getRequestData($this->getPrefixedId());
			
			if (is_array($value)) {
				$this->__value = $value;
			}
			else if (!$this->isNullable()) {
				$this->__value = [];
			}
		}
		
		return $this;
	}
	
	/**
	 * @inheritDoc
	 */
	public function validate() {
		if ($this->getValue() !== null && !empty(array_diff($this->getValue(), array_keys($this->getOptions())))) {
			$this->addValidationError(new FormFieldValidationError(
				'invalidValue',
				'wcf.global.form.error.noValidSelection'
			));
		}
		
		parent::validate();
	}
	
	/**
	 * @inheritDoc
	 */
	public function value($value) {
		// ignore `null` as value which can be passed either for nullable
		// fields or as value if no options are available<commit_msg>Fix validation of required multiple selection form fields<commit_after><?php
namespace wcf\system\form\builder\field;
use wcf\system\form\builder\field\validation\FormFieldValidationError;

/**
 * Implementation of a form field for selecting multiple values.
 * 
 * @author	<NAME>
 * @copyright	2001-2018 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\System\Form\Builder\Field
 * @since	3.2
 */
class MultipleSelectionFormField extends AbstractFormField implements INullableFormField, ISelectionFormField {
	use TNullableFormField;
	use TSelectionFormField;
	
	/**
	 * @inheritDoc
	 */
	protected $templateName = '__multipleSelectionFormField';
	
	/**
	 * @inheritDoc
	 */
	public function readValue() {
		if ($this->getDocument()->hasRequestData($this->getPrefixedId())) {
			$value = $this->getDocument()->getRequestData($this->getPrefixedId());
			
			if (is_array($value)) {
				$this->__value = $value;
			}
			else if (!$this->isNullable()) {
				$this->__value = [];
			}
		}
		
		return $this;
	}
	
	/**
	 * @inheritDoc
	 */
	public function validate() {
		$value = $this->getValue();
		
		if (($value === null || empty($value)) && $this->isRequired()) {
			$this->addValidationError(new FormFieldValidationError('empty'));
		}
		else if ($value !== null && !empty(array_diff($this->getValue(), array_keys($this->getOptions())))) {
			$this->addValidationError(new FormFieldValidationError(
				'invalidValue',
				'wcf.global.form.error.noValidSelection'
			));
		}
		
		parent::validate();
	}
	
	/**
	 * @inheritDoc
	 */
	public function value($value) {
		// ignore `null` as value which can be passed either for nullable
		// fields or as value if no options are available
{'fraction_non_alphanumeric': 0.14845854534570488, 'fraction_numerical': 0.00598623166716552, 'mean_word_length': 4.0636363636363635, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3114556', 'n_tokens_mistral': 1149, 'n_tokens_neox': 998, 'n_words': 220}