Merge pull request #190 from silverstripe-rebelalliance/numeric

BUG: Fixes #189 default error message being shown
This commit is contained in:
Will Rossiter 2014-02-12 22:03:58 +13:00
commit 743e7773f7
4 changed files with 23 additions and 5 deletions

View File

@ -600,7 +600,9 @@ class UserDefinedForm_Controller extends Page_Controller {
if($this->Fields()) { if($this->Fields()) {
foreach($this->Fields() as $field) { foreach($this->Fields() as $field) {
$messages[$field->Name] = $field->getErrorMessage()->HTML(); if (!in_array($field->ClassName, array('EditableEmailField', 'EditableNumericField'))) {
$messages[$field->Name] = $field->getErrorMessage()->HTML();
}
if($field->Required) { if($field->Required) {
$rules[$field->Name] = array_merge(array('required' => true), $field->getValidation()); $rules[$field->Name] = array_merge(array('required' => true), $field->getValidation());

View File

@ -14,6 +14,15 @@ class EditableEmailField extends EditableFormField {
private static $plural_name = 'Email Fields'; private static $plural_name = 'Email Fields';
public function getFormField() { public function getFormField() {
if ($this->Required) {
// Required and Email validation can conflict so add the Required validation messages
// as input attributes
$errorMessage = $this->getErrorMessage()->HTML();
$field = new EmailField($this->Name, $this->Title);
$field->setAttribute('data-rule-required','true');
$field->setAttribute('data-msg-required',$errorMessage);
return $field;
}
return new EmailField($this->Name, $this->Title); return new EmailField($this->Name, $this->Title);
} }

View File

@ -499,7 +499,8 @@ class EditableFormField extends DataObject {
$title = strip_tags("'". ($this->Title ? $this->Title : $this->Name) . "'"); $title = strip_tags("'". ($this->Title ? $this->Title : $this->Name) . "'");
$standard = sprintf(_t('Form.FIELDISREQUIRED', '%s is required').'.', $title); $standard = sprintf(_t('Form.FIELDISREQUIRED', '%s is required').'.', $title);
$errorMessage = ($this->CustomErrorMessage) ? $this->CustomErrorMessage : $standard; // only use CustomErrorMessage if it has a non empty value
$errorMessage = (!empty($this->CustomErrorMessage)) ? $this->CustomErrorMessage : $standard;
return DBField::create_field('Varchar', $errorMessage); return DBField::create_field('Varchar', $errorMessage);
} }

View File

@ -22,12 +22,18 @@ class EditableNumericField extends EditableTextField {
$taf = new NumericField($this->Name, $this->Title); $taf = new NumericField($this->Name, $this->Title);
$taf->setRows($this->getSetting('Rows')); $taf->setRows($this->getSetting('Rows'));
$taf->addExtraClass('number'); $taf->addExtraClass('number');
return $taf;
} }
else { else {
$taf = new NumericField($this->Name, $this->Title, null, $this->getSetting('MaxLength')); $taf = new NumericField($this->Name, $this->Title, null, $this->getSetting('MaxLength'));
$taf->addExtraClass('number'); $taf->addExtraClass('number');
return $taf;
} }
if ($this->Required) {
// Required and numeric validation can conflict so add the Required validation messages
// as input attributes
$errorMessage = $this->getErrorMessage()->HTML();
$taf->setAttribute('data-rule-required','true');
$taf->setAttribute('data-msg-required',$errorMessage);
}
return $taf;
} }
} }