Radicore Forum - RDF feed
https://forum.radicore.org/index.php
Validation plug-ins
https://forum.radicore.org/index.phpindex.php?t=rview&goto=264&th=90#msg_264
I'd like to integrate my own validations in the validation class. For example for checking postcodes, telephonenumber formats etc. These are validations that occur over and over in projects and it's a waste to reimplement them in every database class.
It would by nice if you could plug-in validators into the standard validation object.
Maybe it should work this way:
- define in the subtype which validator is to be loaded: subtype="dutch_postcode"
- load the class dutch_postcode-validator in the validation class.
- do the validation with this class
We could setup some kind of repositery for exchanging validator classes between developers.
Maybe you think it's not a good idea to mess with the current validation class. It still would be nice to have a standard way to implement validators in an other way.
--
Jan Alwin de Jong
]]>janalwin2006-09-07T12:57:56-00:00Re: Validation plug-ins
https://forum.radicore.org/index.phpindex.php?t=rview&goto=265&th=90#msg_265
I will have a think about how this may be done to see what the possibilities are, then I will get back to you.
Off the top of my head something like an extra field in the data dictionary to identify a validation method for that field, at at runtime I could instantiate an object from a copy of 'custom_validation_class' which exists in your project directory, then pass that field's value to that method name. How does that sound?]]>AJM2006-09-07T13:34:47-00:00Re: Validation plug-ins
https://forum.radicore.org/index.phpindex.php?t=rview&goto=266&th=90#msg_266
janalwin2006-09-07T13:56:56-00:00Re: Validation plug-ins
https://forum.radicore.org/index.phpindex.php?t=rview&goto=267&th=90#msg_267
If you call a method in a custom validator class by the name defined in the data dictonary, it will be hard to exchange the validator-methods with others, or to put it in other projects without altering working code.
Wouldn't it be a better idea to load a class with a standard interface from a directory with validators.
For example
=== Valdidator class ====
class dutch_postcode {
var $errors=Array();
function validate($fieldname) {
//.. put custom validation code here
}
function getErrors() {
return $errors
}
}
==== validator call code ====
if (isset($fieldspec[$fieldname]['custom_validator'])) {
]]>janalwin2006-09-07T14:49:30-00:00Re: Validation plug-ins
https://forum.radicore.org/index.phpindex.php?t=rview&goto=268&th=90#msg_268
(a) the name of the file containing the class definition
(b) the class name
(c) the method name
I can either have (a) and (b) taken care of by providing a blank custom validation class with a fixed name to which you just add your own methods, or I can allow you to create your own class(es) with your own name(s).
The first option means that all you have to supply in the data dictionary is the method name, while the second option means that you have to supply the file name, the class name and the method name.
The first option means that you can have a separate copy of the custom validation class in your own application subdirectory so that it does not clash with anything being used in any other subdirectories, but it does mean that you can only have one custom validation class per subdirectory. This would mean that merging several different classes into one class would be a manual exercise.
The second option would be more flexible as you could have a range of different methods available in a range of different classes, and you would not have to touch any code. All you would have to do is specify in the data dictionary for each field the name of the file/class/method to be used for validation.
How does that sound?]]>AJM2006-09-07T15:28:03-00:00Re: Validation plug-ins
https://forum.radicore.org/index.phpindex.php?t=rview&goto=269&th=90#msg_269
Every validator could extent this class for example:
class custom_validator {
var $errors
function validate($fieldvalue) {
return;
} // end method
function getErrors() {
return $this->errors
} // end method
} // end class
Examples of emplementations
class dutch_postcode_validator extends validator {
function validate($fieldvalue) {
// check
if (eregi..... {
$this->errors[]="The postcode may only contain... "
} //end if
} // end method
} // end class
class dutch_phonenumber_validator extends validator {
function validate($fieldvalue) {
// check
if (eregi..... {
$this->errors[]="A phonenumber can only contain numbers"
} // end if
if(strlen($fieldvalue)!=10) {
$this->errors[]="A phonenumber must contain 10 characters"
} // end if
} // end method
} // end class
An other way to do it would be to define an interface, but it's only possible in PHP5 so that not an option I think.
I'm relatively new to object oriented programming. But this seems the most flexible method to me.
The indents in the code seem to get lost when I'm posting. Sorry
--
Jan Alwin
]]>janalwin2006-09-07T16:05:05-00:00Re: Validation plug-ins
https://forum.radicore.org/index.phpindex.php?t=rview&goto=270&th=90#msg_270
You are correct in saying that I do not want to use any options that only work in PHP 5.]]>AJM2006-09-07T16:18:51-00:00Re: Validation plug-ins
https://forum.radicore.org/index.phpindex.php?t=rview&goto=271&th=90#msg_271
I understand your point.
Jan Alwin]]>janalwin2006-09-08T08:31:29-00:00Re: Validation plug-ins
https://forum.radicore.org/index.phpindex.php?t=rview&goto=275&th=90#msg_275
Refer to http://www.radicore.org/viewarticle.php?article_id=84 for documentation.]]>AJM2006-09-11T16:36:46-00:00