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

67 lines
1.6 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
{
2020-01-28 15:25:51 +01:00
private static $version = '5.12.0';
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
2020-01-28 14:54:17 +01:00
Requirements::css('https://use.fontawesome.com/releases/v'.self::config()->get('version').'/css/all.css');
Requirements::css(FONT_AWESOME_DIR . '/client/dist/css/lib/fontawesome-iconpicker.min.css');
Requirements::javascript(FONT_AWESOME_DIR . '/client/dist/js/lib/fontawesome-iconpicker.min.js');
// Module
Requirements::css(FONT_AWESOME_DIR . '/client/dist/css/font-awesome-module.css');
Requirements::javascript(FONT_AWESOME_DIR . '/client/dist/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)
{
2020-01-28 14:50:15 +01:00
if (!empty($this->value) && !preg_match('/^(fas|fa|far|fab) fa-[a-z0-9]+/', $this->value)) {
2015-05-09 05:55:15 +02:00
$validator->validationError(
$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
}