0

I have a field as such in PHP:

return [
    'text1' => [
        'control' => [
            'text' => [
                'show_if' => [
                    'field' => 'text2',
                    'comparison' => '===',
                    'Show text 1'
                ]
            ]
        ]
    ],
    'text2' => [
        'control' => 'text'
    ]
];

In theory text1's control (a form) should only show if text2's value is ever Show text 1, but I'm baffled as to how I'd transfer this relationship from PHP to JS.

I can't use eval, since that's evil, but is there no way for me to practically transform the code?

In other words, I'm trying to get PHP to tell JS to do:

(if text2 !== 'Show text 1') {
 //Don't show text 1
}
5
  • Very vague but echo json_encode($array); ??? Commented May 18, 2021 at 17:19
  • You want to establish this condition in JS? Commented May 18, 2021 at 17:20
  • @nice_dev Yes, indeed. Commented May 18, 2021 at 18:07
  • @AbraCadaver The problem is not sending this array, not sure how you got the idea from the question. The problem is how do I get JS to do (if text2 !== 'Show text 1') { //don't show text1 Commented May 18, 2021 at 18:08
  • Ahhh so you want some to write JS code though you haven’t shown how in PHP Commented May 18, 2021 at 18:14

1 Answer 1

1

You could make a map of operator to function which performs the operation. e.g. :

const conditions = {
  '===': (a, b) => a === b,
  '!==': (a, b) => a !== b,
  'count': (a, b) => a && a.length === b,
};

const cond = conditions[condition];
if (!cond) {
  throw new Error('Unknown condition: ' + condition);
}
if (cond(a, b)) {
  // Matches
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.