|
- <?php
-
- namespace frontend\models;
-
- use Yii;
- use yii\base\Model;
-
- /**
- * Producer Code form
- */
- class ProducerCodeForm extends Model {
-
- public $id_producer ;
- public $code;
-
- /**
- * @inheritdoc
- */
- public function rules() {
- return [
- ['id_producer','required','message' => 'Champs obligatoire'],
- ['id_producer', 'integer'],
- ['id_producer', function($attribute, $params) {
- if ($this->id_producer) {
- $producer = Etablissement::findOne($this->id_producer);
- if (!$producer) {
- $this->addError($attribute, 'Ce producteur n\'existe pas.');
- }
- }
- }],
- ['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) {
- $producer = Etablissement::findOne($this->id_producer);
- if ($producer) {
- return strlen($producer->code);
- } else {
- return false;
- }
- }],
- ['code', function($attribute, $params) {
- $code = $this->$attribute;
- $producer = Etablissement::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 de l\'établissement',
- ];
- }
-
- }
|