Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

yii2 - Setting unknown property: yiivalidatorsNumberValidator::0

I try to call a setting form, which shows input forms for saving data into price database.

My model throws the above Exception during rendering:

Unknown Property – yiiaseUnknownPropertyException
Setting unknown property: yiivalidatorsNumberValidator::0

error in line of _price-item:

$form->field($model, "[{$i}]credits")->textInput(['maxlength' => 8])

Model:

<?php

namespace appmodels;


use Yii;

/**
 * @package appmodels
 *
 * @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 appaseActiveRecord
{
    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)],
                ];
        }
}

Widget:

<?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 appmodelsPrice(),
            'template' => $this->render('_price-item', [
                'i' => 0,
                'form' => $form,
                'model' => count($prices) ? $prices[0] : new appmodelsPrice(),
            ]),
            '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;

Can anybody tell me, what is wrong ? my head almost burns

question from:https://stackoverflow.com/questions/66068765/setting-unknown-property-yii-validators-numbervalidator0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's your first 2 rules

[['credits'], 'integer', 'required'],
[['price'], 'integer','integerOnly' => false,'required', 'min' => 0, 'max' => 10000],

You are setting 2 core validators integer , required in one rule which is wrong. The integer for example validator takes max or min parameters and that too as an associative array 'min'=>10,and assigns the property values like $obj->min=10, and your code would force the integer validator to interpret 'required' as 0=>'required' , which clearly explains the error above.

Unknown Property – yiiaseUnknownPropertyException Setting unknown property: yiivalidatorsNumberValidator::0

Chang you rules method to

 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)],
                ];
        }

Update

Your 4th rule will also be throwing error should be

[['start','end'],'datetime','format' => 'php:Y-m-d H:i:s'],

I have updated the code block above too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

2.1m questions

2.1m answers

60 comments

57.0k users

...