简体   繁体   中英

Symfony 1.4 Custom Form

Just when I think I've got this framework down .... something that SEEMS like it should be so simple is thwarting my LIFE!

Here's What I got:

PageMeta:
  connection: doctrine
  actAs: { Timestampable: ~ }
  tableName: page_meta
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    site_id:
      type: integer(4)
      notnull: true
    meta_type_id:
      type: integer(4)
      notnull: true
    page_name_id:
      type: integer(4)
      notnull: true
    value:
      type: string(65535)
      default: ''
      notnull: true

The relational id's in all cases are just id | name tables and are not important. I am filling those just fine, what I need is this.

I want to create a Form with an embedded form for each page_name (there are 9 currently). So I created a custom form

class MagicForm extends BaseForm
{
  public function configure()
  {
    CODE TO GET ALL MY NEEDED VARIABLE and MODELS
    $forms = new sfForm();
       $x = 0;

       if(count($pageMetas) < 1)
       {
         foreach($pages as $page)
         {
           $pageMeta = new PageMeta();
           $pageMeta->PageName = $page;
           $pageMeta->setMetaTypeId(1);
           $pageMeta->setSiteId($sid);
           $pageMetas[] = $pageMeta;
         }
       }

       foreach($pageMetas as $meta)
       {
         $metaForm = new PageMetaForm($meta);
         $metaForm->widgetSchema['value']->setLabel($meta->PageName->getName());
         $metaForm->setDefault('value', $meta->getValue());
         $metaForm->widgetSchema['page_name_id'] = new sfWidgetFormInputHidden();
         $forms->embedForm($x,$metaForm);
         $x++;
       }

       $this->embedForm('TitleTags',$forms);
     }
  }

this works just fine, I load my form and I get the desired 9 forms with the Value field all ready to be populated.

Here is where my issue lies, I can't get them to save! I've tried processing them via binding the form and saving it, I get crsf_token errors, and others.

I've tried just simply grabbing the post values and creating a new PageMeta model and simply saving it.

i.e.
  $pageMeta = new PageMeta();
  $pageMeta->setXXX($request->getPostParamater(XXX);
  etc;

but after initial save I am getting duplicates. And Yes I am passing the Id and yes I even tried doing the whole $pageMeta->setNew(false); When it's not a new value, this gives me an error: Unknown record property / related component "new" on "PageMeta" however I can comfirm PageMeta is indeed a PageMeta object....

Anyway, in the end I want to have multiple (NOT DYNAMIC I know exactly how many I need) of the same form into a single page and then save them all at once.

Thank you reading .. now ... GO!

Forms aren't that nice in symfony. For me it sounds that the crsf_token problems comes with the name oder id in the template.

eg in your lib/form/(/ ?pagemeta.php /) => $this->wigetSchema....is important!!!

public function configure() {
    $this->setWidgets(array(
        'firstname' => new sfWidgetFormInputText(array(), array('size' => '40', maxlength' => '100')),
 ));

$this->widgetSchema->setNameFormat('pagemeta[%s]');
}

in the template Success.php:

<?php include_partial('global/standardform',array('form' => $formPageMeta, 'formTarget' => $formTargetPageMeta)) ?>

and in your action.calls

$this->formPageMeta = new PageMetaForm(array(
                'firstname' => $this->endkunde->getFirstname(),
));

$this->formTargetPageMeta = 'test/index'; //module/action


    if ($request->isMethod('post')) {
        $this->formPageMeta->bind($request->getParameter('pagemeta')); //getParameter is from the action module/action

        if ($this->formPageMeta->isValid()) {
            $formData = $this->formPageMeta->getValues();
            $metaForm->widgetSchema['value']->setLabel($meta->PageName->getName());
        }
    }

But I do hate forms too, but I guess this is the problem that you didn't set a widgetSchema. Hope i didn't do a mistake!

Craphunter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM