2012-04-10 23 views
11

Tôi đang cố gắng hiển thị biểu mẫu Symfony bằng cách sử dụng lệnh ghép trong ứng dụng Silex. Tôi đang sử dụng thư viện Form Symfony 2, Symfony Bridge Twig Forms Extension và Twig.Lỗi khi sử dụng biểu mẫu Symfony/Silex & Twig

Đây là tập tin bootsrap tôi:

require_once __DIR__ . '/../app/silex.phar'; 
$app = new Silex\Application(); 

$app['debug'] = true; 

// Set up the autoloader 
require_once __DIR__ . '/../app/vendor/.composer/autoload.php'; 

// Load Configuration into container 
$parser = new Symfony\Component\Yaml\Parser(); 
$config = $parser->parse(file_get_contents(__DIR__ . '/../app/config/config.yml')); 
$app['config'] = $config; 

// Set up Symfony bridge (nice twig functions) 
$app->register(new Silex\Provider\SymfonyBridgesServiceProvider(), array(
'symfony_bridges.class_path' => __DIR__ . '/vendor/symfony/twig-bridge', 
)); 

// Set Up Templating 
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__ . '/../app/views', 
'twig.class_path' => __DIR__ . '/../app/vendor/twig/twig/lib', 
)); 

// Set Up Database 
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(
    'driver' => $app['config']['db']['driver'], 
    'host' => $app['config']['db']['host'], 
    'user' => $app['config']['db']['user'], 
    'password' => $app['config']['db']['password'], 
    'dbname' => $app['config']['db']['dbname'] 
), 
'db.dbal.class_path' => __DIR__ . '/vendor/doctrine/doctrine-dbal/lib', 
'db.common.class_path' => __DIR__ . '/vendor/doctrine/doctrine-common/lib', 
)); 

// Set up the Symfony forms component 
$app->register(new Silex\Provider\FormServiceProvider()); 

// Routes 
$app->get('/', function() use($app) 
{ 
    return $app['twig']->render('index.twig', array()); 
}); 

$app->get('/info', function() use($app) 
{ 
    return $app['twig']->render('info.twig', array()); 
}); 

$app->get('/register', function() use($app) 
{ 

    $builder = $app['form.factory']->createBuilder(new MppInt\Form\Type\RegisterType()) 
    ->add('id', 'text') 
    ->add('firstName', 'text') 
    ->add('lastName', 'text') 
    ->add('email', 'password') 
    ->add('password', 'text'); 
    $form = $builder->getForm(); 
    return $app['twig']->render('register.twig', array('form' => $form->createView())); 
}); 


$app->run(); 

tôi nhận được lỗi sau:

Twig_Error_Runtime: Biến "khuyết tật" không tồn tại.

Các vết đống là rất lớn nhưng đây là hai mục đầu tiên:

  1. in /nfs/personaldev/bwaine/mpp-audit/app/vendor/twig/twig/lib/Twig/Template.php line 303

  2. at Twig_Template->getContext(array('value' => '', 'attr' => array(), 'form' => object(FormView), 'id' => 'register_firstName', 'name' => 'firstName', 'full_name' => 'register[firstName]', 'errors' => array(), 'read_only' => false, 'required' => true, 'max_length' => null, 'pattern' => null, 'size' => null, 'label' => 'Firstname', 'multipart' => false, 'types' => array('field', 'text'), 'type' => 'text'), 'disabled') in /nfs/personaldev/bwaine/mpp-audit/app/vendor/twig/twig/lib/Twig/Environment.php(314) : eval()'d code line 986

Có ai có bất kỳ ý tưởng whats going on?

EDIT -

Tôi nghĩ biến 'đã tắt' nằm trong mẫu mặc định.

Từ vendor/symfony/cành cầu/Symfony/Bridge/cành/Resources/views/Form/form_div_layout.html.twig

{% block widget_attributes %} 
{% spaceless %} 
id="{{ id }}" name="{{ full_name }}"{% if read_only %} readonly="readonly"{% endif %}{%  if disabled %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif  %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{  pattern }}"{% endif %} 
    {% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue}}" {% endfor %} 
{% endspaceless %} 
{% endblock widget_attributes %} 

Theo yêu cầu - register.twig

register.twig

{% extends "base.twig" %} 

{% block title %}Welcome to Nuts.co.uk{% endblock %} 
{% block head %} 
{{ parent() }} 
{% endblock %} 
{% block content %} 

<div class="row"> 
<div class="span12"> 
    <h2>Register</h2> 
    <p> 
     Register for Nuts.co.uk and we'll give you free access to cool stuff 
     in addition you can subscribe to our premium content. 
    </p> 



    <form action="/contact" method="post"> 
     <fieldset > 
      <legend>Contact</legend> 

       {{ form_errors(form) }} 
       {{ form_row(form.firstName) }} 
       {{ form_row(form.lastName) }} 
       {{ form_row(form.email) }} 
       {{ form_row(form.password) }} 

      <button type="submit" class="btn btn-info">Send</button> 

     </fieldset> 
    </form> 
</div> 
</div> 


{% endblock %} 
+1

Vui lòng cung cấp nguồn 'register.twig'. – igorw

Trả lời

3

Dường như bạn đang sử dụng biến bị tắt trong mẫu cành (như {{disabled}} hoặc trong câu lệnh if) có nghĩa là bạn không chuyển sang mẫu. Trong mã được dán ở trên, tôi không thấy bất kỳ biến nào có tên 'bị vô hiệu hóa' đang được chuyển đến ...

+0

Hey Skoop - có vẻ như biến 'mặc định' nằm trong mẫu mặc định được sử dụng để hiển thị biểu mẫu. Tôi cho rằng nó sẽ tự động được đặt ở đâu đó. –

Các vấn đề liên quan