PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 30, 2021

[FIXED] Setting unknown property: yii\validators\NumberValidator::0

 December 30, 2021     yii, yii2     No comments   

Issue

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 – yii\base\UnknownPropertyException
Setting unknown property: yii\validators\NumberValidator::0

error in line of _price-item:

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

Model:

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

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 \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;

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


Solution

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 – yii\base\UnknownPropertyException Setting unknown property: yii\validators\NumberValidator::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.



Answered By - Muhammad Omer Aslam
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing