سؤال

I have a form for an object called AccountImport. This form lives in an admin-generated module. In addition to the fields that map directly to this object's attributes, I need a couple extra fields.

If I just add the fields to the AccountImport form, it won't save correctly because the form will no longer match the AccountImport object.

If I create a template manually and splice the extra fields in that way, I'm throwing away all the stuff the admin generator gives me for free (i.e. formatting, "Back to list" button, save buttons).

What's a "good" way to do what I'm trying to do?

هل كانت مفيدة؟

المحلول

If you define additional fields in generator.yml, you can override one of the admin generator actions to handle the fields however you want.

Look at the generated actions.class.php in cache/YOURAPP/YOURENV/modules/autoYOURMODULE/actions/actions.class.php . You can override any of those functions with your own in apps/YOURAPP/modules/YOURMODULE/actions/actions.class.php, because it inherits from that cached file. When you make changes to generator.conf, the cached file is updated but your code will still override it. You probably want to override processForm().

I have an example of this in step 5 at this blog post:

protected function processForm(sfWebRequest $request, sfForm $form)
{
  $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

  if ($form->isValid())
  {
$notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

// NEW: deal with tags
if ($form->getValue('remove_tags')) {
  foreach (preg_split('/\s*,\s*/', $form->getValue('remove_tags')) as $tag) {
    $form->getObject()->removeTag($tag);
  }
}
if ($form->getValue('new_tags')) {
  foreach (preg_split('/\s*,\s*/', $form->getValue('new_tags')) as $tag) {
    // sorry, it would be better to not hard-code this string
    if ($tag == 'Add tags with commas') continue;
    $form->getObject()->addTag($tag);
  }
}

try {
  $complaint = $form->save();
  // and the remainder is just pasted from the generated actions file

When I realized I could read the generated files in the cache to see exactly what the admin generator was doing, and that I could override any part of them, it made me a lot more productive with the admin generator.

نصائح أخرى

I asume you have added the extra fields as widgets to your form object, but have you also added their validators?

No matter which form fields you include in the form object, as long as the generator.yml file doesn't override the settings of the form (ie you don't set any value for the [new|form|edit].display key in that file) the object should get successfully saved on valid input.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top