Complex form validation with PHP

I’ve created a simple Email contact form as described in the Kirby Cookbook. It works very well.

Now I’m trying to create a more complexe version where the form presents different fields depending on a chosen subject via <select>.

So let’s say if the user selects topic 1 the input fields A and B are displayed but if the user selects topic 2 B and C are displayed. That behavior is achieved via Javascript.

I’m not from the PHP World and im wondering what is the best strategy to check for the required fields in such a situation ? I could implement a pure Javascript version to validate the input but i would like to try to achieve it with PHP.