Skip to content

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

Open
DemidenkoAndrey wants to merge 2 commits intotrailblazer:masterfrom
DemidenkoAndrey:fix/filled-scope-record-in-uniq-validation
Open

fix(UniquenessValidator): fill record's scope fields if they exist on form#55
DemidenkoAndrey wants to merge 2 commits intotrailblazer:masterfrom
DemidenkoAndrey:fix/filled-scope-record-in-uniq-validation

Conversation

@DemidenkoAndrey
Copy link
Copy Markdown

@DemidenkoAndrey DemidenkoAndrey commented Jul 21, 2017

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 DemidenkoAndrey force-pushed the fix/filled-scope-record-in-uniq-validation branch 2 times, most recently from df6fbe1 to d196b1c Compare July 21, 2017 16:41
@DemidenkoAndrey DemidenkoAndrey force-pushed the fix/filled-scope-record-in-uniq-validation branch from d196b1c to 211c82a Compare July 21, 2017 16:41
@seuros
Copy link
Copy Markdown
Member

seuros commented Jul 21, 2017

Can you write a sample for this usage ?

@DemidenkoAndrey
Copy link
Copy Markdown
Author

DemidenkoAndrey commented Jul 21, 2017

@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
Copy link
Copy Markdown
Author

@seuros Do you have any update for this issue?

@shadowreplicant
Copy link
Copy Markdown

+1
@DemidenkoAndrey there is skipped test for it

skip "replace ActiveModel::Validations with our own, working and reusable gem."

@DemidenkoAndrey
Copy link
Copy Markdown
Author

DemidenkoAndrey commented Dec 14, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants