AddForm.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace ghs\models\Admin;
  3. /**
  4. * 添加员工表单模型
  5. */
  6. class AddForm extends BaseForm
  7. {
  8. /**
  9. * @var string 用户名
  10. */
  11. public $username;
  12. /**
  13. * @var string 密码
  14. */
  15. public $password;
  16. /**
  17. * @var string 姓名
  18. */
  19. public $name;
  20. /**
  21. * @var string 手机号
  22. */
  23. public $mobile;
  24. /**
  25. * @var int 角色ID
  26. */
  27. public $roleId;
  28. /**
  29. * @var int 商家ID
  30. */
  31. public $sjId;
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['username', 'password', 'name', 'mobile', 'roleId'], 'required', 'message' => '{attribute}不能为空'],
  39. [['username', 'name'], 'string', 'max' => 50],
  40. [['password'], 'string', 'min' => 6, 'max' => 20],
  41. [['mobile'], 'match', 'pattern' => '/^1[3-9]\d{9}$/', 'message' => '手机号格式不正确'],
  42. [['roleId', 'sjId'], 'integer'],
  43. [['username'], 'validateUsername'],
  44. ];
  45. }
  46. /**
  47. * 验证用户名是否已存在
  48. *
  49. * @param string $attribute 属性名
  50. * @param array $params 参数
  51. */
  52. public function validateUsername($attribute, $params)
  53. {
  54. if (!$this->hasErrors()) {
  55. $exists = \bizGhs\admin\classes\AdminClass::getByCondition(['username' => $this->$attribute]);
  56. if ($exists) {
  57. $this->addError($attribute, '用户名已存在');
  58. }
  59. }
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'username' => '用户名',
  68. 'password' => '密码',
  69. 'name' => '姓名',
  70. 'mobile' => '手机号',
  71. 'roleId' => '角色',
  72. 'sjId' => '商家ID',
  73. ];
  74. }
  75. }