October CMS - required checkbox

- 1 answer

Ad

How could I validate if the checkbox is ticked?

public $rules = [
    'name' => 'required',
    'email' => 'required|email',
    'phone' => 'required',
    'terms' => 'required',
];

'terms' is the name of the checkbox. Now I get the alert about required field even if it is checked.

HTML:

<input type="checkbox" name="terms" value="1" id="terms"/>

Simple HTML5 attribute required is also not working.

Ad

Answer

Ad

Wouter Van Damme has it right. You simply need to use the trait listed in that documentation. like so:

public $rules = [
    'name' => 'required',
    'email' => 'required|email',
    'phone' => 'required',
    'terms' => 'required|accepted',
];

here is an october specific link although the answers are the same.http://octobercms.com/docs/services/validation#rule-accepted

Ad
source: stackoverflow.com
Ad