Hi,

I create a form with one embedded form and one upload file field at
the main form, at the same time, I need to change the validation for
the embedded form.

I use the sfShop Open source.

I tried to use the address_book plugin. they use the "state_id" and
"state_title" based on the country whether has the states listing.

Then I met the troubles for saving object , updating object and
uploading file, it spent my many hours to make them work together.

My ProfileForm.class.php :

class ProfileForm extends BaseProfileForm
{
  protected $member_id;

  public function configure()
  {
    //    parent::configure();

    $this->member_id = sfContext::getInstance()->getUser()->getUserId
();

    $arrayCategories = array();

    $categories = CategoryPeer::getAllPublic();

    if ($categories !== null) {
      $arrayCategories[] = ' - Select category - ';
      foreach ($categories as $category) {
        array_push($arrayCategories, $category);
      }
    }

    $this->setWidgets(
    array(
                        'title'           => new sfWidgetFormInput(),
                        'logo_path'       => new sfWidgetFormInputFile
(
 
array('label' => 'Company logo',
                                                               )),
........

    )
    );

    $this->validatorSchema['category_id'] = new sfValidatorPropelChoice
(array
    ('model' => 'category', 'column' => 'id', 'required' => true)
    );


    $this->widgetSchema->setLabels(array(
      'category_id'    => 'Category',
      'message'        => 'Weclome Message',
      'announcement'   => 'Ads Details',
    ));

    $this->validatorSchema['logo_path'] = new sfValidatorFile(array(
      'required'   => true,
      'path'       => sfConfig::get('sf_upload_dir').'/logo/',
      'mime_types' => 'web_images',
    ));

    $address_book = $this->getObject()->getAddressBook();
    $address_book_form = new ProfileAddressBookForm($address_book);
    $this->embedForm('address', $address_book_form);


    $this->getWidgetSchema()->setNameFormat('data[%s]');
    $this->defineSfsListFormatter();
    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);

  }

  public function bind(array $taintedValues = null, array
$taintedFiles = null)
  {
     // $ret = parent::bind($taintedValues, $taintedFiles);  // If I
uncomment it, It will call the sfValidation of "state_title" or
"state_id" is invalid.

    if (isset($taintedValues['address']['country_has_states']) &&
$taintedValues['address']['country_has_states'] == 1)
    {
      unset($this->validatorSchema["address"]['state_title']);
    }
    else if(isset($taintedValues['address']['country_has_states']))
    {
      //      unset($taintedValues['address']['state_id']);
      unset($this->validatorSchema["address"]['state_id']);
    }

    $ret = parent::bind($taintedValues, $taintedFiles);  // If I put
parent:bind at here, the validation behavious is ok.

    return $ret;
  }

  protected function doSave($con = null)
  {

    $file = $this->getValue('logo_path');
    if(is_object($file)) {
      if (file_exists($this->getObject()->getLogoPath()))
      {
        unlink($this->getObject()->getLogoPath());
      }
      $filename = sha1($file->getOriginalName()).$file->getExtension
($file->getOriginalExtension());

      $file->save(sfConfig::get('sf_upload_dir').'/logo/'.$filename);

      //    echo $filename;
      $this->getObject()->setLogoPath($filename);
    }
//    $this->getObject()->save();         // this isn't work to save
object to database
//    return parent::doSave($con);     // this isn't still  work to
save object to database
  }
}

At my actioin class :


  public function executeEdit(sfWebRequest $request)
  {
    $member_id = $this->getUser()->getUserId();
    $this->profile  =  $this->getOrCreateMemberProfile($member_id);

    $this->form = new ProfileForm($this->profile);


    if ($request->isMethod('post')) {
      $this->form->bind($request->getParameter($this->form->getName
()), $request->getFiles($this->form->getName()));
      if ($this->form->isValid())
      {
        $this->form->save();   // this only trigger doSave() at form
file.
        $this->profile = $this->form->updateObject($request-
>getParameter($this->form->getName()));
        $this->profile->save();  // for saving the profile object with
address_book object to database.
        $this->redirect('@profile?action=show');
      }
    }
  }


I hope some one can hope me to figure out why I need to save the
object twice time.

It took my many time to figure out how it work. but it must be
improved .

Thanks in advance.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to