Esempio n. 1
0
 /**
  * Callback used on add this attribute to an input
  *
  * @param Input $input The input in which the attribute will be added
  * @param mixed $value The value of this attribute
  *
  * @return mixed $value The value sanitized
  */
 public static function onAdd(Input $input, $value)
 {
     if ($input->attr('multiple') && substr($value, -2) !== '[]') {
         $value .= '[]';
     }
     return $value;
 }
Esempio n. 2
0
 /**
  * Validates the input value according to this attribute
  *
  * @param Input $input The input to validate
  *
  * @return boolean|string True if its valid, string with the error if not
  */
 public static function validate(Input $input)
 {
     $value = $input->val();
     //File
     if ($input->attr('type') === 'file') {
         $value = isset($value['name']) && !empty($value['size']) ? $value['name'] : null;
     }
     $attr = $input->attr('required');
     return empty($attr) || !empty($value) || strlen($value) > 0 ? true : sprintf(static::$error_message, $attr);
 }
Esempio n. 3
0
 /**
  * Callback used on add this attribute to an input
  *
  * @param Input $input The input in which the attribute will be added
  * @param mixed $value The value of this attribute
  *
  * @return boolean $value The value sanitized
  */
 public static function onAdd(Input $input, $value)
 {
     if (!is_bool($value)) {
         throw new \InvalidArgumentException('The multiple value must be a boolean');
     }
     if ($value && ($name = $input->attr('name')) && substr($name, -2) !== '[]') {
         $input->attr('name', $name . '[]');
     }
     return $value;
 }
Esempio n. 4
0
 /**
  * Validates the input value according to this attribute
  *
  * @param Input $input The input to validate
  *
  * @return boolean|string True if its valid, string with the error if not
  */
 public static function validate(Input $input)
 {
     $value = $input->val();
     //File
     if ($input->attr('type') === 'file') {
         $value = isset($value['name']) ? $value['name'] : null;
     }
     $attr = str_replace('/', '\\/', $input->attr('pattern'));
     return empty($attr) || empty($value) || filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^{$attr}\$/"))) ? true : sprintf(static::$error_message, $attr);
 }
Esempio n. 5
0
 /**
  * Validates the input value according to this attribute
  *
  * @param Input $input The input to validate
  *
  * @return boolean|string True if its valid, string with the error if not
  */
 public static function validate(Input $input)
 {
     $value = $input->val();
     if (empty($value['tmp_name'])) {
         return true;
     }
     $attr = $input->attr('accept');
     $accept = array_map('trim', explode(',', $attr));
     $filename = $value['tmp_name'];
     $finfo = finfo_open(FILEINFO_MIME_TYPE);
     $mime = finfo_file($finfo, $filename);
     finfo_close($finfo);
     return array_search($mime, $accept) !== false ? true : sprintf(static::$error_message, $attr);
 }
Esempio n. 6
0
 /**
  * {@inheritDoc}
  */
 public function validate()
 {
     $value = $this->val();
     if (!empty($value) && !filter_var($value, FILTER_VALIDATE_URL)) {
         $this->error(static::$error_message);
         return false;
     }
     return parent::validate();
 }
Esempio n. 7
0
 /**
  * {@inheritDoc}
  */
 public function validate()
 {
     $value = $this->val();
     if (!empty($value) && !preg_match('/^#[A-Fa-f0-9]{6}$/', $value)) {
         $this->error(static::$error_message);
         return false;
     }
     return parent::validate();
 }
Esempio n. 8
0
 /**
  * {@inheritDoc}
  */
 public function validate()
 {
     $value = $this->val();
     if (isset(self::$uploadErrors[$value['error']])) {
         $this->error(self::$uploadErrors[$value['error']]);
         return false;
     }
     return parent::validate();
 }
Esempio n. 9
0
 /**
  * {@inheritDoc}
  */
 public function validate()
 {
     $value = $this->val();
     if ($value) {
         if (!($date = date_create($value))) {
             $this->error(static::$error_message);
             return false;
         }
         $this->val($date->format(static::$format));
     }
     return parent::validate();
 }
Esempio n. 10
0
 /**
  * {@inheritDoc}
  */
 public function validate()
 {
     $value = $this->val();
     if (!empty($value) && !$this->allowNewValues) {
         if ($this->attr('multiple') && is_array($value)) {
             foreach ($value as $val) {
                 if (!isset($this->options[$val])) {
                     $this->error(static::$error_message);
                     return false;
                 }
             }
         } elseif (!isset($this->options[$value])) {
             $this->error(static::$error_message);
             return false;
         }
     }
     return parent::validate();
 }
Esempio n. 11
0
 /**
  * Validates the datetime input value according to this attribute
  *
  * @param Input $input The input to validate
  *
  * @return boolean|string True if its valid, string with the error if not
  */
 public static function validateDatetime(Input $input)
 {
     $value = $input->val();
     $attr = $input->attr('min');
     return empty($attr) || strtotime($value) >= strtotime($attr) ? true : sprintf(static::$error_message, $attr);
 }
Esempio n. 12
0
 public function __construct()
 {
     $this->input = Input::radio();
 }
Esempio n. 13
0
 /**
  * Validates the input value according to this attribute
  *
  * @param Input $input The input to validate
  *
  * @return boolean|string True if its valid, string with the error if not
  */
 public static function validate(Input $input)
 {
     $value = $input->val();
     $attr = $input->attr('maxlength');
     return empty($attr) || strlen($value) <= $attr ? true : sprintf(static::$error_message, $attr);
 }
Esempio n. 14
0
 public function __construct()
 {
     $this->input = Input::checkbox();
 }
Esempio n. 15
0
 public function testUrl()
 {
     $input = Input::url();
     $this->genericElementTest($input);
     $this->genericInputTest($input, 'url');
     //Values
     $input->val('invalid-url');
     $this->assertFalse($input->isValid());
     $input->val('http://valid-url.com');
     $this->assertTrue($input->isValid());
     $this->assertEquals('http://valid-url.com', $input->val());
 }
Esempio n. 16
0
 public function testColor()
 {
     $input = Input::color();
     $this->genericElementTest($input);
     $this->genericInputTest($input, 'color');
     //Values
     $input->val('red');
     $this->assertFalse($input->isValid());
     $input->val('11234f');
     $this->assertFalse($input->isValid());
     $input->val('#11234f');
     $this->assertTrue($input->isValid());
     $this->assertEquals('#11234f', $input->val());
 }