Code Monkey home page Code Monkey logo

yii2-required-validator's Introduction

Starting with php 7.4 I started getting errors.

Typed property must not be accessed before initialization

Yii2 allows to create a model without data and the program works until [[validate]] is called.

Moreover, we don't receive a validation error, but a php error.

There are the following calls $model->$attribute when an attribute value is validating and adding an error. What generates the error.

There are 5 ways:

  1. Add getter and setter everywhere - a bunch of methods, not clear
  2. Remove types, it's a return to php 7.3 and is not clear
  3. Change all properties to ?int $attribute = null. Not clear
  4. Change public to private and override __get from Component. This method won't work at all (1)
  5. Add [['prop1', 'prop2'], InitializedValidator::class] before other rules for user land code. Great, this is clear. But you'll have to rewrite many models and projects. I would like this to work like in php<=7.3

Conclusion

It is necessary to slightly change the internal RequiredValidator so that it checks the initialization of the fields itself. There is a modified version in the src folder.

(1) - When accessing private fields, the __get magic method will be called. There we can check that the property is not initialized and return null in [[validateAttribute]] and [[addError]] validator. We will get the required validation error.

BUT! When we call [[setAttributes]], we will get an attempt to set values in private properties. And it will either be property not found, or you need to change __set($name, $value) and all private ones will become public. Not clear.

Adding only the setName() setter will make the fields writeOnly and will cause an error when read.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.