Symfony Admin Generator Doctrine executeCreate

2019-06-28 05:11发布

问题:

Thanks for any help with this, I'm very new to the Symfony framework so just trying to get my head around it.

I want to be able to intercept a submitted form from the admin area and modify the data.

Here's what I've got so far (in basic form)..

/apps/backend/modules/proposition/actions/action.class.php

class propositionActions extends autoPropositionActions {

  public function executeCreate(sfWebRequest $request) {
    // modify the name
    $name = $request->getParameter('name');
    $name = $name . ' is an idiot';
    $request->setParameter('name', $name);

    return parent::executeCreate($request);
  }

}

My form does contain a name field:

/apps/backend/modules/proposition/config/generator.yml

generator:
  class: sfDoctrineGenerator
  param:
    model_class:           Proposition
    theme:                 admin
    non_verbose_templates: true
    with_show:             false
    singular:              ~
    plural:                ~
    route_prefix:          proposition
    with_doctrine_route:   true
    actions_base_class:    sfActions

    config:
      actions: ~
      form:
        display: [name, icon, overview, published]

I'm not sure if that's the file you need to see of not, but it's definitely in the HTML:

<input type="text" id="proposition_name" name="proposition[name]">

When I submit the form it just saves my name. I want it to save my name appending ' is an idiot'.

Many thanks

回答1:

I think you are on the right track Peter, but modifying the $request too late for it to have any effect.

You could do this sort of modification of inbound data in the "doClean" part of the Form's validator.

Or, if you have special processing to do, it might make more sense to override the generated processForm() function. Just copy it out of cache/frontend/dev/modules/autoProposition/actions/actions.class.php into your apps/backend/modules/proposition/actions/action.class.php and start hacking.



回答2:

Why do want to do it in your action? I think most suitable place is form itself.

You can modify value of any column by adding update*Column method:

class PropositionForm extends BasePropositionForm
{
  public function updateNameColumn($name)
  {
    return $name . ' is an idiot';
  }
}

Note: If you don't want the " is an idiot" string appended in other places your form is used you can subclass the original form and add your method there (i.e. in AdminPropositionForm).

You can change the form used with admin generated module by overloading getFormClass() method in your module's configuration class (propositionGeneratorConfiguration). It should return the name of form class you want to use.

Note 2: You have to accessed form values differently:

$proposition = $request->getParameter('proposition', array());
$name = $proposition['name'];