-
basic usage:, (*9)
$bean = R::dispense('user'); // the redbean model
$required = [
'Name'=>'name', // post key + rule(s)
'Email'=>'email',
'User_Name'=>['rmnl','az_lower'],
'Password'=>'password_hash',
];
$fvm->generate_model($bean,$required); //the magic
R::store($bean);
-
optional parameters:, (*10)
$bean = R::dispense('user'); // the redbean model
$required = [
'Name'=>'name', // post key + rule(s)
'Email'=>'email',
'User_Name'=>['rmnl','az_lower'],
'Password'=>'password_hash',
];
//here we are adding the optional array. these fields are optional, so we raise no exception for missing values.
$optional = [
'username'=>'min' //min is the minimum validation/filter
];
$fvm->generate_model($bean,$required,$optional); //the magic
R::store($bean);
-
custom data source, (*11)
$bean = R::dispense('user'); // the redbean model
$required = [
'Name'=>'name', // post key + rule(s)
'Email'=>'email',
'User_Name'=>['rmnl','az_lower'],
'Password'=>'password_hash',
];
$optional = [
'username'=>'min' //min is the minimum validation/filter
];
// here we add a custom data source
$data = array_merge($_POST,$_GET);
$fvm->generate_model($bean,$required,$optional,$data); //the magic
R::store($bean);
-
manual usage of the methods., (*12)
$unsafeData = 'alfjasldfajsl1000afdasjlkl';
//we can use RedBeanFVM manually too.
$bean->someProperty = $fvm->cast_int($unsafeData);
-
chainable methods with chain(), (*13)
$input = $_POST['username'];
$rules = ['rmnl','az','name'];
$bean->user_name = $fvm->chain($rules,$input);
-
custom filters(named closures), (*14)
$fvm = \RedBeanFVM\RedBeanFVM::getInstance();
$bean = R::dispense('automobile');
$required = [
'make'=>'min',
'model'=>'min',
'year'=>'car_year', //this is custom filter
'vin-number'=>'car_vin_number',// so is this
];
//now we must create the custom filters ...
//create a custom filter to validate the `year` of the automobile
$fvm->custom_filter('car_year',function($input){
if(!preg_match('/^[0-9]{4}$/',$input)){
throw new \exception('Invalid Year entered');
}
return $input;
});
//create a custom filter to validate the vin number of the automobile.
$fvm->custom_filter('car_vin_number',function($input){
// vin numbers are 17 in length alphanumeric
if(!preg_match('/^[0-9A-Za-z]{17}$/',$input)){
throw new \exception('Invalid VIN entered.');
}
return strtoupper($input);//we dont really care if they typed lower case. we can fix it for them.
});
//now we can use our custom filters for year and vin.
$required = [
'make'=>'min',
'model'=>'min',
'year'=>'car_year', //this is custom filter
'vin-number'=>'car_vin_number',// so is this
];
$fvm->generate_model($bean,$required);
-
advanced custom filters., (*15)
- Some functions like
name
accept optional second parameters.
- by design, FVM only accepts 1 argument, the $input to be filtered.
- we can work around this like so:
$min_length = 10;
$max_length = 55;
$fvm->custom_filter('name_custom',function($input) use($fvm,$min_length,$max_length){
return $fvm->name($input,$min_length,$max_length);
});
-
calling custom filter directly on $fvm is possible., (*16)
$fvm->custom_filter('foo',function($input){
return 'foo';
});
$input = 'abcdefg';
$bean->foo = $fvm->foo($input);
-
checkboxes, (*17)
$checkboxes = [
'remember_me'=>1,//the expected value of the input field.
'email_subscribe'=>1,
];
$fvm->checkbox($bean,$checkboxes,$_POST);
-
loading a custom filter class., (*18)
//latest version of RedbeanFVM introduces the ability to load a custom class of filters. this way you
//can write code for your filters in a normal class instead of relying on `RedbeanFVM::custom_filter()`
//at present time, it is only possible to load a single class, in future support for passing an array of classes is possible,
//although personally i believe it to be cleaner to use a single class.
//note: namespaces must be escaped as shown
\RedBeanFVM\RedBeanFVM::configure([
'user_filters'=>'\\App\\Util\\CustomFilters'
]);
-
required and optional can be called directly., (*19)
$required = [...rules];
$optional = [...rules;
$fvm->required($bean,$required,$_POST);
$fvm->optional($bean,$optional,$_POST);
-
file uploads (coming soonish), (*20)
-
all config options and their default values., (*21)
\RedbeanFVM\RedbeanFVM::configure([
'password'=>[
'cost'=>12, //cost of password_hash
'algo'=>PASSWORD_DEFAULT //password algo. see PHP Manual entry for password_hash() for more info.
],
'locale'=>'\\RedBeanFVM\\Locale\\US', //default locale is this.
'user_filters'=>'',//load a custom class of filters. filter methods must be public.
'optional_defaults'=>false //require that optional parameters are provided a default. see example 13 for more info.
]);
-
optional defaults explained., (*22)
//say you want a field to be optional, but it needs a default value if not specified right?
//FVM will expect that you provide an array of rules, with the last rule being the default value like so
$optional = [
'about_me'=>['paragraph','no info provided'],
'phone_number'=>['us_phone','not provided']
];
//as you can see its pretty convenient to type and should be simple to get the hang of.
//if users don't like this, please open an issue and we can think of an alternative way to handle it.
//this option is disabled by default.