Profile.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use dektrium\user\models\Profile as BaseProfile;
  5. class Profile extends BaseProfile
  6. {
  7. /**
  8. * Clean parent rules: keep only attributes that exist in table schema.
  9. * Then merge with custom rules.
  10. */
  11. public function rules()
  12. {
  13. $parentRules = parent::rules();
  14. // real columns in DB table
  15. $columns = array_keys(static::getTableSchema()->columns);
  16. $filteredParentRules = [];
  17. foreach ($parentRules as $rule) {
  18. // if rule not an array (rare), keep as is
  19. if (!is_array($rule) || !isset($rule[0])) {
  20. $filteredParentRules[] = $rule;
  21. continue;
  22. }
  23. // normalize attributes list (string "a, b" or array)
  24. $raw = $rule[0];
  25. if (is_string($raw)) {
  26. $attrs = preg_split('/\s*,\s*/', trim($raw));
  27. } elseif (is_array($raw)) {
  28. $attrs = $raw;
  29. } else {
  30. $attrs = [(string)$raw];
  31. }
  32. // keep only attrs that are real columns
  33. $keep = [];
  34. foreach ($attrs as $a) {
  35. if (in_array($a, $columns, true)) {
  36. $keep[] = $a;
  37. }
  38. }
  39. if (empty($keep)) {
  40. // no real attributes left for this rule -> skip it
  41. continue;
  42. }
  43. // replace attribute list in the rule
  44. $rule[0] = (count($keep) === 1) ? $keep[0] : $keep;
  45. $filteredParentRules[] = $rule;
  46. }
  47. // custom rules (adjusted: firma_full and phone NOT required)
  48. $custom = [
  49. // required only those you want required (remove firma_full & phone from required)
  50. [['inn', 'zkpo', 'u_address', 'f_address'], 'required'],
  51. // numeric checks
  52. ['role', 'integer'],
  53. // ['inn', 'match', 'pattern' => '/^[0-9]+$/'],
  54. // ['inn', 'string', 'min' => 6, 'max' => 20],
  55. // ['zkpo', 'match', 'pattern' => '/^[0-9]+$/'],
  56. // ['zkpo', 'string', 'min' => 6, 'max' => 12],
  57. // strings
  58. [['firma_full','u_address','f_address','member','member_phone','member_email','site','fax','files','fio','at_org'], 'string', 'max' => 255],
  59. // email & phone formats
  60. ['member_email', 'email'],
  61. [['phone','member_phone','fax'], 'match', 'pattern' => '/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/'],
  62. // make fields safe so load() will accept them (even if not required)
  63. [['inn','zkpo','u_address','f_address','firma_full','member','phone','member_phone','fax','member_email','site','role','files','status','org_type','at_org','fio'], 'safe'],
  64. ];
  65. return array_merge($filteredParentRules, $custom);
  66. }
  67. /**
  68. * filter parent attributeLabels to only include real columns,
  69. * then merge with our labels
  70. */
  71. public function attributeLabels()
  72. {
  73. $parent = parent::attributeLabels();
  74. $columns = array_keys(static::getTableSchema()->columns);
  75. $filtered = [];
  76. foreach ($parent as $k => $v) {
  77. if (in_array($k, $columns, true)) {
  78. $filtered[$k] = $v;
  79. }
  80. }
  81. return array_merge($filtered, [
  82. 'firma_full' => Yii::t('app','Full organization name'),
  83. 'inn' => Yii::t('app','INN'),
  84. 'zkpo' => Yii::t('app','ZKPO'),
  85. 'u_address' => Yii::t('app','Legal address'),
  86. 'f_address' => Yii::t('app','Personal address'),
  87. 'member' => Yii::t('app','Contact Person'),
  88. 'phone' => Yii::t('app','Phone'),
  89. 'fax' => Yii::t('app','Fax'),
  90. 'member_phone' => Yii::t('app','Member phone'),
  91. 'member_email' => Yii::t('app','E-mail'),
  92. 'site' => Yii::t('app','Site'),
  93. 'fio' => Yii::t('app','ФИО'),
  94. 'at_org' => Yii::t('app','Организация'),
  95. 'org_type' => Yii::t('app','Тип организации'),
  96. ]);
  97. }
  98. }