PHP Classes
elePHPant
Icontem

File: src/Field/CheckBox.php

Recommend this page to a friend!
  Classes of Derek McLean  >  HTML Forms  >  src/Field/CheckBox.php  >  Download  
File: src/Field/CheckBox.php
Role: Class source
Content type: text/plain
Description: Class source
Class: HTML Forms
Generate and validate submitted HTML forms
Author: By
Last change:
Date: 10 months ago
Size: 1,268 bytes
 

Contents

Class file image Download
<?php
/**
 * User: delboy1978uk
 * Date: 19/11/2016
 * Time: 21:37
 */

namespace Del\Form\Field;

use
Del\Form\Renderer\Field\CheckboxRender;
use
Del\Form\Traits\CanRenderInlineTrait;
use
Del\Form\Traits\HasOptionsTrait;

class
CheckBox extends FieldAbstract implements ArrayValueInterface
{

    use
CanRenderInlineTrait;
    use
HasOptionsTrait;

   
/**
     * We end up ignoring this during rendering Checkboxes, see the renderer for info
     *
     * @return string
     */
   
public function getTag()
    {
        return
'div';
    }

    public function
init()
    {
       
$this->setValue([]);
       
$this->setRenderInline(false);
       
$this->setRenderer(new CheckboxRender());
    }

   
/**
     * @param $key
     * @return $this
     */
   
public function checkValue($key)
    {
       
$values = $this->getValue();
       
$values[$key] = true;
       
$this->setValue($values);
        return
$this;
    }

   
/**
     * @param $key
     * @return $this
     */
   
public function uncheckValue($key)
    {
       
$values = $this->getValue();
        if (
in_array($key, $values)) {
           
$index = array_search($key, $values);
            unset(
$values[$index]);
        }
       
$this->setValue($values);
        return
$this;
    }
}