day8 / re-com

A ClojureScript library of reusable components for Reagent
https://re-com.day8.com.au
MIT License
799 stars 145 forks source link

Override validate fn with :validate-fn #41

Closed theronic closed 9 years ago

theronic commented 9 years ago

Please let me validate input-text input with my own fn. Right now I have to do it in on-change, but would like to use re-com's validation statuses.

mike-thompson-day8 commented 9 years ago

I'm afraid I don't follow. Can you provide more detail about your request please. Is this related to the #43 which you raised?

mike-thompson-day8 commented 9 years ago

I'm going to close this because I suspect it is a duplicate of #43. But if that is wrong, please drop a further comment in here, and we'll re-open.

theronic commented 9 years ago

This issue is distinct from #43. By validation, I mean validating entered text, not restricting input. So, for example, a user may enter a number, but only certain numbers would be considered valid.

E.g. I want to do a Luhn check on an IMEI number or a credit card number, which cannot be done with a regular expression. Instead of preventing entry, I want to give the user feedback about invalid input.

mike-thompson-day8 commented 9 years ago

As per #43 you can set :change-on-blur? to false and then your :on-change fn will get called back on each character press. On each callback, this :on-change callback can choose to update the value/ratom given to :model or not.
In that way, you control what the user is able to enter. You control validation in whatever way you want.

mike-thompson-day8 commented 9 years ago

For example, the following (untested) code will never allow the user to enter an 'e' character:

(defn test
  [] 
  (let [model   (reagent/ratom  "world")]
    (fn []
      [input-text 
        :model   model 
        :change-on-blur? false
        :on-change  (fn [new-value] (if-not (some #(= \e %) new-value)  (reset! model new-value))])))
theronic commented 9 years ago

Thanks, that attribute solves my problem :). I saw the :change-on-blur? attribute, but it was not obvious to me that false would result in on-change firing on-key-up. I tried specifying :on-key-up fn, but that was not allowed. May I suggest considering {:fire-change :on-blur/:on-key-up} for future APIs.

mike-thompson-day8 commented 9 years ago

Yep, there was a reason I suggested this was a duplicate of #43.