silverstripe-framework/forms/NumericField.php
Damian Mooyman 0b1f297873 Merge remote-tracking branch 'origin/3.1'
Conflicts:
	.travis.yml
	README.md
	admin/code/LeftAndMain.php
	admin/css/screen.css
	admin/scss/screen.scss
	api/RestfulService.php
	conf/ConfigureFromEnv.php
	control/injector/ServiceConfigurationLocator.php
	control/injector/SilverStripeServiceConfigurationLocator.php
	core/ClassInfo.php
	core/Object.php
	css/AssetUploadField.css
	css/ComplexTableField_popup.css
	dev/CSSContentParser.php
	dev/DevelopmentAdmin.php
	docs/en/changelogs/index.md
	docs/en/misc/contributing/code.md
	docs/en/reference/execution-pipeline.md
	filesystem/GD.php
	filesystem/ImagickBackend.php
	filesystem/Upload.php
	forms/Form.php
	forms/FormField.php
	forms/HtmlEditorConfig.php
	forms/gridfield/GridFieldDetailForm.php
	forms/gridfield/GridFieldSortableHeader.php
	lang/en.yml
	model/Aggregate.php
	model/DataList.php
	model/DataObject.php
	model/DataQuery.php
	model/Image.php
	model/MySQLDatabase.php
	model/SQLQuery.php
	model/fieldtypes/HTMLText.php
	model/fieldtypes/Text.php
	scss/AssetUploadField.scss
	search/filters/SearchFilter.php
	security/Authenticator.php
	security/LoginForm.php
	security/Member.php
	security/MemberAuthenticator.php
	security/MemberLoginForm.php
	security/Security.php
	tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php
	tests/control/HTTPTest.php
	tests/control/RequestHandlingTest.php
	tests/filesystem/UploadTest.php
	tests/forms/FormTest.php
	tests/forms/NumericFieldTest.php
	tests/model/DataListTest.php
	tests/model/DataObjectTest.php
	tests/model/TextTest.php
	tests/security/MemberAuthenticatorTest.php
	tests/security/SecurityDefaultAdminTest.php
	tests/view/SSViewerCacheBlockTest.php
	tests/view/SSViewerTest.php
2014-11-18 12:45:54 +13:00

83 lines
1.7 KiB
PHP

<?php
/**
* Text input field with validation for numeric values. Supports validating
* the numeric value as to the {@link i18n::get_locale()} value.
*
* @package forms
* @subpackage fields-formattedinput
*/
class NumericField extends TextField {
public function Type() {
return 'numeric text';
}
public function getAttributes() {
return array_merge(parent::getAttributes(), array(
'type' => 'number',
'step' => 'any' // allows entering float/decimal numbers like "1.2" instead of just integers
));
}
/**
* Validate this field
*
* @param Validator $validator
* @return bool
*/
public function validate(Validator $validator) {
if(!$this->value) {
return true;
}
require_once THIRDPARTY_PATH."/Zend/Locale/Format.php";
$valid = Zend_Locale_Format::isNumber(
trim($this->value),
array('locale' => i18n::get_locale())
);
if(!$valid) {
$validator->validationError(
$this->name,
_t(
'NumericField.VALIDATION', "'{value}' is not a number, only numbers can be accepted for this field",
array('value' => $this->value)
),
"validation"
);
return false;
}
return true;
}
public function dataValue() {
return (is_numeric($this->value)) ? $this->value : 0;
}
/**
* Returns a readonly version of this field
*/
public function performReadonlyTransformation() {
$field = new NumericField_Readonly($this->name, $this->title, $this->value);
$field->setForm($this->form);
return $field;
}
}
class NumericField_Readonly extends ReadonlyField {
public function performReadonlyTransformation() {
return clone $this;
}
public function Value() {
return Convert::raw2xml($this->value ? "$this->value" : "0");
}
}