Fork me on GitHub

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


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


If you’re using spec, s/and


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


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


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


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


@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)


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.


@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.


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


that makes sense. Sure no problem


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


Sweet thanks!


@eoliphant also if it’s a protocol you’re going to be adding a lot, then you can get shorter syntax too, using this


in the incubator


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