reform-rails icon indicating copy to clipboard operation
reform-rails copied to clipboard

fix(UniquenessValidator): fill record's scope fields if they exist on form

Open DemidenkoAndrey opened this issue 7 years ago • 5 comments

When you try to create new record and we have validates_uniqueness_of with scope we need to assign scope property from form to model to run validation correct

DemidenkoAndrey avatar Jul 21 '17 16:07 DemidenkoAndrey

Can you write a sample for this usage ?

seuros avatar Jul 21 '17 16:07 seuros

@seuros We have to fill region on model to be insure that validation on model will work correct with our region from form

# model
User(name: string, region: string)
class UsersForm < Reform::Form
  include Reform::Form::ActiveRecord
  property :name
  property :region

  validates_uniqueness_of :name, scope: :region

  model :user
end

# users_controller
def create
  user = User.new
  @form = UsersForm.new(user)

  respond_to do |format|
    if @form.validate(user_params)
      @form.save
      format.html { redirect_to @form, notice: 'User was successfully created.' }
    else
      format.html { render :new }
    end
  end
end

def user_params
  params.require(:user).permit(:name, :region)
end

DemidenkoAndrey avatar Jul 21 '17 16:07 DemidenkoAndrey

@seuros Do you have any update for this issue?

DemidenkoAndrey avatar Sep 26 '17 08:09 DemidenkoAndrey

+1 @DemidenkoAndrey there is skipped test for it https://github.com/trailblazer/reform-rails/blob/c0124fccab74668e340f2dbdf0dba0e3d550604b/test/active_record_test.rb#L54

shadowreplicant avatar Dec 13 '17 20:12 shadowreplicant

@shadowreplicant Thank you for mention that. I've added second commit with removing skip from this test

DemidenkoAndrey avatar Dec 14 '17 12:12 DemidenkoAndrey