8

I have two channel_fields that can be filled, and I need one of these values to be required (but not both).

Is there fieldtype that can solve this problem?

NB Only one of the fields needs to be filled. I don't want to set both fields as required.

4
  • I am not aware of any add-on to manage this. I have had similar requests and gotten around it by using Safecracker on the front-end using JS validation, but this does nothing if you need to use the CP. – Justin Kimbrell Nov 30 '12 at 13:51
  • 2
    So you want to check that at least one of the two fields has been completed, but not necessarily both, right? A few questions to help guide us: Is it always the same two types of fields that are dependent on each other? If so what types are they? (text input, checkbox etc?) – Tom Davies Nov 30 '12 at 13:54
  • what kind of fields are we talking about. Would it be possible to replace with a checkbox group instead? – erwinheiser Nov 30 '12 at 14:03
  • The field are both simple text fields – Jan Nov 30 '12 at 15:36
6

So the short answer is, I'm not aware of an existing fieldtype that would do this at the moment.

However if you know a little PHP and you're willing to get your hands dirty it would be pretty easy to create something custom, depending on your validation requirements.

The full in's and out's of doing so go beyond the scope of this answer, but here's a summary of the approach I'd take:

The easiest thing would be too start with an existing compound field fieldtype (ie a fieldtype that outputs a group of fields in the CP). I'd probably start with VZ Address as the source is pretty readable and it's CC Share-Alike licensed.

You'd essentially just need to take the VZ Address FT and:

  • Rename the ft class/file as something else to avoid collisions.
  • strip out the stuff you don't need so you've just got a FT with two text fields, and set names for these for use in your template output.
  • hook up a simple validate() method that checks that at least one of the fields is not empty.

If you're feeling fancy you could add some javascript validation for extra sugar.

PS If you need help with this hit me up on chat or Twitter, and I'll do my best to help you out.

1
  • Thanks, I developed my own field type with VZ address as an example – Jan Dec 4 '12 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.