yiisoft/yii2 · error · InvalidConfigException
The "formName()" method should be explicitly defined for ano
Error message
The "formName()" method should be explicitly defined for anonymous models
What it means
Model::formName() returns the class short name and is the prefix load(), ActiveForm and Html::active* helpers use for input names (POST[formName][attribute]). PHP anonymous classes have no usable short name, so when ReflectionClass::isAnonymous() is true (PHP >= 7) formName() throws InvalidConfigException unless the anonymous class overrides it. The throw usually surfaces indirectly: $model->load($post) calls formName() to match POST keys.
Source
Thrown at framework/base/Model.php:271
* an empty string, then the input name would be "b".
*
* The purpose of the above naming schema is that for forms which contain multiple different models,
* the attributes of each model are grouped in sub-arrays of the POST-data and it is easier to
* differentiate between them.
*
* By default, this method returns the model class name (without the namespace part)
* as the form name. You may override it when the model is used in different forms.
*
* @return string the form name of this model class.
* @see load()
* @throws InvalidConfigException when form is defined with anonymous class and `formName()` method is
* not overridden.
*/
public function formName()
{
$reflector = new ReflectionClass($this);
if (PHP_VERSION_ID >= 70000 && $reflector->isAnonymous()) {
throw new InvalidConfigException('The "formName()" method should be explicitly defined for anonymous models');
}
return $reflector->getShortName();
}
/**
* Returns the list of attribute names.
*
* By default, this method returns all public non-static properties of the class.
* You may override this method to change the default behavior.
*
* @return string[] list of attribute names.
*/
public function attributes()
{
$class = new ReflectionClass($this);
$names = [];
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
if (!$property->isStatic()) {View on GitHub (pinned to 66f00d18a2)
Solutions
- Override formName() inside the anonymous class: public function formName() { return 'QuickForm'; }
- Or skip form-based loading: set attributes directly with setAttributes($data, false) and then call validate()
- Or promote the class to a named class file - named classes never hit this guard
Example fix
// before
$model = new class extends \yii\base\Model {
public $email;
public function rules() { return [[['email'], 'email']]; }
};
$model->load(Yii::$app->request->post()); // throws
// after
$model = new class extends \yii\base\Model {
public $email;
public function rules() { return [[['email'], 'email']]; }
public function formName() { return 'QuickForm'; }
};
$model->load(Yii::$app->request->post()); // matches POST['QuickForm']['email'] Defensive patterns
Strategy: validation
Validate before calling
$reflector = new \ReflectionClass($model);
if ($reflector->isAnonymous()
&& (new \ReflectionMethod($model, 'formName'))->getDeclaringClass()->getName() === \yii\base\Model::class) {
// formName() will throw; override it or bypass load()
$model->setAttributes($data, false);
$ok = $model->validate();
} else {
$ok = $model->load($data);
} Try / catch
try {
$model->load(Yii::$app->request->post());
} catch (\yii\base\InvalidConfigException $e) {
// anonymous model without formName() override
Yii::error($e->getMessage());
$model->setAttributes(Yii::$app->request->post(), false);
} Prevention
- Always override formName() in anonymous models used with forms
- For pure server-side validation use setAttributes() plus validate() instead of load()
- Review 'new class extends' near Model in code review as a lint habit
When it happens
Trigger: Creating an inline model `new class extends \yii\base\Model { ... }` and calling load(), rendering it with ActiveForm/ActiveField, or using Html::activeTextInput() on it; calling formName() directly for cache keys or logging on an anonymous model instance.
Common situations: One-off validation models for API payloads written inline to avoid creating a file; anonymous subclasses of Model used in tests and fixtures.
Related errors
- Unknown scenario: $scenario
- Invalid validation rule: a rule must specify both attribute
- There is no type mapping for '{attribute}'.
- Attribute name must contain word characters only.
- {class}::formName() cannot be empty for tabular inputs.
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/feffdc6888e65073.
Report an issue: GitHub.