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?
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.Sign up to join this community
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.