Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在对象上下文中将字符串转换为有效的php代码?_Php_Silex_Formbuilder - Fatal编程技术网

在对象上下文中将字符串转换为有效的php代码?

在对象上下文中将字符串转换为有效的php代码?,php,silex,formbuilder,Php,Silex,Formbuilder,我正在应用程序中使用Silex FormBuilder。建筑商的结构如下所示: $form = $app['form.factory']->createBuilder('form') ->add('name', 'text', array( 'constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 5))) )) ->add('email', 'tex

我正在应用程序中使用Silex FormBuilder。建筑商的结构如下所示:

$form = $app['form.factory']->createBuilder('form')
->add('name', 'text', array(
        'constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 5)))
))
->add('email', 'text', array(
    'constraints' => new Assert\Email()
))
->add('gender', 'choice', array(
    'choices' => array(1 => 'male', 2 => 'female'),
    'expanded' => true,
    'constraints' => new Assert\Choice(array(1, 2)),
))
->getForm();
$form = $app['form.factory']->createBuilder('form')
foreach ($definitions as $definition) {
  something to do with the $definition;
}
->getForm();
假设我将从块构建这样的构建器,这些块作为一个整体存储在数据库中。我知道,在我创建的任何形式中,电子邮件字段的定义总是相同的。所以我会从数据库中获取定义,并将其用于创建表单

->add('email', 'text', array(
'constraints' => new Assert\Email()))
当我从数据库(在我的例子中是posgresql)中得到这个时,它将是一个字符串(因为数据库字段是文本类型)。
我的问题:有没有可能将它转换为有效代码

我想到了这样的事情:

$form = $app['form.factory']->createBuilder('form')
->add('name', 'text', array(
        'constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 5)))
))
->add('email', 'text', array(
    'constraints' => new Assert\Email()
))
->add('gender', 'choice', array(
    'choices' => array(1 => 'male', 2 => 'female'),
    'expanded' => true,
    'constraints' => new Assert\Choice(array(1, 2)),
))
->getForm();
$form = $app['form.factory']->createBuilder('form')
foreach ($definitions as $definition) {
  something to do with the $definition;
}
->getForm();
但不是foreach createBuilder期望->添加。。。方法,这就是我卡住的地方

最好的问候,
卡米尔

/*Concidering $definitions = array("$form->add('email', 'text', array(
        'constraints' => new Assert\Email()
    ));"); 
*/
$form = $app['form.factory']->createBuilder('form');
foreach($definitions as $definition){
    eval($definition);
}
$form->getForm();
我不建议你像这样使用eval。。。您最好存储要传递给函数的参数以使其工作。。。您还可以创建一个函数或类来处理电子邮件等典型元素,因此您只需要引用数据库中的输入类型,有点像这样:

function addFormElement(&$form,$type){
    switch($type){
        case 'email':$form->add('email', 'text', array(
                    'constraints' => new Assert\Email()
                ));
        break;
    }
}
/*Concidering $definitions = array("email"); 
*/
$form = $app['form.factory']->createBuilder('form');
foreach($definitions as $definition){
    addFormElement($form,$definition);
}
$form->getForm();

谢谢我想了解一下eval,但不了解自定义函数。我之所以尝试做这件奇怪的事情:-)是为了避免创建一个相当复杂的定义生成器——我想让它成为动态的。当我添加一个新的字段类型时,我希望在不编辑源代码的情况下从应用程序中执行此操作。因此,我认为用户输入是不可避免的,但唯一创建新定义的人是我:-)我想连接一个JS编辑器并在我的应用程序中创建一个定义。有没有聪明的方法让eval-see使用子句?电子邮件只是一个例子。我的应用程序将处理各种电子产品,构建器中的“name”元素引用数据库中的其他属性form_builder的定义对于“processor”来说只是众多参数中的一个。很抱歉,这里的混乱:关于查看
use
子句——我应该使用类定义的完整路径。如果您真的想从web界面添加PHP,我建议您将其写入PHP文件,而不是eval,您可以将其包括在内。。。将定义存储在数据库中可能很容易,因此您可以添加、编辑和删除表单类型。每次修改时,PHP文件都会重新生成。保持两个功能分开。