I like to validate my block on save. (And throw error if needed)
Now i could use the
page.update:before and parse the content all way through until i found my block-type. But this is not so elegant, kills performance and is pretty prone to failure.
Is there another way. Maybe inherit some block-classes to do it? Or hook some API?
What exactly do you want to validate? Fields inside a block can be validated out of the box, but I guess you have something else in mind?
Of course, I have something special in mind. Otherwise kirby will become boring for me.
Inside the Block, i have other blocks and structures which having a slug-field. At the end, i need to check, that all values occur just once.
It’s for the kirby-form-blocks plugin.
Hm, in general validation always happens on the field level, so either for the whole blocks fields or for fields inside the block. I can’t really see anything in the block class that would allow you to hook into for validation, although you can have custom block models since 3.6
I tried something similar when I built form blocks for a client project. In the end I couldn’t find an easy solution and just appended
name-3, … in the controller.
@texnixe And what about a nice walker for the site.update-hook? You have surely some nice snippets in petto where you can shake out of your sleeves?
@thguenther Nice workaround. If the semantic doesn’t important, you could also use
In my case, there’s some more conditions i like to prove.
I think a block level validator would be nice, similar to the field level validators. Something for a feature request.
Luckily, you can filter blocks by type, so it wouldn’t be that difficult to find your form blocks. However, the downside of the hook approach is that you would actually have to find out if a page/site/user has a blocks field at all, which is not ideal.
You maybe can use Schema: Data Validation | Nette Documentation for this. I’m using it for validate data that is being send to a custom rest api.