silverstripe-framework/forms/RequiredFields.php
Daniel Hensby 6c943007a1 removeRequiredField() limits field (fixes #2165)
Added tests to RequiredFields and fixed bugs that were found

Now you:
1. Can't add the same field name many times
2. Can use append RequiredFields correctly without fear of duplicates

I've also added a Deprecation warning to $useLabels as it's not used
*anywhere* in framework
2013-09-27 19:58:59 +02:00

160 lines
3.7 KiB
PHP

<?php
/**
* Required Fields allows you to set which fields
* need to be present before submitting the form
* Submit an array of arguments or each field as a
* seperate argument. Validation is performed on a name by
* name basis.
*
* @package forms
* @subpackage validators
*/
class RequiredFields extends Validator {
protected $required;
protected $useLabels = true;
/**
* Pass each field to be validated as a seperate argument
* to the constructor of this object. (an array of elements are ok)
*/
public function __construct() {
$required = func_get_args();
if(isset($required[0]) && is_array($required[0])) {
$required = $required[0];
}
if(!empty($required)) {
$this->required = ArrayLib::valuekey($required);
} else {
$this->required = array();
}
parent::__construct();
}
public function useLabels($flag) {
Deprecation::notice('3.2', 'useLabels will be removed from 3.2, please do not use it or implement it yourself');
$this->useLabels = $flag;
return $this;
}
/**
* Clears all the validation from this object.
*/
public function removeValidation(){
$this->required = array();
return $this;
}
/**
* Debug helper
*/
public function debug() {
if(!is_array($this->required)) return false;
$result = "<ul>";
foreach( $this->required as $name ){
$result .= "<li>$name</li>";
}
$result .= "</ul>";
return $result;
}
/**
* Allows validation of fields via specification of a php function for validation which is executed after
* the form is submitted
*/
public function php($data) {
$valid = true;
$fields = $this->form->Fields();
foreach($fields as $field) {
$valid = ($field->validate($this) && $valid);
}
if($this->required) {
foreach($this->required as $fieldName) {
if(!$fieldName) continue;
$formField = $fields->dataFieldByName($fieldName);
$error = true;
// submitted data for file upload fields come back as an array
$value = isset($data[$fieldName]) ? $data[$fieldName] : null;
if(is_array($value)) {
if($formField instanceof FileField && isset($value['error']) && $value['error']) {
$error = true;
} else {
$error = (count($value)) ? false : true;
}
} else {
// assume a string or integer
$error = (strlen($value)) ? false : true;
}
if($formField && $error) {
$errorMessage = _t(
'Form.FIELDISREQUIRED',
'{name} is required',
array(
'name' => strip_tags(
'"' . ($formField->Title() ? $formField->Title() : $fieldName) . '"'
)
)
);
if($msg = $formField->getCustomValidationMessage()) {
$errorMessage = $msg;
}
$this->validationError(
$fieldName,
$errorMessage,
"required"
);
$valid = false;
}
}
}
return $valid;
}
/**
* Add's a single required field to requiredfields stack
*/
public function addRequiredField( $field ) {
$this->required[$field] = $field;
return $this;
}
public function removeRequiredField($field) {
unset($this->required[$field]);
return $this;
}
/**
* allows you too add more required fields to this object after construction.
*/
public function appendRequiredFields($requiredFields){
$this->required = $this->required + ArrayLib::valuekey($requiredFields->getRequired());
return $this;
}
/**
* Returns true if the named field is "required".
* Used by FormField to return a value for FormField::Required(), to do things like show *s on the form template.
*/
public function fieldIsRequired($fieldName) {
return isset($this->required[$fieldName]);
}
/**
* getter function for append
*/
public function getRequired(){
return array_values($this->required);
}
}