Sorry for the many questions but I'm new to Symfony. I made a form and all it working well. I a few fields for pictures (Picture1, Picture2, Picture3, etc). I used te sfWidgetFormInputFile and all is working well with that as well. It uploads the file where it needs to and it gives it a unique name.
The problem I'm having is that those fields are set in mySQL to have a default value of "no-pic.png" so that if the user doesn't upload a picture, it will still have something to show in the post. But every time someone uploads a picture, it deletes my no-pic.png file and the posts without a picture show with a blank square. I'm thinking it's because it's set to "delete previous image" before uploading the new one to reduce excess unused pictures on the server.
I'm wondering if there's a way to have it check to see if the value is "no-pic.png" before deleting it from the folder. In case it helps, this is the code I'm using in the form.class file.
$this->widgetSchema['picture1'] = new sfWidgetFormInputFile(array('label' => 'Pictures', ));
$this->validatorSchema['picture1'] = new sfValidatorFile(array('required' => false, 'path' => sfConfig::get('sf_upload_dir').'/car', 'mime_types' => 'web_images', ));
$this->widgetSchema['picture1'] = new sfWidgetFormInputFileEditable(array(
'label' => 'Pictures',
'file_src' => '/uploads/car/'.$this->getObject()->getPicture1(),
'is_image' => true,
'edit_mode' => !$this->isNew(),
'template' => '<div>%file%<br />%input%<br />%delete%%delete_label%</div>',
));
$this->validatorSchema['picture_delete'] = new sfValidatorPass();
Just for reference here is the code I used.
$this->setWidget('logo_image', new sfWidgetFormInputFileEditable(array(
'file_src' => 'uploads/logos/'.$this->getObject()->getFilename(),
'edit_mode' => !$this->isNew()
)));
$validatorOptions = array(
'max_size' => 1024*1024*10,
'path' => sfConfig::get('sf_upload_dir').'/logos',
'mime_type_guessers' => array(),
'required' => false
);
if ($this->isNew())
{
$validatorOptions['required'] = true;
}
$this->setValidator('logo_image', new sfValidatorFile($validatorOptions));
$this->setValidator('logo_image_delete', new sfValidatorString(array('required' => false)));
I found a workaround. I had the page check to see if it was set to no-pic.png and then it displays a no-pic.png in another location. That way when it goes to upload a picture, it won't delete it. :) Thanks for your help though.
Related
I create a script to show the qty_ordered from the table sales_flat_order_item.
But now i want only the qty_ordered when the order is on pending, processing.
But i dont know how i can do that.
I search on the internet but no results.
I create a LeftJoin for this.
$totalOrderedQuery = Mage::getSingleton('core/resource')->getConnection('core_read')
->select()
->from('sales_flat_order_item', array('product_id', 'qty_ordered' => 'SUM(`qty_ordered`)'))
->group('product_id');
$collection->joinField('qty_ordered', $totalOrderedQuery, 'qty_ordered', 'product_id=entity_id', null, 'left');
and i show with
$this->addColumn('qty_ordered',
array(
'header'=> Mage::helper('catalog')->__('test'),
'width' => '70px',
'type' => 'number',
'index' => "qty_ordered"
));
its in the GRID.
can somebody pls help me.
thank you.
I use a form with a multiupload. My addAction save information regarding the downloaded files in the database, and obtains an array lastInsrtId() values. If the upload was successful, I need to be redirected to editAction, where a user can see a list of downloaded files and user can edit file attributes such as title, description and alt attribute for images for each downloaded file using this files list. How do I pass an array of values in the route? Here is my code addAction:
// upload success
$fileIds = $this->getContentService()->makeFiles($parent, $data);
return $this->redirect()->toRoute('sc-admin/file/edit', array('ids' => $fileIds));
Here is the definition for a route that should display a list of files for editing:
'edit' => array(
'type' => 'segment',
'options' => array(
'route' => '/edit[/:ids]',
'defaults' => array(
'controller' => 'sc-file',
'action' => 'edit',
),
),
),
But it generates an error for editAction
rawurlencode() expects parameter 1 to be string, array given
I do not want to use the session whenever the parameters necessary to pass an array of values, because it is a matter solely routing.
Ever seen this url?
http://foo.bar/baz?array(1=>2,3=>4)
Probably not. You got an error message so do as the error message says. This has nothing to do with Zend Framework, this is basic PHP (too many people often forget what's the core...).
array('ids' => serialize($data))
See php:serialize() and php:unserialize()
Im using zend framework 2. I want to call a function in model from zend form.
The situation is Im having a combo box & I need to bind data from database to fill its options and value.
This is my select tag in zend form
$this->add(array(
'name' => 'ddlcountry',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Country',
'value_options' => (here I've to call function),
),
));
For this value option I want to call a function which is in model below is my function in model:
public function fetchcountry()
{
$this->adapter = $this->getServiceLocator()->get('db');
$dbAdapterConfig = $this->adapter;
$dbAdapter = $dbAdapterConfig;
$driver = $dbAdapter->getDriver();
$connection = $driver->getConnection();
$result = $connection->execute("CALL sp_showcountry()");
$statement = $result->getResource();
$resultdata = $statement->fetchAll(\PDO::FETCH_OBJ);
return $resultdata;
}
before you write such a Question, please check at least the first 10 Questions on this Page, as your Question has been asked SEVERAL Times lately ;)
Please refer to my answer provided here:
How to get data from different model for select?
Or refer to my Blogpost, which covers your problem in detail
Zend\Form\Element\Select and Database-Values
Iam new guy to Zend framework and currently Iam working on Zend2...I want to ask about Translator usage in Zend forms....If i want to use translator i directly using for labels in form view i.e.form_view.php like
$this->formLabel()->setTranslator($translator, 'date_of_birth');
But I want to add the translator at the form only i.e.in src/my_module/Form/UserForm.php
like
$this->add(array(
'name' => 'date_of_birth',
'attributes' => array(
'type' => 'text',
'id' => 'date_of_birth',
),
'options' => array(
'label' => 'DateOfBirth',
), //Here there is any option to put translator
));
Please help me...any answer would be help for me like I asked
Thanks in advance
You don't really need to do that. Since the the Translator that is set up using the factory-key translator will automatically be injected into the Form.
The best approach (in my opinion) is to make extensive use of the translator text_domain:
'translator' => array(
'locale' => 'de_DE',
'translation_file_patterns' => array(
array(
'type' => 'phparray',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.php',
'text_domain' => 'MyModuleTextDomain'
),
),
),
With this setup, the Files of your Module will automatically be inserted into the default TranslatorService which every Zend\Form knows of.
So ultimately all you have to do is make the ViewHelpers know of the TextDomain that you are using. And this is done in the following manner:
$this->formLabel()->setTranslatorTextDomain('MyModuleTextDomain');
$this->formButton()->setTranslatorTextDomain('MyModuleTextDomain');
$this->formElementErrors()->setTranslatorTextDomain('MyModuleTextDomain');
You need to do this once inside your respective view.phtml before(!) using the ViewHelpers like $this->formElement($element) or $this->formCollection($form)
And that's really all there is to it. I recall having seen a discussion somewhere about making it easier to pass along Text-Domain-Data, but i can't find it right now. So things may get a little easier in the future ;) For now, 3 lines are all that's needed though!
above answer is quite unnecessary ... as your translator was added automatically to zend form for rendering form labels and ....
only use this code in your module config :
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'phparray',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.php',
),
),
),
if u use the correct view helpers for rendering form elements (or whole form) it will automatically translated
This is not a recommended approach because forms are translated automatically if you have a translator configured (which you do if you are using the Skeleton Application). However, since you asked how to use the translator directly within your form, I will show you how you can do it. Please carefully consider if you really want to do this, as I cannot imagine a use case where it would be necessary.
To do exactly what you were asking, you can inject the translator into your form. You can do this either in your controller or in a factory. I will be using a factory in this example because it is more DRY.
// In your module's config file
'service_manager' => array(
'factories' => array(
'YourModule\Form\YourForm' => function($sm) {
$translator = $sm->get('Translator');
return new \YourModule\Form\YourForm($translator);
},
),
),
Then in your form class, you can do like this:
namespace YourModule\Form;
class RegisterForm extends \Zend\Form\Form {
public function __construct($translator) {
// Do something
$translated_string = $translator->translate('string to translate');
}
}
Then in your controller, you can do like this:
$your_form = $this->servicelocator->get('YourModule\Form\YourForm');
Or if you don't want to use the factory, you can choose to not add it and do like this instead:
$your_form = new \YourModule\Form\YourForm($this->servicelocator->get('Translator'));
I would recommend going with the factory, though.
i am trying to go to an location choosen by my script.
I'm using url() and drupal_goto() to archive this, but the fragment-option named at the url()-documentation-page seems not working like i understand it or more likely drupal_goto() is changing the link.
The link-string i want should look like:
/topsection/section#subsection
but instead i'm getting the hash-sign encoded like
/topsection/section%23subsection
Here is my code:
$section = url( '/topsection/' . 'section', array( 'fragment' => 'subsection', 'alias' => TRUE ) );
drupal_goto( $section );
Any help would be nice!
Thank you.
Ha! just found the solution:
I misunderstood the documentation.
It is correctly telling that i should use drupal_goto() with fragment/anchor passed as option like i would give to url().
This is working:
drupal_goto( '/topsection/' . 'section',
array(
'fragment' => 'subsection',
'alias' => TRUE ) );