silverstripe-font-awesome/code/formfields/FontAwesomeField.php

62 lines
1.4 KiB
PHP
Raw Normal View History

2015-05-09 02:31:32 +02:00
<?php
2017-06-09 09:14:28 +02:00
namespace SilverStripe\FontAwesome;
use SilverStripe\Forms\TextField;
use SilverStripe\View\Requirements;
2015-05-09 02:31:32 +02:00
/**
* Class FontAwesomeField
*/
2015-05-09 04:58:24 +02:00
class FontAwesomeField extends TextField
2015-05-09 02:31:32 +02:00
{
2015-05-10 02:00:14 +02:00
/**
* @return string
*/
2015-05-09 02:31:32 +02:00
public function Type()
{
2015-05-09 04:58:24 +02:00
return 'text';
2015-05-09 02:31:32 +02:00
}
2015-05-10 02:00:14 +02:00
/**
* @param array $properties
*
* @return mixed
*/
2015-05-09 04:58:24 +02:00
public function Field($properties = array())
2015-05-09 02:31:32 +02:00
{
2015-05-09 04:58:24 +02:00
$this->addExtraClass('form-control icp icp-auto');
//Libraries
Requirements::css(FONT_AWESOME_DIR . '/css/lib/font-awesome.min.css');
Requirements::css(FONT_AWESOME_DIR . '/css/lib/font-awesome-iconpicker.min.css');
Requirements::javascript(FONT_AWESOME_DIR . '/js/lib/font-awesome-iconpicker.min.js');
// Module
Requirements::css(FONT_AWESOME_DIR . '/css/font-awesome-module.css');
Requirements::javascript(FONT_AWESOME_DIR . '/js/font-awesome-module.js');
2015-05-09 04:58:24 +02:00
Requirements::set_force_js_to_bottom(true);
2015-05-09 05:55:15 +02:00
2015-05-09 04:58:24 +02:00
return parent::Field($properties);
2015-05-09 02:31:32 +02:00
}
2015-05-09 05:55:15 +02:00
2015-05-10 02:00:14 +02:00
/**
* @param $validator
*
* @return bool
*/
2015-05-09 05:55:15 +02:00
public function validate($validator)
{
if (!empty ($this->value) && !preg_match('/^fa-[a-z]+/', $this->value)) {
2015-05-09 05:55:15 +02:00
$validator->validationError(
2015-05-10 02:00:14 +02:00
$this->name, 'Please enter a valid Font Awesome font name format.', 'validation', false
2015-05-09 05:55:15 +02:00
);
2015-05-09 05:55:15 +02:00
return false;
}
2015-05-09 05:55:15 +02:00
return true;
}
2015-05-09 02:31:32 +02:00
}