reform-rails
reform-rails copied to clipboard
fix(UniquenessValidator): fill record's scope fields if they exist on form
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
Can you write a sample for this usage ?
@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
@seuros Do you have any update for this issue?
+1 @DemidenkoAndrey there is skipped test for it https://github.com/trailblazer/reform-rails/blob/c0124fccab74668e340f2dbdf0dba0e3d550604b/test/active_record_test.rb#L54
@shadowreplicant Thank you for mention that. I've added second commit with removing skip from this test