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

63 lines
1.4 KiB
PHP
Raw Permalink 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
{
2024-07-27 11:22:49 +02: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');
2024-07-27 11:38:37 +02:00
Requirements::css(FONT_AWESOME_DIR . '/dist/css/app.css');
Requirements::javascript(FONT_AWESOME_DIR . '/dist/js/app.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
}