Fork me on GitHub
#fulcro
<
2019-03-04
>
eoliphant17:03:36

any suggestions on the best way to handle multiple validators (with corresponding error message) with form state?

eoliphant17:03:11

say a field is required, and can’t have more than 10 chars, etc

tony.kay17:03:52

If you’re using spec, s/and

eoliphant19:03:03

it’s a bit of both. but the thing is associating messages to specific failure

eoliphant19:03:15

so even with spec if i have (s/and nonblank? less-than-10-chars?), i’d like to be able to distinguish the cases

eoliphant19:03:44

so I can tell you “Field is required”, “Field can’t be greater than 10 chars” ,etc

tony.kay19:03:33

what’s in the book is what you’ve got. If you want custom rendering based on the cases, you’ll have to write that part into the error message UI code. Form-state doesn’t do the UI parts at all

mdhaney20:03:17

@eoliphant to distinguish failures with spec, you can use explain-data and then check the :pred field to tell you which predicate failed. It’s not too difficult to setup generic handling for this by using a multi method to dispatch on the predicate and return an error message. I think the “expound” library may do this for you, although I’ve never used it (had to do this on a previous project and it was easy enough to just roll my own solution)

eoliphant20:03:00

yeah I’ve played around with expound a bit, and there’s another one called ‘parse’ or something that allows you to be explicit with your messages, which is better for user facing stuff, but the last time I checked, the 1.10 improvments had broken some of it.

mdhaney20:03:49

@eoliphant as I recall, the main issue was that when spec reported the predicate that caused the failure, for built in predicates it was inconsistent- most of the time it reported the fully qualified name, but sometimes it had just the name, with no namespace. I’m not sure if that changed in 1.10 or not, but can see how fixing it would break the dispatching. That was one reason I didn’t use a library- it’s a quick change to make in my code if spec changes, vs waiting for a library to be updated.

mdhaney20:03:33

Also, thanks for the pointer to ‘phrase’. I hadn’t seen that library before - might be useful for something I’m working on.

eoliphant21:03:59

that makes sense. Sure no problem

eoliphant21:03:40

I want to add some extra metadata to a class for something i’m working on.

{:query ..
 :my/stuff {..}
is there an api or something that would allow me to get at that info, particularly dynamically? e.g. this -> MyComponent -> :my/stuff

eoliphant23:03:06

Sweet thanks!

currentoor23:03:02

@eoliphant also if it’s a protocol you’re going to be adding a lot, then you can get shorter syntax too, using this https://github.com/fulcrologic/fulcro-incubator/blob/develop/src/main/fulcro/incubator/defsc_extensions.clj#L114

currentoor23:03:11

in the incubator

currentoor23:03:33

here’s an example of it being used for routing targets