In Yii2, I have two fields in my database: emailandshopId
email
shopId
unique
empty
NULL
These are my rules in the model:
[['email'],'default','value' => NULL], [['shopId'], 'integer'], [['email','shopId'], 'unique', 'targetAttribute' => ['email', 'shopId'], 'message' => 'Already taken!'],
This does not work when I have two entries, for example. email="NULL"and shopId="1".
email="NULL"
shopId="1"
How can i solve this?
Set the property skipOnEmptyat false http://www.yiiframework.com/doc-2.0/yii-validators-validator.html# $ skipOnEmpty-detail
skipOnEmpty
false
, .
[['email','shopId'], 'unique', 'targetAttribute' => ['email', 'shopId']]
email shopId, email ( ), shopId email, shopId (). DB.
, ,
[['email'], 'unique', 'targetAttribute' => ['email', 'shopId']]
: " email , , email shopId email.
I used the when condition inside the rule
[ ['email', 'shopId'], 'unique', 'targetAttribute' => ['email', 'shopId'], 'message' => 'Diese E-Mail Adresse ist bereits registriert!', 'when' => function ($model) { return !empty($model->email); } ],
Source: https://habr.com/ru/post/1678648/More articles:How to center a form using Bootstrap? - htmlWhich is more efficient: docs or names like Python? - pythonredefineBest way to subtract a vector from a matrix in Julia - julia-langHow Haskell handles parallel computing on a multicore machine / cluster - multithreadingI do not understand what is happening with file_exists and scandir - filePHP - inconsistency with "Undefined offset" error - arraysImportError: no module named google.cloud - python-2.7Create a common dashboard for multiple properties on one page - google-analyticsCreate SourceMaps for a single js file that includes modules with npm and gulp requirements - requireAll Articles