You can save the html code in col, and in another col you can save json data for the validation options.
For example:
<input type="text" name="username" />
<input type="password" name="pass" />
And for validation:
$validation = array(
'username' => array(
'regex' => '/\w{6,12}/',
'error' => 'Username must be 6 - 12 chars'
),
'pass' => array(
'func' => 'validate_password',
'error' => 'Week password'
)
);
$validation_json_for_db = json_encode( $validation );
Example usage:
// may be need to receive from db and json_decode parse
$validation = array(
'username' => array(
'regex' => '/\w{6,12}/',
'error' => 'Username must be 6 - 12 chars'
),
'pass' => array(
'func' => 'validate_password',
'error' => 'Week password'
)
);
$valid_errors = array();
foreach( $validation as $key => $val ){
if( isset( $_POST[$key] ) ){
$isValid = false;
if( isset( $val['func'] ) ){
$isValid = call_user_func( $val['func'], $_POST[$key] );
} elseif( isset( $val['regex'] ){
$isValid = preg_match( $val['regex'], $_POST[$key] );
}
if( ! $isValid ){
$valid_errors[$key] = $val['error'];
}
}
}