These are chat archives for dry-rb/chat

20th
Jan 2016
Tim Riley
@timriley
Jan 20 2016 04:56
key(:plan) { |plan| plan.str? & plan.inclusion?(%w[individual student]) }
key(:institution_name) { |name| name.none? | name.filled? }
key(:student_evidence) { |evidence| evidence.none? | evidence.filled? }

rule(:require_institution_name) do
  value(:plan).student_plan? > rule(:institution_name).filled?
end

rule(:require_student_evidence) do
  value(:plan).student_plan? > rule(:student_evidence).filled?
end

def student_plan?(value)
  value == "student”
end
<3 you, conditional validations
Fran Worley
@fran-worley
Jan 20 2016 08:09
See I would love for a shorthand of key(:institution_name) { |name| name.none? | name.filled? }.
What we are actually saying is, don't care what this is at this stage but I need to use it later in a Hight Level Rule. Wouldn't something like key(:institution_name)or even key(:institution_name, :student_evidence) be easier for cases like this?
Fran Worley
@fran-worley
Jan 20 2016 09:09
So much more expressive than
Piotr Solnica
@solnic
Jan 20 2016 09:44
It is not true that you don't care :) you actually do care because you expect it to be either nil or a string
Shortcuts can be provided of course
It is a low priority though
Fran Worley
@fran-worley
Jan 20 2016 09:47
Yeah I guess... Hadn't really thought of it like that. Forget that it defaults to .str? I was really just thinking aloud. Agree it's very very low down the list!
Piotr Solnica
@solnic
Jan 20 2016 09:48
I'd be interested in building expressive DSLs eventually. But now I gotta focus on essential functionality
Re str? it is not needed in form schemas as everything is a string there /cc @timriley
Tim Riley
@timriley
Jan 20 2016 10:29
Ah yes. That was a spurious str?. Thanks @solnic.
Piotr Solnica
@solnic
Jan 20 2016 11:19
@timriley btw in 0.5.1 you’ll be able to do rule(plan: :required_when_student)
Tim Riley
@timriley
Jan 20 2016 11:19
ohhhh! :hushed:
That’s neat.
Piotr Solnica
@solnic
Jan 20 2016 11:20
and have msgs under plan key
ugh, sorry, confusing the name but you get the idea
Tim Riley
@timriley
Jan 20 2016 11:20
Yep :)
Piotr Solnica
@solnic
Jan 20 2016 11:20
furthermore, it should work with built-in msgs too
Tim Riley
@timriley
Jan 20 2016 11:20
Yeag, that’s great. I had to add some custom ones.
Piotr Solnica
@solnic
Jan 20 2016 11:21
when you don’t care about a custom message but I bet in 99% cases people will want to customize msgs
Tim Riley
@timriley
Jan 20 2016 11:21
I got some weird keys
en:
  errors:
    rules:
      institution_name:
        institution_name: "must be filled"
Piotr Solnica
@solnic
Jan 20 2016 11:21
yeah exactly
working on fixing/improving this
Tim Riley
@timriley
Jan 20 2016 11:21
cool
Piotr Solnica
@solnic
Jan 20 2016 11:23
we can have various shortcuts, ie rule(student_evidence: :filled?).when(rule(:plan).value.student?) or sth
Tim Riley
@timriley
Jan 20 2016 11:23
That’s quite expressive.
Piotr Solnica
@solnic
Jan 20 2016 11:23
this will come closer to 1.0 though, first I gotta make sure core stuff is solid
Tim Riley
@timriley
Jan 20 2016 11:23
Good work putting a solid underlying rule strucutre in place that’ll support stuff like this eventually :)
Piotr Solnica
@solnic
Jan 20 2016 11:23
exactly :)
ok, I’ll work on this now, hoping to push 0.5.1 later today
Piotr Solnica
@solnic
Jan 20 2016 11:51
@fran-worley @timriley confirmation(:password) should now work with a msg defined under en.errors.password.confirmation
Tim Riley
@timriley
Jan 20 2016 12:06
:pray:
Piotr Solnica
@solnic
Jan 20 2016 12:27
@kwando dryrb/dry-validation@5543df5
Piotr Solnica
@solnic
Jan 20 2016 12:35
lemme know if this is helpful
any rbx users here? /cc @tak1n
Benjamin Klotz
@tak1n
Jan 20 2016 12:40
hey yea I'm quietly following the chat :P
Piotr Solnica
@solnic
Jan 20 2016 12:47
@tak1n I broke dry-v on rbx
I’ve no idea why it’s failing now
works fine on all other rubies
Piotr Solnica
@solnic
Jan 20 2016 13:28
ok fixed
Benjamin Klotz
@tak1n
Jan 20 2016 13:37
okay :D
what was the problem?
Piotr Solnica
@solnic
Jan 20 2016 13:40
different behavior when relying on duck-typing with basicobject
Benjamin Klotz
@tak1n
Jan 20 2016 13:42
okay
at least u didnt subclass array and overwrote some methods on which other array methods depend on :D :D
Piotr Solnica
@solnic
Jan 20 2016 13:42
right ;)
Benjamin Klotz
@tak1n
Jan 20 2016 13:42
there are so many bug reports because ppl do that and rubinius is implemented in ruby itself :P
Piotr Solnica
@solnic
Jan 20 2016 13:51
Screen Shot 2016-01-20 at 14.50.44.png
I’m really proud of the yard doc coverage :joy:
@fran-worley would you have a moment to pull from master and see if your stuff is working? I’ve got 0.6.0 tag ready to be released...
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 14:03
@solnic so now key(:name) is like: key(:name, &:filled?) or I just didn't read the changelog right?
Piotr Solnica
@solnic
Jan 20 2016 14:03
@mrbongiolo in Form, yes
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 14:03
Ah, I see.
I'll try to start converting my validation to dry-v in here
Piotr Solnica
@solnic
Jan 20 2016 14:04
from AM?
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 14:05
yup
but they are pretty messy right now... so it's gonna take a while
Piotr Solnica
@solnic
Jan 20 2016 14:05
that’s gonna be a great excercise
lemme know how it goes and what could be improved
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 14:06
sure thing
I'm using Reform, so I'll also test how the dry-v integration in on the wild :D
it should just work, but you know
Piotr Solnica
@solnic
Jan 20 2016 14:07
in 0.7.0 I want to focus on improvements for error messages and then we could think about some shortcut-dsls for common cases…then some clean up / bug fixing and 1.0.0 can be released
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 14:07
ah, good
custom messages are always located in the YML file right?
Piotr Solnica
@solnic
Jan 20 2016 14:08
yes
you can merge custom ones in self.messages too but it is not recommended
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 14:10
how I define a custom message? ie is_this_a_super_hero?
just en.errors.is_this_a_super_hero? then?
Piotr Solnica
@solnic
Jan 20 2016 14:17
@mrbongiolo assuming you’ve got rule(:is_this_a_super_hero) { .. } then yes
Fran Worley
@fran-worley
Jan 20 2016 15:51
@solnic just on my way back will do when I get my laptop open!!
@mrbongiolo if you need a fellow reform AM to reform dry-v user, I am one such person. So keep in touch would be great to see how you find the transition. I put a guide up in the wiki for dry v with achieving the basics of AM in dry-v.
It's very much a draft but should give you a starting point. They key is to ensure you are correctly coercing everything. Concepts in AM like numericality don't 100% map (and quite rightly so)
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 16:00
Hey @fran-worley thanks, I've seen your wiki page, it's very good.
it will help a lot, but first I need to upgrade some gems... fix all the warnings
Fran Worley
@fran-worley
Jan 20 2016 16:06
Always a good place to start! I'm working on this for the next couple of hours so do give me a shout. Would be great to share issues/ ideas and see how a fellow reform user gets on with it. There is more flexibility with reform as you can access the Form object in your custom predicate methods.
Krzysztof Wawer
@wafcio
Jan 20 2016 16:10
This message was deleted
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 16:11
Sure, I'll keep in touch, not sure if I'll be able to update validations today, maybe I can start, have to finish a small feature
Piotr Solnica
@solnic
Jan 20 2016 16:12
@fran-worley awesome :)
I’m basically waiting for you a green light from you that stuff is working in your use cases and I’ll push a release :)
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 16:21
fran-worley-driven-deploy? :D
Fran Worley
@fran-worley
Jan 20 2016 16:27
@solnic coffee in hand laptop open. Let's go!
Piotr Solnica
@solnic
Jan 20 2016 16:30
go go go :D
Fran Worley
@fran-worley
Jan 20 2016 16:33
Right it looks good. my tests are passing...
Forms that used to work still work the same. Let me just update my password code and test out the new message thing
Piotr Solnica
@solnic
Jan 20 2016 16:47
sweet
Fran Worley
@fran-worley
Jan 20 2016 16:52

@solnic I've probably overstepped the gun (so shoot me down in necessary) ! but...

key(:password){ |pass| pass.none? | (pass.filled? & pass.min_size?(6)) }
key(:password_confirmation){ |pass| pass.none? | pass.filled? }

rule(password_confirmation: :confirmation_valid?) do |pass_conf| 
  value(:password).filled?.then(pass_conf.filled? & pass_conf.eql?([:password, :password_confirmation]))
end

seems to be returning undefined method 'filled?' for nil:NilClass and I can't see why...

pointing to this line:
value(:password).filled?.then(pass_conf.filled? & pass_conf.eql?([:password, :password_confirmation]))
Piotr Solnica
@solnic
Jan 20 2016 16:53
@fran-worley why not just use confirmation(:password)?
Fran Worley
@fran-worley
Jan 20 2016 16:53
I do in a simpler form. Let me try it
Oh I remember. I don't fully understand precedence. If I define pass.min_size?(6) and then define confirmation(:password) does it respect both, or override?
Piotr Solnica
@solnic
Jan 20 2016 16:55
and btw if you want to compare values from multiple places you need to use rule(:foo, eql?: [:left, :right])
Fran Worley
@fran-worley
Jan 20 2016 16:55
Ah that makes sense, I wasn't sure (even though you clearly show it in your examples) :wink:
Piotr Solnica
@solnic
Jan 20 2016 16:56
lemme try and see
Fran Worley
@fran-worley
Jan 20 2016 17:05

@solnic I'm clearly still missing something.
schema

key(:password){ |pass| pass.none? | (pass.filled? & pass.min_size?(6)) }
key(:password_confirmation){ |pass| pass.none? | pass.filled? }

rule(password_confirmation: :eql?) do |pass_conf| 
  value(:password).filled?.then(confirmation(:password))
end

error config (wasn't exactly sure what it would fall under so tried both)

en:
  errors:
    password_confirmation:
      eql?: "doesn't agree to password"
    password:
      confirmation: "doesn't agree to password"
  rules:
    password: 
      filled?: "can't be blank"
      min_size?: "must be at least %{num} characters long"

I'm getting this: undefined method 'name' for #<Array:0x007ff852a22ae0>. Which from memory usually means that I've not got the error message right as Reform is failing to read the message

Piotr Solnica
@solnic
Jan 20 2016 17:08
uhm, gimme a minute it’s not what I meant :)
Fran Worley
@fran-worley
Jan 20 2016 17:08
Haha I'll test it in my simple form example at least that way I can give you the answer you want without over complicating/ getting ahead of the API and moving into Fran Dreamland...
Fran Worley
@fran-worley
Jan 20 2016 17:13
So to answer your original question Yes. Your update fixes messages on confirmation(:password) One tiny thing. You put the error on password: :confirmation so the message gets attached to the password field and not the confirmation field which was not quite what I expected...
again. Tiny tiny thing. Absolutely perfect in all other respects. Push push push!
Piotr Solnica
@solnic
Jan 20 2016 17:16
hmm, right, this makes no sense
I’m gonna revert that actually, it should be attached to password_confirmation as that’s the name of the field after all
Fran Worley
@fran-worley
Jan 20 2016 17:20
cool. so the message config would look like:
en:
  errors:
    password_confirmation: "doesn't agree to password"
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 17:27
@solnic and @fran-worley how are you creating the unique validations?
Fran Worley
@fran-worley
Jan 20 2016 17:28
I use reform, and I've done this to verify uniqueness of an email address (you may see a theme emerging in my examples...)
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 17:30
validation :default do
      key(:name)
      key(:email) do |email|
        email.filled? & email.email? & email.unique?(:email)
      end
      key(:cellphone)

      def email?(value)
        ValidateEmail.valid?(value)
      end

      def unique?(field, value)
        form.model.class.exists?(field => value)
      end
    end
i was thinking in something like this (with reform and rails)
Fran Worley
@fran-worley
Jan 20 2016 17:30
class BasicForm < Reform::Form
  validation :default do
    #basic stuff here
  end
  validation :unique_email, if: :default do
    configure { |config| config.messages_file = 'config/error_messages.yml' }
    key(:email) { |email| email.empty? | email.unique_email? }
    def unique_email?(value)
      User.where(deleted: false, email: value).where.not(id: form.persisted? ? form.id : nil).blank?
    end   
  end
end
I split it into a new section as whats the point in hitting the DB if something basic like key(:email, &:filled?) doesn't pass?
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 17:31
yup
the problem is that for the same form I have to check uniqueness of 3 fields =/
that's why I was looking for a more generic approach
Fran Worley
@fran-worley
Jan 20 2016 17:33
Yeah, you'll have found my Uniqueness section in the guide I wrote really helpful (NOT!)
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 17:34
hahahaha
Fran Worley
@fran-worley
Jan 20 2016 17:34
Yeah even the TRB example is just
validation :default do
  key(:title) { |title| title.filled? & title.unique? }

  def unique?(value)
    Album.find_by(title: value).nil?
  end
end
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 17:35
undefined method%' for nil:NilClass` is that I require a message?
Fran Worley
@fran-worley
Jan 20 2016 17:35
It's not Dry-V but you could look at
"reform/form/validation/unique_validator.rb"
for inspiration
@mrbongiolo it might be, I know I bumped into that and solved it (sadly I can't remember exactly how)... check that your using feature and not include and that your requiring "reform/form/dry"
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 17:39
It seems to be working with include...
Fran Worley
@fran-worley
Jan 20 2016 17:40
Word of warning. If you try to use Dry V in a collection you'll get an undefined method key if you don't switch to feature
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 17:40
uhm, ok :)
I guess I haven't added a spec for dry-v in collections
Fran Worley
@fran-worley
Jan 20 2016 17:42
It's no different apart from the 'feature' thing. You define your validation blocks within the collection and the dry-v schema in your validation block.
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 17:44
If i set a custom YML file does it override the one that come with dry-v? or I can just put my own messages in there?
Fran Worley
@fran-worley
Jan 20 2016 17:45
That I wasn't 100% clear on, so I just defined everything! A bit of a safety net so I could see exactly what message was going to get spat out.
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 17:45
ah ok, let me try it out then
Fran Worley
@fran-worley
Jan 20 2016 17:45
That might be your error. If you don't define the message correctly in the yaml file for a rule you'll get a very cryptic reform error message
and at the very least you have to provide a message for custom ones
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 17:52
uhm nice
if I use i18n, then it just loades my dry_v_messages in config/locales
expected that #<Api::Customer::Update:0x007f6735e94140> had an error on :cellphone with message: 'já está em uso'. Instead got: {:email=>["that bloody value already exists ¬¬"]}o/
Piotr Solnica
@solnic
Jan 20 2016 17:55
@fran-worley bundle update dry-validation and replace all these lines with this: confirmation(:password, min_size: 6)
Fran Worley
@fran-worley
Jan 20 2016 17:56
@solnic I hope my crazy suggestions are helping. I don't want to make more work for you!
Piotr Solnica
@solnic
Jan 20 2016 17:56
they are super helpful
Fran Worley
@fran-worley
Jan 20 2016 17:56
@mrbongiolo whats your yml file look like and what is your validation block like?
@solnic theoretically, could you chain a number of options to that. E.g. confirmation(:password, min_size: 6, format: REGEX)
Piotr Solnica
@solnic
Jan 20 2016 17:58
yes
just remember it “ANDs” them
key(name) do |value|
  options.map { |p, args| value.__send__(:"#{p}?", *args) }.reduce(:&)
end
Fran Worley
@fran-worley
Jan 20 2016 17:59
That is good to remember.
Piotr Solnica
@solnic
Jan 20 2016 17:59
this is what happens behind the scenes, so you can pass one or more args as an array, too
ie some_predicate: [1, 2]
oh and ? is appended automatically
lemme know if that works for you now :)
Fran Worley
@fran-worley
Jan 20 2016 18:02
Nice, will do. On the subject of custom predicates can you pass in extra options e.g.
key(:name){|name| name.unique?(User, :name)}
def unique?(klass,attribute,value) do
#predicate code here...
end
Piotr Solnica
@solnic
Jan 20 2016 18:02
uhm, of course, it’s your method
I would suggest dedicated methods though
so unique_user? etc
Fran Worley
@fran-worley
Jan 20 2016 18:03
I do separate ones personally. I find it clearer. was just intrigued bu @mrbongiolo idea.
Piotr Solnica
@solnic
Jan 20 2016 18:04
which idea?
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 18:04
This message was deleted
Fran Worley
@fran-worley
Jan 20 2016 18:04
validation :default do
      key(:name)
      key(:email) do |email|
        email.filled? & email.email? & email.unique?(:email)
      end
      key(:cellphone)

      def email?(value)
        ValidateEmail.valid?(value)
      end

      def unique?(field, value)
        form.model.class.exists?(field => value)
      end
    end
It would appear you can't quote code properly on here...
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 18:06
my method works, not sure if I can pass this field to my message...
Fran Worley
@fran-worley
Jan 20 2016 18:09
@solnic I can confirm your new confirmation helper works.
my error config looks like this:
en:    
  errors:
    password_confirmation: "doesn't agree to password"
    password:
      min_size?: "must be at least %{num} characters long"
Piotr Solnica
@solnic
Jan 20 2016 18:10
awesome
tagging/releasing then
:) :tada: dry-validation 0.6.0 was released :tada: :)
Fran Worley
@fran-worley
Jan 20 2016 18:16
Lovely thanks again, a big +1 to you.
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 18:17
:D
Piotr Solnica
@solnic
Jan 20 2016 18:17
:dancers:
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 19:44
I'm trying this: confirmation(:password, size: 6..20), but then getting this error:
10) Api::Customer::Update::WithPassword Contract should require cellphone to be set
      Failure/Error: !@contract.validate(params) && has_correct_error

      TypeError:
        wrong argument type FalseClass (expected Proc)
      # /home/vagrant/.gem/ruby/2.2.3/gems/dry-logic-0.1.2/lib/dry/logic/predicate.rb:26:in `curry'
      # /home/vagrant/.gem/ruby/2.2.3/gems/dry-logic-0.1.2/lib/dry/logic/rule_compiler.rb:62:in `visit_predicate'
      # /home/vagrant/.gem/ruby/2.2.3/gems/dry-logic-0.1.2/lib/dry/logic/rule_compiler.rb:18:in `visit'
      # /home/vagrant/.gem/ruby/2.2.3/gems/dry-logic-0.1.2/lib/dry/logic/rule_compiler.rb:47:in `visit_val'
      # /home/vagrant/.gem/ruby/2.2.3/gems/dry-logic-0.1.2/lib/dry/logic/rule_compiler.rb:18:in `visit'
      # /home/vagrant/.gem/ruby/2.2.3/gems/dry-logic-0.1.2/lib/dry/logic/rule_compiler.rb:67:in `visit_and'
      # /home/vagrant/.gem/ruby/2.2.3/gems/dry-logic-0.1.2/lib/dry/logic/rule_compiler.rb:18:in `visit'
      # /home/vagrant/.gem/ruby/2.2.3/gems/dry-logic-0.1.2/lib/dry/logic/rule_compiler.rb:13:in `block in call'
      # /home/vagrant/.gem/ruby/2.2.3/gems/dry-logic-0.1.2/lib/dry/logic/rule_compiler.rb:13:in `map'
      # /home/vagrant/.gem/ruby/2.2.3/gems/dry-logic-0.1.2/lib/dry/logic/rule_compiler.rb:13:in `call'
      # /home/vagrant/.gem/ruby/2.2.3/bundler/gems/dry-validation-250f7a41478e/lib/dry/validation/schema.rb:81:in `initialize'
      # /home/vagrant/.gem/ruby/2.2.3/bundler/gems/dry-validation-250f7a41478e/lib/dry/validation/schema/form.rb:26:in `initialize'
      # /home/vagrant/.gem/ruby/2.2.3/gems/reform-2.1.0/lib/reform/form/dry.rb:46:in `initialize'
      # /home/vagrant/.gem/ruby/2.2.3/gems/reform-2.1.0/lib/reform/form/dry.rb:32:in `new'
      # /home/vagrant/.gem/ruby/2.2.3/gems/reform-2.1.0/lib/reform/form/dry.rb:32:in `call'
      # /home/vagrant/.gem/ruby/2.2.3/gems/reform-2.1.0/lib/reform/validation/groups.rb:57:in `block in call'
      # /home/vagrant/.gem/ruby/2.2.3/gems/reform-2.1.0/lib/reform/validation/groups.rb:51:in `each'
      # /home/vagrant/.gem/ruby/2.2.3/gems/reform-2.1.0/lib/reform/validation/groups.rb:51:in `call'
      # /home/vagrant/.gem/ruby/2.2.3/gems/reform-2.1.0/lib/reform/validation.rb:36:in `valid?'
      # /home/vagrant/.gem/ruby/2.2.3/gems/reform-2.1.0/lib/reform/contract/validate.rb:18:in `validate!'
      # /home/vagrant/.gem/ruby/2.2.3/gems/reform-2.1.0/lib/reform/contract/validate.rb:10:in `validate'
      # /home/vagrant/.gem/ruby/2.2.3/gems/reform-2.1.0/lib/reform/form/validate.rb:23:in `validate'
      # ./spec/support/matchers/contract_validates_presence_matcher.rb:12:in `block (2 levels) in <top (required)>'
      # ./spec/operations/api/customer/update/with_password_spec.rb:14:in `block (3 levels) in <top (required)>'
On dry-v master right now
Piotr Solnica
@solnic
Jan 20 2016 19:45
@mrbongiolo it’s because it splats the args :( so range won’t work
Fran Worley
@fran-worley
Jan 20 2016 19:46
sorry I only tested a single value...
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 19:46
Ah ok
so I should do this one with a regular key + confirmation?
Piotr Solnica
@solnic
Jan 20 2016 19:46
yeah, for now at least
it’s a simple fix but I’m already in a “client work” mode so..
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 19:50
no problem
this confirmation(:field) is just a shorthand or?
Fran Worley
@fran-worley
Jan 20 2016 19:51
Its in the wiki under high level rules
Piotr Solnica
@solnic
Jan 20 2016 20:04
@mrbongiolo it’s a macro
it defines multiple rules for you
Piotr Solnica
@solnic
Jan 20 2016 20:33
btw we could easily introduce a shortcut syntax for defining conjunction rules via key(:email, format: /…/) etc.
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 20:34
My first updated contract:
contract do
    feature Reform::Form::Dry::Validations

    property :name
    property :email
    property :cellphone
    property :phone
    property :facebook_id
    property :device_id, virtual: true
    property :accept_notifications, virtual: true

    # validates :name, presence: true
    # validates :email,
    #           presence: true,
    #           email: true,
    #           unique: true
    # validates :cellphone,
    #           presence: true,
    #           phony_plausible: true,
    #           unique: true
    # validates :facebook_id, unique: { allow_blank: true }
    # validates :phone, phony_plausible: { allow_blank: true }

    validation :default do
      configure { config.messages = :i18n }

      key(:name)
      key(:email) do |email|
        email.filled? & email.email? & email.unique?(:email)
      end
      key(:cellphone) do |cellphone|
        cellphone.filled? &
        cellphone.valid_phone_number? &
        cellphone.unique?(:cellphone)
      end
      key(:facebook_id) do |facebook_id|
        facebook_id.none? | facebook_id.unique?(:facebook_id)
      end
      key(:phone) do |phone|
        phone.none? | phone.valid_phone_number?
      end

      def email?(value)
        ValidateEmail.valid?(value)
      end

      def valid_phone_number?(value)
        Phony.plausible?(value)
      end

      def unique?(field, value)
        return true if value == form.model.public_send(field)
        !form.model.class.exists?(field => value)
      end
    end
end
Piotr Solnica
@solnic
Jan 20 2016 20:35
nice one @mrbongiolo
I guess filled? could be dropped for phone number since Phony should handle an empty string, right?
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 20:37
Probably
but filled will ahve a better error message
well a "phone number is invalid" would be okay when it's empty, but I like to be explicit here
Piotr Solnica
@solnic
Jan 20 2016 20:39
good point!
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 20:39
Why call Phony if it's easier to just test with :filled?
:D
Piotr Solnica
@solnic
Jan 20 2016 20:39
right, of course you’re right
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 20:39
but it was your work that made it possible xD
@fran-worley how are you setting "_destroy" for an collection object?
Fran Worley
@fran-worley
Jan 20 2016 21:31
Basically I have a hidden field in my form which is set to true via JS when a user clicks the remove button. I used JS because I wanted the collection item to be removed from the UI on click aswell.
Ralf Schmitz Bongiolo
@mrbongiolo
Jan 20 2016 21:33
Ok, thanks! I went with a basic checkbox and the default Save action.
don't want to add JS to it righht now
Fran Worley
@fran-worley
Jan 20 2016 21:33
that would work too. Just call it _destroy