Posted: Sat Feb 18, 2006 2:05 pm
Now that I think about it, a class for every field type is not what I meant to say. As you stated an object for every field.Which part is reduntant? Do you mean a "class" for every field or an "object" for every field? Are you talking about the PHP side or the Javascipt side?
Agreed.The thing about "fields" for me is that they potenially need a bunch of stuff for them.
IMHO I don't think this is a very effective way of handling your field values. How aboutFor a form each data field needs a one or more filters, one or more validators, plus error messages, and potentially one or more callbacks that run on some state change (load/submit/accepted). That's just the data fields.
Code: Select all
$fields = array(
'username' => array('isAllLetters', 'isNotNull'),
'email' => array('isEmailFormat')
);
$validated = new fields($fields, $_POST);
class fields extends fieldValidation
{
function __construct($fieldsArray, $source) {
$this->fields = $fieldArray;
$this->validateRules($fieldsArray);
$this->method = $source;
}
function getField($field) {
if (!array_key_exists($field, $this->errors) && isset($this->fields[$field])) {
return $this->fields[$field];
}
return false;
}
function assignRules() {
foreach ($fieldsArray as $field => $rules) {
if (is_array($rule)) {
foreach ($rules as $rule) {
if (!$this->validate($rule)) {
$this->errors[$field][] = $this->getError($rule);
}
}
}
else {
if (!$this->validate($rule)) {
$this->errors[$field][] = $this->getError($rule);
}
}
}
}
}
class fieldValidation
{
function _construct() { }
function validate($field, $rule) {
if (method_exists($this, $rule) && (!empty($this->method[$field])) {
if ($this->$rule($field)) {
return true;
}
$this->addError($field);
}
}
}Yea..If you think about a general architecture -- a parameter could be an Event that triggers an action.
Code: Select all
if ($validated->getField('action') !== false) {
//trigger event
}Your way has no more reusability than my idea.. IMO. Can you show otherwise please?The point of a controller is to create a reusable system rather than custom coding every form.
I prefer to encapsulate all closely related stuff.And for me encapsulating all the stuff for each potential parameter makes sense.
My way or the highway.The question is how to do it.