<p>In your FieldType you have <code>$this-&gt;element</code> available, representing the Element the field is in. If you use <code>$this-&gt;element-&gt;lightSwitchHandle</code> you can access its value.</p>

CC BY-SA 3.0
2015-06-18T14:29:39.043Z
Oh! So much simpler than I thought. I'd been scouting through Craft's code looking at ways to loop through the Entry's fields etc. Thanks. (Coincidentally, just as your answer popped up I was reading your Import plugin!)
Phil Gyford
CC BY-SA 3.0
2015-06-18T15:08:50.383Z
Although, having tried this... in order to ensure the Assets field is used when the Lightswitch is 'on', my custom field type needs to be a child of Lightswitch, and that needs to check the Assets field is set. I can't work out how to check if it has any Assets set.
Phil Gyford
CC BY-SA 3.0
2015-06-18T15:40:37.960Z
I think I stumbled on the answer: `$this->element->[assetsHandle]->total()`.
Phil Gyford
CC BY-SA 3.0
2015-06-18T16:59:13.593Z
That is correct
Bob Olde Hampsink
CC BY-SA 3.0
2015-06-18T14:25:49.483Z
2015-06-18T14:25:49.483Z
Bob Olde Hampsink
<p>I have an Assets field which I want to be required only if a separate Lightswitch field on the same Entry is 'on'. I'm currently trying to implement this with a simple plugin that creates a custom field type (<a href="https://github.com/rally-agency/Craft-Unique-Text-FieldType" rel="nofollow">along these lines</a>).</p>

<p>However, in my Field Type's <code>validation()</code> method I can't work out how to access the Lightswitch field's value on this same Entry, in order see if it's 'on' or not.</p>

<plugins><validation>
A plugin to validate a field depending on another field's value
# A plugin to validate a field depending on another field's value

(Asked by: username_0 on 2015-06-18)

I have an Assets field which I want to be required only if a separate Lightswitch field on the same Entry is 'on'. I'm currently trying to implement this with a simple plugin that creates a custom field type ([along these lines](https://github.com/rally-agency/Craft-Unique-Text-FieldType)).

However, in my Field Type's `validation()` method I can't work out how to access the Lightswitch field's value on this same Entry, in order see if it's 'on' or not.

**Tags:** plugins, validation

---

## 1 answers

### Answer by username_1 on 2015-06-18. Score: 3 | Accepted answer

In your FieldType you have `$this->element` available, representing the Element the field is in. If you use `$this->element->lightSwitchHandle` you can access its value.

> **Comments:**
> **username_0**: Oh! So much simpler than I thought. I'd been scouting through Craft's code looking at ways to loop through the Entry's fields etc. Thanks. (Coincidentally, just as your answer popped up I was reading your Import plugin!)  
> **username_0**: Although, having tried this... in order to ensure the Assets field is used when the Lightswitch is 'on', my custom field type needs to be a child of Lightswitch, and that needs to check the Assets field is set. I can't work out how to check if it has any Assets set.  
> **username_0**: I think I stumbled on the answer: `$this->element->[assetsHandle]->total()`.  
> **username_1**: That is correct  

---

