设置未知 属性:yii\validators\NumberValidator::0
Setting unknown property: yii\validators\NumberValidator::0
我尝试调用一个设置表单,它显示用于将数据保存到价格数据库中的输入表单。
我的模型在渲染过程中抛出上述异常:
未知 属性 – yii\base\UnknownPropertyException
设置未知 属性:yii\validators\NumberValidator::0
_price-item 行错误:
$form->field($model, "[{$i}]credits")->textInput(['maxlength' => 8])
型号:
<?php
namespace app\models;
use Yii;
/**
* @package app\models
*
* @property integer $id
* @property integer $credits
* @property integer $price
* @property integer $reduced_price
* @property integer $discount
* @property string $start
* @property string $end
* @property integer $active
*/
class Price extends \app\base\ActiveRecord
{
public function rules()
{
return [
[['credits'], 'integer', 'required'],
[['price'], 'integer','integerOnly' => false,'required', 'min' => 0, 'max' => 10000],
[['reduced_price','discount'],'integer','integerOnly' => false,'min' => 0, 'max' => 10000],
[['start','end'],'format' => 'php:Y-m-d H:i:s'],
[['active'], 'integer'],
[['active'], 'in', 'range' => array_keys(self::$_CONDITIONS)],
];
}
}
小部件:
<?php DynamicFormWidget::begin([
'widgetContainer' => 'wrapper-prices',
'widgetBody' => '.container-items',
'widgetItem' => '.item',
'limit' => 30,
'min' => 1,
'insertButton' => '.add-item',
'deleteButton' => '.remove-item',
'model' => count($prices) ? $prices[0] : new \app\models\Price(),
'template' => $this->render('_price-item', [
'i' => 0,
'form' => $form,
'model' => count($prices) ? $prices[0] : new \app\models\Price(),
]),
'formId' => 'dynamic-form',
'formFields' => [
'credits',
'price',
'reduced_price',
'discount',
'start',
'end',
'active',
],
]); ?>
mysql:
CREATE TABLE `price` (
`id` int(11) NOT NULL,
`credits` int(11) NOT NULL,
`price` float NOT NULL,
`reduced_price` float DEFAULT NULL,
`discount` float DEFAULT NULL,
`start` datetime DEFAULT NULL,
`end` datetime DEFAULT NULL,
`active` smallint(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
谁能告诉我,怎么了?
我的头都快烧起来了
这是你的前 2 条规则
[['credits'], 'integer', 'required'],
[['price'], 'integer','integerOnly' => false,'required', 'min' => 0, 'max' => 10000],
您在一个错误的规则中设置了 2 个核心验证器 integer
、required
。 integer
例如验证器采用 max
或 min
参数,并且也作为关联数组 'min'=>10
,并分配 属性 值,如 $obj->min=10
,并且您的代码将强制整数验证器将 'required'
解释为 0=>'required'
,这清楚地解释了上述错误。
Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: yii\validators\NumberValidator::0
将规则方法更改为
public function rules()
{
return [
[['credits','price'], 'required'],
[['price'], 'integer','integerOnly' => false, 'min' => 0, 'max' => 10000],
[['reduced_price','discount'],'integer','integerOnly' => false,'min' => 0, 'max' => 10000],
[['start','end'],'datetime','format' => 'php:Y-m-d H:i:s'],
[['active','credits'], 'integer'],
[['active'], 'in', 'range' => array_keys(self::$_CONDITIONS)],
];
}
更新
你的第 4 条规则也会抛出错误
[['start','end'],'datetime','format' => 'php:Y-m-d H:i:s'],
我也更新了上面的代码块。
正确的工作规则是:
感谢穆罕默德
public function rules()
{
return [
[['credits','price'], 'required'],
[['price','reduced_price','discount'],'integer','integerOnly' => false,'min' => 0, 'max' => 10000],
[['start','end'],'datetime','format' => 'php:Y-m-d'],
[['status','credits'], 'integer'],
[['status'], 'in', 'range' => array_keys(self::$_CONDITIONS)],
];
}
我尝试调用一个设置表单,它显示用于将数据保存到价格数据库中的输入表单。
我的模型在渲染过程中抛出上述异常:
未知 属性 – yii\base\UnknownPropertyException
设置未知 属性:yii\validators\NumberValidator::0
_price-item 行错误:
$form->field($model, "[{$i}]credits")->textInput(['maxlength' => 8])
型号:
<?php
namespace app\models;
use Yii;
/**
* @package app\models
*
* @property integer $id
* @property integer $credits
* @property integer $price
* @property integer $reduced_price
* @property integer $discount
* @property string $start
* @property string $end
* @property integer $active
*/
class Price extends \app\base\ActiveRecord
{
public function rules()
{
return [
[['credits'], 'integer', 'required'],
[['price'], 'integer','integerOnly' => false,'required', 'min' => 0, 'max' => 10000],
[['reduced_price','discount'],'integer','integerOnly' => false,'min' => 0, 'max' => 10000],
[['start','end'],'format' => 'php:Y-m-d H:i:s'],
[['active'], 'integer'],
[['active'], 'in', 'range' => array_keys(self::$_CONDITIONS)],
];
}
}
小部件:
<?php DynamicFormWidget::begin([
'widgetContainer' => 'wrapper-prices',
'widgetBody' => '.container-items',
'widgetItem' => '.item',
'limit' => 30,
'min' => 1,
'insertButton' => '.add-item',
'deleteButton' => '.remove-item',
'model' => count($prices) ? $prices[0] : new \app\models\Price(),
'template' => $this->render('_price-item', [
'i' => 0,
'form' => $form,
'model' => count($prices) ? $prices[0] : new \app\models\Price(),
]),
'formId' => 'dynamic-form',
'formFields' => [
'credits',
'price',
'reduced_price',
'discount',
'start',
'end',
'active',
],
]); ?>
mysql:
CREATE TABLE `price` (
`id` int(11) NOT NULL,
`credits` int(11) NOT NULL,
`price` float NOT NULL,
`reduced_price` float DEFAULT NULL,
`discount` float DEFAULT NULL,
`start` datetime DEFAULT NULL,
`end` datetime DEFAULT NULL,
`active` smallint(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
谁能告诉我,怎么了? 我的头都快烧起来了
这是你的前 2 条规则
[['credits'], 'integer', 'required'],
[['price'], 'integer','integerOnly' => false,'required', 'min' => 0, 'max' => 10000],
您在一个错误的规则中设置了 2 个核心验证器 integer
、required
。 integer
例如验证器采用 max
或 min
参数,并且也作为关联数组 'min'=>10
,并分配 属性 值,如 $obj->min=10
,并且您的代码将强制整数验证器将 'required'
解释为 0=>'required'
,这清楚地解释了上述错误。
Unknown Property – yii\base\UnknownPropertyException Setting unknown property: yii\validators\NumberValidator::0
将规则方法更改为
public function rules()
{
return [
[['credits','price'], 'required'],
[['price'], 'integer','integerOnly' => false, 'min' => 0, 'max' => 10000],
[['reduced_price','discount'],'integer','integerOnly' => false,'min' => 0, 'max' => 10000],
[['start','end'],'datetime','format' => 'php:Y-m-d H:i:s'],
[['active','credits'], 'integer'],
[['active'], 'in', 'range' => array_keys(self::$_CONDITIONS)],
];
}
更新
你的第 4 条规则也会抛出错误
[['start','end'],'datetime','format' => 'php:Y-m-d H:i:s'],
我也更新了上面的代码块。
正确的工作规则是:
感谢穆罕默德
public function rules()
{
return [
[['credits','price'], 'required'],
[['price','reduced_price','discount'],'integer','integerOnly' => false,'min' => 0, 'max' => 10000],
[['start','end'],'datetime','format' => 'php:Y-m-d'],
[['status','credits'], 'integer'],
[['status'], 'in', 'range' => array_keys(self::$_CONDITIONS)],
];
}