How to integrate JSON Form library to CakePHP 4.3.2?

Hi there,

I would require to build a dynamic fieldset in a form, and I was not able to find any satisfactory means of CakePHP 4.3.2 to be capable of it.
So for example if user selects one option in a selection field, other fields contents would change and some other fields would disappear and such.

Therefore I was searching around the internet, and came across a seemingly great library called “JSON Form”, in which you can set up dynamic forms in seconds with a really simple language. JSON Form GitHUB link

How could I fully integrate this library to CakePHP, and how could I make it so that CakePHP would handle all the database side, and it would only fetch the data that was input by the user?
Because then that data would of course need to be validated by CakePHP and then sent off to the MySQL DB. (Even though it has it’s own build in validation as well, I’d rather stick with CakePHP’s… After all that is why I decided to use CakePHP in the first place.)

I’d appreciate some step-by-step guide here… :stuck_out_tongue:
Thank you for your time.

Your desired functionality is a pure frontend requirement which therefore only can be handled via JS (showing/hiding fields depending on a given structure/data input)

But the base concept of “never trust the user” also should be kept in mind because users are able to manipulate the DOM and therefore are able to send you stuff which you are not expecting.

So you would need to add all permutations of possible input data into your validation logic.

One example which I use is:

    $validator->numeric( 'netto' )
      ->allowEmptyString( 'netto', null, function( $context ) {
        if( !empty( $context['data']['is_done'] ) && $context['data']['is_done'] === '1' ) {
          return false;

        return true;
      } )
      ->requirePresence( 'netto', function( $context ) {
        if( !empty( $context['data']['is_done'] ) && $context['data']['is_done'] === '1' ) {
          return true;

        return false;
      } );

Here I connect 2 fields together: netto (number field) and is_done (checkbox)

If is_done is not checked then the netto field is allowed to be empty.
If is_done is checked then the netto field has to have a value.

But I don’t know how to do that depending on the given input data (so e.g. add validation to nested fields which are dynamically added)

Everything you’re talking about doing looks like client-side JavaScript. CakePHP doesn’t provide any helpers for that, but also doesn’t hinder you from adding any JavaScript code you might need. In other words, it’s pretty much all entirely unrelated to the fact that your server-side code is built with CakePHP. You might want to build yourself a custom helper or write some elements to generate JavaScript code that you find to be common across various of your pages, that’s about the only place where the two would intersect in any way.