You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7KB

  1. <?php
  2. namespace frontend\models;
  3. use Yii;
  4. use yii\base\Model;
  5. /**
  6. * Producer Code form
  7. */
  8. class ProducerCodeForm extends Model {
  9. public $id_producer ;
  10. public $code;
  11. /**
  12. * @inheritdoc
  13. */
  14. public function rules() {
  15. return [
  16. ['id_producer','required','message' => 'Champs obligatoire'],
  17. ['id_producer', 'integer'],
  18. ['id_producer', function($attribute, $params) {
  19. if ($this->id_producer) {
  20. $producer = Etablissement::findOne($this->id_producer);
  21. if (!$producer) {
  22. $this->addError($attribute, 'Ce producteur n\'existe pas.');
  23. }
  24. }
  25. }],
  26. ['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) {
  27. $producer = Etablissement::findOne($this->id_producer);
  28. if ($producer) {
  29. return strlen($producer->code);
  30. } else {
  31. return false;
  32. }
  33. }],
  34. ['code', function($attribute, $params) {
  35. $code = $this->$attribute;
  36. $producer = Etablissement::findOne($this->id_producer);
  37. if ($producer && strtolower(trim($code)) != strtolower(trim($producer->code))) {
  38. $this->addError($attribute, 'Code incorrect');
  39. }
  40. }],
  41. ];
  42. }
  43. public function attributeLabels() {
  44. return [
  45. 'id_producer' => 'Producteur',
  46. 'code' => 'Code de l\'établissement',
  47. ];
  48. }
  49. }