|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
-
-
-
- namespace frontend\models;
-
- use Yii;
- use yii\base\Model;
-
-
- class ProducerCodeForm extends Model
- {
-
- public $id_producer ;
- public $code;
-
-
-
- public function rules()
- {
- return [
- ['id_producer','required','message' => 'Champs obligatoire'],
- ['id_producer', 'integer'],
- ['id_producer', function($attribute, $params) {
- if ($this->id_producer) {
- $producer = Producer::findOne($this->id_producer);
- if (!$producer) {
- $this->addError($attribute, 'Ce producteur n\'existe pas.');
- }
- }
- }],
- ['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) {
- $producer = Producer::findOne($this->id_producer);
- if ($producer) {
- return strlen($producer->code);
- } else {
- return false;
- }
- }],
- ['code', function($attribute, $params) {
- $code = $this->$attribute;
- $producer = Producer::findOne($this->id_producer);
- if ($producer && strtolower(trim($code)) != strtolower(trim($producer->code))) {
- $this->addError($attribute, 'Code incorrect');
- }
- }],
- ];
- }
-
- public function attributeLabels()
- {
- return [
- 'id_producer' => 'Producteur',
- 'code' => 'Code du producteur',
- ];
- }
-
- }
|