| @@ -63,12 +63,14 @@ class Admins::UsersController < Admins::BaseController | |||||
| end | end | ||||
| def create | def create | ||||
| logger.info "---validate_create_params: #{validate_create_params}" | |||||
| Users::AdminCreateUserForm.new(validate_create_params).validate! | Users::AdminCreateUserForm.new(validate_create_params).validate! | ||||
| user = User.new(create_params) | |||||
| user.type = 'User' | |||||
| user.login = User.generate_login("p") | |||||
| user = User.new(create_params) | |||||
| login = User.generate_login("p") | |||||
| user.type = 'User' | |||||
| user.login = login | |||||
| user.mail = "#{login}@example.org" | |||||
| ActiveRecord::Base.transaction do | ActiveRecord::Base.transaction do | ||||
| if user.save! | if user.save! | ||||
| UserExtension.create!(user_id: user.id) | UserExtension.create!(user_id: user.id) | ||||
| @@ -108,6 +110,6 @@ class Admins::UsersController < Admins::BaseController | |||||
| end | end | ||||
| def validate_create_params | def validate_create_params | ||||
| create_params.slice(:mail, :phone, :password) | |||||
| create_params.slice(:phone, :password) | |||||
| end | end | ||||
| end | end | ||||
| @@ -2,22 +2,10 @@ class Users::AdminCreateUserForm | |||||
| include ActiveModel::Model | include ActiveModel::Model | ||||
| attr_accessor :mail, :phone, :password | |||||
| attr_accessor :phone, :password | |||||
| validates :mail, presence: true, format: { with: CustomRegexp::EMAIL, message: "邮箱格式错误." } | |||||
| validates :phone, presence: true, format: { with: CustomRegexp::PHONE, message: "手机号格式错误" } | validates :phone, presence: true, format: { with: CustomRegexp::PHONE, message: "手机号格式错误" } | ||||
| validates :password, presence: true, length: { minimum: 8, maximum: 16 }, format: { with: CustomRegexp::PASSWORD, message: "8~16位密码,支持字母数字和符号" } | validates :password, presence: true, length: { minimum: 8, maximum: 16 }, format: { with: CustomRegexp::PASSWORD, message: "8~16位密码,支持字母数字和符号" } | ||||
| validate :check_mail | |||||
| private | |||||
| def check_mail | |||||
| return if mail.blank? | |||||
| if User.exists?(mail: mail) | |||||
| raise "邮箱 #{mail} 已使用." | |||||
| errors.add(:mail, :not_exist) | |||||
| end | |||||
| end | |||||
| end | end | ||||
| @@ -11,7 +11,6 @@ | |||||
| <div class="form-group px-2"> | <div class="form-group px-2"> | ||||
| <div class="form-row"> | <div class="form-row"> | ||||
| <%= f.input :phone, as: :tel, label: '手机号', required: true, wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-sm-11', autocomplete: 'off' } %> | <%= f.input :phone, as: :tel, label: '手机号', required: true, wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-sm-11', autocomplete: 'off' } %> | ||||
| <%= f.input :mail, as: :email, label: '邮箱地址', required: true, error_html: { id: 'password_error'}, wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-sm-11' } %> | |||||
| </div> | </div> | ||||
| </div> | </div> | ||||