By leon kessler on
Is this possible through the Drupal form API?
So for example, I have an address form. The postcode field is required unless you select Ireland as your country, then it's optional.
With the Drupal form API, you set what is required and then Drupal renders the form. Is there a way to alter requirements using JavaScript?
Thanks
Comments
Here is one possible way to
Here is one possible way to do this:
If you want to add a little Javascript that will add the '*', you can do the following (note that you will still need the validate hook because just putting the '*' in doesn't actually make the field required):
I didn't test this, so there may be a typo somewhere in the code. I hope not though.
thanks for the detailed
thanks for the detailed response.
This was sort of how I was thinking about doing it, but seeing your code has helped me out in a few areas. The only thing is that I am editing the Ubercart checkout form, so I've got two postcode fields to deal with, plus the form does not use labels.
Oh and also you've got . for concatenation instead of +
The '.' he is using is for
The '.' he is using is for PHP concatenation, so those are correct.
Actually, it should be
Actually, it should be
.form-requiredinstead of.form-requiedin the selectors.