ChoiceGuard
in package
Rejects a submitted select value that is not among the options, instead of dropping it silently.
Nette ChoiceControl::getValue() returns null for an unknown submitted key and an optional select
skips all its rules when it is "not filled" (Rules::validate()), so a deleted or
forged id (producerId=999999) passed validation as "no choice" and the save cleared the stored
relation (a saved product whose producer is gone). A rule on the control would never run for that value,
hence the check hooks into the form onValidate and reads the raw submitted key.
An empty submit (the prompt) stays allowed; a required select handles it with its own message. A disabled control is not submitted, so there is nothing to check.
A multi select or checkbox list (MultiChoiceControl) drops unknown keys the same way: a forged role id saved the user with no roles at all (admin walkthrough A11). One unknown key rejects the whole submit, the known ones are not saved without it.
IMPORTANT: the message is a global key with the // prefix (see ColumnGate), in
admin.general.choiceGuard.*.
Table of Contents
Constants
- UNKNOWN_MESSAGE : string = '//admin.general.choiceGuard.unknown'
Methods
- isUnknown() : bool
- Tells whether the submitted raw value is filled in and not among the items (any of them for a multi choice).
- rejectUnknown() : ChoiceControl|MultiChoiceControl
- Adds the check to an already attached select (its form must exist).
Constants
UNKNOWN_MESSAGE
public
string
UNKNOWN_MESSAGE
= '//admin.general.choiceGuard.unknown'
Methods
isUnknown()
Tells whether the submitted raw value is filled in and not among the items (any of them for a multi choice).
public
static isUnknown(ChoiceControl|MultiChoiceControl $control) : bool
Parameters
- $control : ChoiceControl|MultiChoiceControl
Return values
boolrejectUnknown()
Adds the check to an already attached select (its form must exist).
public
static rejectUnknown(ChoiceControl|MultiChoiceControl $control[, string $message = self::UNKNOWN_MESSAGE ]) : ChoiceControl|MultiChoiceControl
Parameters
- $control : ChoiceControl|MultiChoiceControl
- $message : string = self::UNKNOWN_MESSAGE