I am writing a custom validation rule. In my form I validate fields using rules with a validation group called 'my-event'. One of my rules is if a checkbox is checked called 'other', then the text field 'other' needs to be filled in.
My request gets validated against these rules:
EventRequest.php
public function rules()
{
return [
'my-event.event-types' => 'required'
'my-event.event-type-other' => [
'string', new CheckboxIsValid($checkboxArray)
],
];
}
CheckboxIsValid is a helper class that I have written that implements Laravel's rule:
class CheckboxIsValid implements Rule
{
public $checkboxArray;
public function __construct($checkboxArray)
{
$this->checkboxArray = $checkboxArray;
}
public function passes($attribute, $value)
{
if(in_array('other', $this->checkboxArray)) {
if($value) {
return true;
}
}
return false;
}
}
This checks whether 'other' is in my array of checked checkboxes. I would like to pass in the value of my-event.event-types
. How do I do this?