Symfony 1.4 form checkbox array to string conversation - php

I have following error during creating checboxes in Symfony 1.4 app.
Checkboxes are rendered but with "Array to string conversation" error.
Below is my code.
Form
$this->setWidget('emails', new sfWidgetFormChoice([
'label' => 'Emails',
'expanded' => true,
'multiple' => true,
'choices' => array('test', 'test2'),
]));
Render
<div class="container emails">
<fieldset>
<legend>
<?php echo $form['emails']->renderLabel(); ?>
</legend>
<?php echo $form['emails']->render(); ?>
<?php echo $form['emails']->renderError(); ?>
</fieldset>
</div>
Error
Notice: Array to string conversion in /var/www/html/symfony/1_4_1/lib/widget/sfWidgetFormSelectCheckbox.class.php on line 103
Notice: Array to string conversion in /var/www/html/symfony/1_4_1/lib/widget/sfWidgetFormSelectCheckbox.class.php on line 10
My Php version is 5.5.8
Symfony version 1.4.19
I know that the best way would be move app to newest symfony version, but this application is too large to rewrite.
Anyone have idea how to solve it?
//Edit
I noticed that if I change code to
$arr = [];
array_push($arr, 'test');
array_push($arr, 'test2');
$this->setWidget('emails', new sfWidgetFormSelectCheckbox([
'label' => __('Adresy email'),
'choices' =>$arr,
]));
it returns any errors, but if I add one more value to array, errors shows again.
Whole class code
class EmailFooterGeneratorForm extends BaseForm
{
/**
* configure...
*
* #param mixed $data
*/
public function configure()
{
sfContext::getInstance()->getConfiguration()->loadHelpers('I18N');
$this->setWidget('regards', new sfWidgetFormInputText([
'label' => __('Treść pozdrowień'),
'default' => 'Pozdrowienia/Best regards',
], [
'size' => 45
]));
$this->setWidget('emails', new sfWidgetFormChoice([
'label' => __('Adresy email'),
'choices' => $this->getDefault('phones'),
]));
$this->setWidget('phones', new sfWidgetFormChoice([
'label' => __('Numery telefonu'),
'expanded' => true,
'multiple' => true,
'choices' => $this->getDefault('phones'),
]));
$this->setWidget('employment', new sfWidgetFormSelectRadio([
'label' => __('Zatrudnienie'),
'choices' => $this->buildEmployment($this->getDefault('employment'))
]));
$this->setWidget('certyfications', new sfWidgetFormChoice([
'label' => __('Certyfikaty'),
'multiple' => true,
'expanded' => true,
'choices' => ['AEO', 'TÜV Rheinland', 'FSC']
]));
$templateChoices = [
'' . __('Szablon') . ' 1 ',
'' . __('Szablon') . ' 2 '
];
$this->setWidget('templates', new sfWidgetFormSelectRadio([
'label' => __('Szablony'),
'choices' => $templateChoices
]));
$this->setWidget('www', new sfWidgetFormInputText([
'label' => __('Strona wwww'),
'default' => 'www.fakro.com'
]));
$this->setValidators([
'regards' => new sfValidatorString(
['max_length' => 50, 'min_length' => 12],
['required' => __('Wymagane'),
'min_length' => __('Treść pozdrowień musi mieć przynajmniej %min_length% znaków.'),
'max_length' => __('Treść pozdrowień może mieć maksymalnie %max_length% znaków.')
]
),
'emails' => new sfValidatorChoice(
['choices' => array_keys($this->getDefault('emails')), 'multiple' => true],
['required' => __('Wymagane')]
),
'employment' => new sfValidatorChoice(
['choices' => array_keys($this->getDefault('employment'))],
['required' => __('Wymagane')]
),
'templates' => new sfValidatorChoice(
['choices' => array_keys($templateChoices)],
['required' => __('Wymagane')]
),
'www' => new sfValidatorRegex(
['pattern' => '/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/'],
['invalid' => __('Niepoprawny adres strony wwww')]
)
]);
$this->validatorSchema['phones'] = new sfValidatorString(['required' => false]);
$this->validatorSchema['certyfications'] = new sfValidatorString(['required' => false]);
}
private function buildEmployment($employment)
{
$arr = [];
foreach ($employment as $key) {
$str =
"<div style='margin-left: 30px'>" .
__('Firma') . ": " . $key['company_name'] . "<br>" .
__('NIP') . ": " . $key['nip'] . "<br>" .
__('REGON') . ": " . $key['regon'] . "<br>" .
__('Miasto') . ": " . $key['city'] . "<br>" .
__('Ulica') . ": " . $key['street'] . "<br>" .
__('Kod') . ": " . $key['postal'] . "<br>" .
__('Kraj') . ": " . $key['country'] . "<br>" .
__('Wydział') . ": " . $key['depertment_name'] . "<br>" .
__('Stanowisko') . ": " . $key['job_name'] . "<br>
</div>"
;
$arr[] = $str;
}
return $arr;
}
}

It is a bit late, but since I was having this issue and someone else may need it, I found that the issue was that I was using a sfWidgetFormChoice set to be multiple, but I didn't tell it to the validator.
The solution is just to pass the option to it as well.
End up having something like
$this->validatorSchema['choices_name'] = new sfValidatorChoice([
'required' => false,
'multiple' => true,
'choices' => $choices
]);
I'm using it in a Form class, that's why you see $this->validatorSchema, if you are using it anywhere else, like in the OP case, just add that 'multiple' => true as option in the options array.

Related

CRUD generator yii2 error

I have error "syntax error, unexpected '}'" in yii2 CRUD generator. I created controller CountryController in basic/controllers path, model Country in basic/models path and folder country in views. I'm trying to make CRUD operation with parameters:
Model Class : app\models\Country
Search Model Class : app\models\CountrySearch
Controller Class : app\controllers\CountryController
View path : #app/views/country
and have error. I cant find solution in google and dont know what to do.
ERROR:
in W:\XAMPP\htdocs\basic\vendor\yiisoft\yii2-gii\generators\crud\default\views\_search.php at line 28
19202122232425262728293031323334353637
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-search">
<?= "<?php " ?>$form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
<?php if ($generator->enablePjax)
?>'options' => [
'data-pjax' => 1
],<?php } >
]); ?>
<?php
$count = 0;
foreach ($generator->getColumnNames() as $attribute) {
if (++$count < 6) {
echo " <?= " . $generator->generateActiveSearchField($attribute) . " ?>\n\n";
} else {
echo " <?php // echo " . $generator->generateActiveSearchField($attribute) . " ?>\n\n";
2. yii\base\ErrorHandler::handleFatalError()
$_GET = [
'r' => 'gii/default/view',
'id' => 'crud',
];
$_POST = [
'_csrf' => 'ZFFUX0VCUjgAMhwAKDgwZ1UQFw8dexZPLwtlZhEMF2AJAh0ZEi8QFQ==',
'Generator' => [
'modelClass' => 'app\\models\\Country',
'searchModelClass' => 'app\\models\\CountrySearch',
'controllerClass' => 'app\\controllers\\CountryController',
'viewPath' => '#app/views/country',
'baseControllerClass' => 'yii\\web\\Controller',
'indexWidgetType' => 'grid',
'enableI18N' => '0',
'enablePjax' => '0',
'messageCategory' => 'app',
'template' => 'default',
],
'preview' => '',
];
$_COOKIE = [
'Phpstorm-b0412478' => '63e8918e-ac29-43de-9816-571b71512aad',
'_csrf' => 'e3a51f05887c990dee11a82408437a3e87c4de7d372dba216a0834374d5b602ca:2:{i:0;s:5:"_csrf";i:1;s:32:"dcH_mzb_1ACPX9DwKZ19TNEXmSIFWmB-";}',
'PHPSESSID' => '2a91078gc1nh6q5br71p0hkuu6',
'_identity' => '8142bf058f7de9bda458b829902ac7db3b69c135c0c908aaaefe8bf2105e8d62a:2:{i:0;s:9:"_identity";i:1;s:28:"["100","test100key",2592000]";}',
];
You close if condition ,where it is open,try below one
<?= "<?php " ?>$form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
<?php if ($generator->enablePjax) {
?>'options' => [
'data-pjax' => 1
],<?php } >
]); ?>

Why do I get an error when adding a category programmatically in Prestashop?

I am trying to write a script that imports my categories from a XML into prestashop. There is one problem, the script does not add the category and stops working when it reaches ->add();
I'm trying to find the problem, but I really don't know what to do anymore.
Here is my script:
if ($cat == "") {
$category = new Category;
$category->active = 1;
$category->id_parent = 3;
$category->name[1] = $product->category_name;;
$category->link_rewrite[1] = Tools::link_rewrite($product_xml->category_name);
echo "<br />name of new category = $product->category_name <br /> <br />";
$category->add();
$G_array[] = $category->id;
$G_cat = $G_cat . "\r\n" . 'Category: ' . $category->name[1].' with the id: '.$category->id.' Created'; //adding new category to var, to be displayed at the index page.
}else{
I am using prestashop 1.6, I hope that someone can explain to me what I am doing wrong..
It works for me
$Category = new Category();
$Category->name = [(int)Configuration::get('PS_LANG_DEFAULT') => 'nome'];
$Category->link_rewrite = [(int)Configuration::get('PS_LANG_DEFAULT') => 'link_rewrite'];
$Category->description = [(int)Configuration::get('PS_LANG_DEFAULT') => 'descrizione'];
$Category->active = 1;
$Category->id_parent = 3;
$Category->add();
for update
$Category = new Category($id_category);
$Category->name = [(int)Configuration::get('PS_LANG_DEFAULT') => 'nome'];
$Category->link_rewrite = [(int)Configuration::get('PS_LANG_DEFAULT') => 'link_rewrite'];
$Category->description = [(int)Configuration::get('PS_LANG_DEFAULT') => 'descrizione'];
$Category->active = 1;
$Category->id_parent = 3;
$Category->update();
you can check if the category is valid with this function
Validate::isLoadedObject($Category) //true or false
remember prestashop checks whether the values of the changes are valid
'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 128),
'link_rewrite' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isLinkRewrite', 'required' => true, 'size' => 128),
'description' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
isCatalogName, isLinkRewrite, isCleanHtml these are controls insertion
You must delete character not valid in link_rewrite. Delete punctuation marks, vowels with accents, brackets, ...
In prestashop code there is a function to convert a string to link_rewrite.
Rarely does the array starts at 1... Change it:
$category->name[0]=...
$category->link_rewrite[0]=...
PD: The best way to manipulate data in prestashop is the Rest API

symfony 1.4 file uploads and templates

I successfully saved the path of photo into mysql database.My question is,how to render correctly in templates?I tried
<td width="129" rowspan="5"><img src= "/uploads/photos<?php echo $applicant->photo?>"width="129" height="129"/></td>
Not working.
In my action, i tried
$photo = $this->form->getValue('photo');
$filename = 'photo'.sha1($photo->getOriginalName());
$extension = $photo->getExtension($photo->getOriginalExtension());
//$photo->save(sfConfig::get('sf_upload_dir').'/' . $filename . $extension);
$photo->save(sfConfig::get('sf_upload_dir') . '/'.$photo) . date('Ym') . '/' . $filename . $extension);
and in my form i tried
$this->widgetSchema['photo'] = new sfWidgetFormInputFileEditable(array(
'label' => 'Photo',
'file_src' => '/uploads/photos/'.$this->getObject()->getPhoto(),
'is_image' => true,
'edit_mode' => !$this->isNew(),
'template' => '<div class="sublabel">%file%<br />%input%<br />%delete% %delete_label%</div>',
));
$this->validatorSchema['photo'] = new sfValidatorFile(array(
'required' => false,
'path' => sfConfig::get('sf_upload_dir').'/photos',
'mime_types' => 'web_images',
));

Gravity Forms - gform_after_submission not working

gform_after_submission is getting called in my page, but the $entry and $form objects are null. Is there any reason why that would be happening? Based on the output of the logs, I know the code runs, but can't figure out why the $entry argument is null.
add_action('gform_after_submission_2', 'post_to_third_party', 10, 2);
function post_to_third_party($entry, $form) {
error_log("Posting comments form");
$post_url = 'https://api.club-os.com/prospects?clubLocationId=686';
$body = array(
'first_name' => $entry['7.3'],
'last_name' => $entry['7.6'],
'email' => $entry['6'],
'mobilePhone' => $entry['8']
);
error_log('Before Post to' . $post_url);
$args = array(
'headers' => array('Authorization' => 'Basic ' . base64_encode( 'username' . ':' . 'password' )),
'body' => $body,
'sslverify' => false
);
foreach ($body as $key => $value) {
error_log($value . " in " . $key . ", ");
}
$request = new WP_Http();
$response = $request->post($post_url, $args);
}
Figured it out.
'first_name' => $entry['7.3'],
'last_name' => $entry['7.6'],
The 7.3 and 7.6 were incorrect indexes to the $entry object. I just used '7' and the split function to get it to work correctly.

zf2 how to upload files, more than 1 file

I refer to http://samsonasik.wordpress.com/2012/08/31/zend-framework-2-creating-upload-form-file-validation/ and follow this, I can upload 1 file successfully by using rename filter in ZF2.
However when I use this way to upload 2 files, it goes wrong. I paste my code as following:
$this->add(array(
'name' => 'bigpicture',
'attributes' => array(
'type' => 'file'
),
'options' => array(
'label' => 'Big Pic'
)
));
$this->add(array(
'name' => 'smallpicture',
'attributes' => array(
'type' => 'file'
),
'options' => array(
'label' => 'Small Pic'
)
));
<div class="row"><?php echo $this->formRow($form->get('smallpicture')) ?></div>
<div class="row"><?php echo $this->formRow($form->get('bigpicture')) ?></div>
$data = array_merge(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
$form->setData($data);
if ($form->isValid()) {
$product->exchangeArray($form->getData());
$picid = $this->getProductTable()->saveProduct($product);
$pathstr = $this->md5path($picid);
$this->folder('public/images/product/'.$pathstr);
//move_uploaded_file($data['smallpicture']['tmp_name'], 'public/images/product/'.$pathstr.'/'.$picid.'_small.jpg');
//move_uploaded_file($data['bigpicture']['tmp_name'], 'public/images/product/'.$pathstr.'/'.$picid.'_big.jpg');
$fileadaptersmall = new \Zend\File\Transfer\Adapter\Http();
$fileadaptersmall->addFilter('File\Rename',array(
'source' => $data['smallpicture']['tmp_name'],
'target' => 'public/images/product/'.$pathstr.'/'.$picid.'_small.jpg',
'overwrite' => true
));
$fileadaptersmall->receive();
$fileadapterbig = new \Zend\File\Transfer\Adapter\Http();
$fileadapterbig->addFilter('File\Rename',array(
'source' => $data['bigpicture']['tmp_name'],
'target' => 'public/images/product/'.$pathstr.'/'.$picid.'_big.jpg',
'overwrite' => true
));
$fileadapterbig->receive();
}
the above are form,view,action.
using this way, only the small picture uploaed successfully. the big picture goes wrong.
a warning flashed like the following:
Warning:move_uploaded_file(C:\WINDOWS\TMP\small.jpg):failed to open stream:Invalid argument in
E:\myproject\vendor\zendframework\zendframework\library\zend\file\transfer\adapter\http.php
on line 173
Warning:move_uploaded_file():Unable to move 'C:\WINDOWS\TMP\php76.tmp'
to 'C:\WINDOWS\TEMP\big.jpg' in
E:\myproject\vendor\zendframework\zendframework\library\zend\file\transfer\adapter\http.php
on line 173
Who can tell me how to upload more than 1 file in this way. you know, the rename filter way similar to above. thanks.
I ran into the same problem with a site i did. The solution was to do the renaming in the controller itself by getting all the images and then stepping through them.
if ($file->isUploaded()) {
$pInfo = pathinfo($file->getFileName());
$time = uniqid();
$newName = $pName . '-' . $type . '-' . $time . '.' . $pInfo['extension'];
$file->addFilter('Rename', array('target' => $newName));
$file->receive();
}
Hope this helps point you in the right direction.
I encountered the same problem and i managed to make it work using the below code;
$folder = 'YOUR DIR';
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->setDestination($folder);
foreach ($adapter->getFileInfo() as $info) {
$originalFileName = $info['name'];
if ($adapter->receive($originalFileName)) {
$newFilePath = $folder . '/' . $newFileName;
$adapter->addFilter('Rename', array('target' => $newFilePath,
'overwrite' => true));
}
}

Categories