Symfony 2.3/2.4 Standalone Form Component -
i trying use symfony 2.3 form standalone component use outside of symfony framework.
i tried following url http://n.clavaud.free.fr/blog/index.php?article31/symfony2-standalone-form-component-tutorial
in above url code based on old versions of symfony framework.
i getting problems while using symfony 2.3
i want code same above in url should work on symfony 2.3 or other suggestions?
i using below code
<?php use symfony\component\classloader\universalclassloader; use symfony\component\form\forms; use symfony\component\form\abstracttype; use symfony\component\form\formbuilder; use symfony\component\form\formbuilderinterface; use symfony\component\form\formfactory; use symfony\component\form\formrenderer; use symfony\component\form\formrendererinterface; use symfony\component\form\extension\core\coreextension; use symfony\component\form\extension\csrf\csrfextension; use symfony\component\eventdispatcher\eventdispatcher; use symfony\component\templating\templatenameparser; use symfony\component\templating\templatenameparserinterface; use symfony\component\templating\templatereference; use symfony\component\templating\loader\filesystemloader; use symfony\component\templating\phpengine; use symfony\component\translation\messageselector; use symfony\component\translation\translator; use symfony\component\translation\translatorinterface; use symfony\component\validator\defaulttranslator; use symfony\bundle\frameworkbundle\templating\helper\formhelper; use symfony\bundle\frameworkbundle\templating\helper\translatorhelper; // validation use symfony\component\form\extension\validator\validatorextension; use symfony\component\validator\validator; use symfony\component\validator\mapping\classmetadata; use symfony\component\validator\mapping\classmetadatafactory; use symfony\component\validator\mapping\loader\staticmethodloader; use symfony\component\validator\constraintvalidatorfactory; use symfony\component\validator\constraints constraints; use symfony\component\validator\validation; /** * load , configure autoloader * @see http://symfony.com/doc/2.0/cookbook/tools/autoloader.html */ require_once 'symfony/component/classloader/universalclassloader.php'; $loader = new universalclassloader(); $loader->register(); $loader->registernamespace('symfony', __dir__.'/'); /** * parameters */ $locale = null; /** * entity */ class message { public $sender; public $recipient; public $message; // validation public static function loadvalidatormetadata(classmetadata $metadata) { $metadata->addpropertyconstraint('sender', new constraints\notblank()); $metadata->addpropertyconstraint('sender', new constraints\email()); $metadata->addpropertyconstraint('recipient', new constraints\notblank()); $metadata->addpropertyconstraint('recipient', new constraints\email()); $metadata->addpropertyconstraint('message', new constraints\notblank()); $metadata->addpropertyconstraint('message', new constraints\minlength(10)); } } /** * form class * @see http://symfony.com/doc/2.0/book/forms.html#creating-form-classes */ class messagetype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('sender', 'email') ->add('recipient', 'email') ->add('message', 'textarea'); } public function getname() { return 'message'; } public function getdefaultoptions(array $options) { return array( 'data_class' => 'new\message', ); } } /** * template name parser * @see symfony\bundle\frameworkbundle\tests\templating\helper\fixtures\stubtemplatenameparser * * needed load templates used rendering form items. */ class stubtemplatenameparser implements templatenameparserinterface { private $root; private $roottheme; public function __construct($root, $roottheme) { $this->root = $root; $this->roottheme = $roottheme; } public function parse($name) { list($bundle, $controller, $template) = explode(':', $name); if ($template[0] == '_') { $path = $this->roottheme.'/custom/'.$template; } elseif ($bundle === 'testbundle') { $path = $this->roottheme.'/'.$controller.'/'.$template; } else { $path = $this->root.'/'.$controller.'/'.$template; } return new templatereference($path, 'php'); } } /** * create entity */ $message = new message(); $message->sender = 'mymail@example.com'; /** * build form form factory */ /* $form_factory = new formfactory(array( new coreextension(), // validation new validatorextension( new validator( new classmetadatafactory( new staticmethodloader() ), new constraintvalidatorfactory(), new defaulttranslator() ) ) ));*/ $validator = validation::createvalidator(); $form_factory = forms::createformfactorybuilder() ->addextension(new coreextension()) ->addextension(new validatorextension($validator)) ->getformfactory(); $form = $form_factory->create(new messagetype(), $message); /** * create php template engine */ $root = realpath(__dir__ . '/symfony/bundle/frameworkbundle/resources/views'); $roottheme = realpath(__dir__ . '/symfony/bundle/frameworkbundle/resources'); $templatenameparser = new stubtemplatenameparser($root, $roottheme); $loader = new filesystemloader(array()); $engine = new phpengine($templatenameparser, $loader); /** * helper rendering form items */ /*$form_helper = new formhelper($engine, array( 'frameworkbundle:form', ));*/ $form_helper = new formhelper(new formrendererinterface()); /** * bind engine */ $engine->sethelpers(array( $form_helper, new translatorhelper(new translator($locale, new messageselector())), )); /** * bind submitted data */ $submitted = false; $valid = null; if (isset($_post[$form->getname()])) { $form->bind($_post[$form->getname()]); $submitted = true; // validation if ($valid = $form->isvalid()) { // may want redirect @ state $data = $form->getdata(); echo 'validated success'; var_dump($data); } } /** * create form view */ $form_view = $form->createview(); /** * now, it's time render html! */ header('content-type: text/html; charset=utf-8'); ?> <!doctype html> <html> <body> <form action="" method="post" <?php print $form_helper->enctype($form_view) ?> novalidate="novalidate"> <?php print $form_helper->widget($form_view) ?></div> <input type="submit" /> </form> <?php if ($submitted && $valid) : ?> <p><strong>submitted form valid.</strong></p> <?php endif; ?> <p><em>message object:</em></p> <pre><?php print print_r($message, true); ?></pre> </body> </html>
i getting error
fatal error: cannot instantiate interface symfony\component\form\formrendererinterface in /var/www/new/test.php on line 193
you're trying instantiate interface:
$form_helper = new formhelper(new formrendererinterface());
interfaces not classes , cannot instantiate them (see http://php.net/interface , read on oop).
check out example of using form component standalone: https://github.com/bschussek/standalone-forms
Comments
Post a Comment