CoolCMS API

PickedRecordGuard
in package

FinalYes

Rejects a record id picked into a hidden field (a Picker) that does not exist, instead of letting the save fail on the foreign key.

A Picker writes the chosen id into a hidden field; the id can be stale (the record was deleted in another tab) or forged in the request. Without a check the save failed in the database (Column … cannot be null, foreign key), the admin got the generic failure message and the entity manager stayed closed for the rest of the request (admin walkthrough B3, B6). The check runs in the form onValidate, i.e. before any write.

Nette sends an error of a hidden field to the form level (HiddenField::addError()), so the message names the field by its label and the admin layout shows it as a flash (BasePresenter::flashFormOwnErrors()).

Only a submit that validates the whole form checks it (save, apply); a redraw button with a narrowed validation scope (a multiplier row, a picker refresh) must keep working with an empty or stale pick.

IMPORTANT: messages are global keys with the // prefix (see ColumnGate), in admin.general.pickedRecord.*; %s is the translated field label.

Table of Contents

Constants

MISSING_MESSAGE  : string = '//admin.general.pickedRecord.missing'
Message for a picked id that does not exist (deleted meanwhile, forged); `%s` is the field label.
REQUIRED_MESSAGE  : string = '//admin.general.pickedRecord.required'
Message for a required pick left empty; `%s` is the field label.

Methods

guard()  : BaseControl
Adds the check to an already attached control of the form.
addError()  : void

Constants

MISSING_MESSAGE

Message for a picked id that does not exist (deleted meanwhile, forged); `%s` is the field label.

public string MISSING_MESSAGE = '//admin.general.pickedRecord.missing'

REQUIRED_MESSAGE

Message for a required pick left empty; `%s` is the field label.

public string REQUIRED_MESSAGE = '//admin.general.pickedRecord.required'

Methods

guard()

Adds the check to an already attached control of the form.

public static guard(Form $form, string $name, string $label, callable(int): bool $exists[, bool $required = false ]) : BaseControl
Parameters
$form : Form
$name : string

control name in the form (the hidden field of the Picker, or a typed id)

$label : string

translation key of the field label (relative to the form translator, as in the template)

$exists : callable(int): bool

tells whether a record with the id exists

$required : bool = false

an empty pick is an error too

Return values
BaseControl

addError()

private static addError(BaseControl $control, string $message, string $label) : void
Parameters
$control : BaseControl
$message : string
$label : string
On this page

Search results