1

I need to run validation in a custom fieldtype but only if a field is required.

How do you determine if the current field is required from within a fieldtype class?

1

In Craft 1/2, there isn't a particularly clean or easy way to do this because $this->model->required is only set in the context of when it's being called from a field layout.

In the context of a field type, Craft doesn't set it because required is a concept that belongs to a field layout and a given field can belong to multiple field layouts.

This whole process has been refactored and is much cleaner in Craft 3 so that $this->model->required works in the context of a field type where you'd expect it to.

3
  • Hmm, doesn't play ball for me, it returns false whatever the field setting. Calling it from within a custom validate() method, that should work right? Apr 28 '15 at 16:46
  • Updated my answer.
    – Brad Bell
    Apr 29 '15 at 6:39
  • Any way to work around this one pre Craft 3? May 11 '15 at 10:24

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.