Adding a label to a symfony 2 form field - php

I'm trying to modify a piece of code that generates an input field using the Symfony 2 framework. The thing he creates the field with a default label equals to the name id of the field, in this case; "amount".
<?php
//++++++++++ descrizione
echo $view['form'] -> row($form["amount"], array(
//widget
"widgetArgs" => array(
"attr" => array(
'class' => 'input-small tooltipRight',
'id' => "gift_amount"
),
"tooltip"=>"gift.tooltip.amount",
"translation_domain" =>"brand"
),
"labelArgs" => array(
"label_attr" => array(
'class' => 'control-label',
)) ,"rowType"=>2
)
);
?>
How can i edit this to make it show a custom label?

You have to add the label properties:
<?php
echo $view['form']->row($form["amount"], array(
'widgetArgs' => array(
"attr" => array(
'class' => 'input-small tooltipRight',
'id' => "gift_amount"
),
'tooltip'=>'gift.tooltip.amount',
'translation_domain' =>'brand'
),
'label' => 'My ammount',
));

Related

Symfony2 Sonata Admin shows text after creating a new item

First, i'm sorry for my bad english,
im using sonata-admin bundle, and adminstrating my own Entity, when i'm creating a new item I have a block of text (the text that i write in the first input) that appears in the top of the Admin page, and it's ugly when its a big paragraph.
Lets say my first input is "Content", so when I write " This is my content " and save the item, it says "The item " This is my content" has been created succefully", how can I make another input be written in the success message
EDIT :
Here is a printscreen :
$formMapper
->add('page_mere1', 'choice', array('label' => 'Page mère 1', 'choices' => array('Podologie' => 'Podologie', 'Podz Pro'=>'Podz Pro')))
->add('page_mere2', 'choice', array('label' => 'Page mère 2', 'choices' => array('Pathologies' => 'Pathologies', 'Maladies spécifiques'=>'Maladies spécifiques')))
->add('page_mere3', 'choice', array('label' => 'Page mère 3', 'choices' => array('Pied' => 'Pied', 'Cheville'=>'Cheville', 'Jambe'=>'Jambe', 'Genou'=>'Genou', 'Hanche'=>'Hanche', 'Dos'=>'Dos')))
->add('translations', 'a2lix_translations_gedmo', array(
'translatable_class' => "Antipodes\SiteBundle\Entity\Pathologie",
'fields' => array(
'titre' => array(
'field_type' => null,
'required' => false,
'label' => 'Titre.',
'locale_options' => array(
'fr' => array(
'label' => 'Titre'
),
'en' => array(
'label' => 'Title'
)
)
),
'Definition' => array(
'field_type' => null,
'required' => false,
'label' => 'Definition.',
'locale_options' => array(
'fr' => array(
'label' => 'Definition'
),
'en' => array(
'label' => 'Definition'
)
)
),
)
))
;
//.......
Thanks in advance
I'm not sure if I'm understanding you correctly, but if you want to change the OK flashbag message after an successful edit you can do this with the _toString() method in your entity.
Open your Pathologie entity and add/edit the __toString() function, example:
public function __toString() {
return $this->titre;
}
The save-flashbag would then merge the "titre"-value instead of "Definition"-value into the message.

change id checkboxs in CGridView

there are 2 columns check box in cgridViewtable and two bootstrap widgets TbButton in view page.
I can not get value of my checkbox very good. My value in checkboxs transfer into controller but changed id of checkboxs After a period of timeand , and controller don't knew checkbox,
View:
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id' => 'profile-information-form',
'enableAjaxValidation' => false,
));
$this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'submit', 'type' => 'primary', 'label' => Yii::t('fa_ir', 'First validation'),));
$this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'submit', 'type' => 'primary', 'label' => Yii::t('fa_ir', 'End validation'),));
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'profile-information-grid',
'dataProvider' => $model->children(),
'filter' => $model,
'columns' => array(
array(
'header' => '',
'value' => '$this->grid->dataProvider->pagination->offset + $row+1', // row is zero based
),
array(
'name' => 'ProfileInformation.user.scope',
'value' => 'CHtml::encode($data->user->scope->name)',
'filter' => Scope::model()->options,
),
array(
'name' => 'id',
'value' => 'CHtml::encode($data->id)',
),
array(
'name' => 'center',
'value' => 'CHtml::encode($data->center)',
),
array(
'name' => 'sendCount',
'value' => 'CHtml::encode($data->sendCount)',
'filter' => '',
),
// Use CCheckbox column with selectableRows = 2 for Select All
array('class' => 'CCheckBoxColumn', 'selectableRows' => 2, 'visible' => (Lookup::isUser2(Yii::app()->user->id) or Lookup::isAdmin(Yii::app()->user->id))),
array('class' => 'CCheckBoxColumn', 'selectableRows' => 2, 'visible' => (Lookup::isUser1(Yii::app()->user->id) or Lookup::isAdmin(Yii::app()->user->id))),
),
));
// action button
$this->endWidget();
Controller:
if (isset($_POST['profile-information-grid_c10']) & isset($_POST['yt0'])) {
////Do action1
}
if (isset($_POST['profile-information-grid_c12']) & isset($_POST['yt1'])) {
////Do action2
}
}
my problem is in $_POST['profile-information-grid_c10'] and _POST['profile-information-grid_c12'] , before id were $_POST['profile-information-grid_c8'] and $_POST['profile-information-grid_c10'] , and now is $_POST['profile-information-grid_c12'] and $_POST['profile-information-grid_c14'].
my problem is very involved. I cannot explain very good.
I want to have a fix id.
I donto why change id of check box?
Can I assign ids for checkboxs?
Can I assign ids for checkboxs? Sure, you better explicitly set checkbox column id:
'class' => 'CCheckBoxColumn', 'id'=>'column1', ... see these docs.
This way you firmly set it and you'll have no things like these yt0, yt2...
The same you can do for the buttons. See TButton docs.
Also you need to use double && in logical condition:
isset($_POST['profile-information-grid_c12']) & isset($_POST['yt1'])

How to validate multiply select using Zend Framework 2

I am trying to validate a multiply select using input filter, but every time I see a error. The error is "notInArray":"The input was not found in the haystack".(I use ajax but it doesn`t metter).
I will show part of my code to be more clear.
in Controller:
if ($request->isPost()) {
$post = $request->getPost();
$form = new \Settings\Form\AddUserForm($roles);//
$form->get('positions')
->setOptions(
array('value_options'=> $post['positions']));
//.... more code...
When I put print_r($post['positions']); I see:
array(0 => 118, 1 => 119)
in ..../form/UserForm.php I create the multiply element
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
and in the validation file the code is:
$inputFilter->add($factory->createInput(array(
'name' => 'positions',
'required' => false,
'validators' => array(
array(
'name' => 'InArray',
'options' => array(
'haystack' => array(118,119),
'messages' => array(
'notInArray' => 'Please select your position !'
),
),
),
),
What can be the reason every time to see this error, and how I can fix it?
By default selects have attached InArray validator in Zend Framework 2.
If you are adding new one - you will have two.
You should disable default one as follow:
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'options' => array(
'disable_inarray_validator' => true, // <-- disable
),
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
And you should get rid of the additional error message.
Please let us know if that would helped you.

Magento adminhtml form fields not adding to POST

I have the following fields in a magento adminhtml form.
On submit im expecting to grab the post, and simply dump its contents, which im doing in my saveAction.
public function saveAction()
{
if ($this->getRequest()->getPost())
{
try{
$postData = $this->getRequest()->getPost();
echo '<pre>';
print_r($postData);
exit;
the output looks as follows.
Array
(
[form_key] => I6jK6swe1EMl0wER
[carrier_code] => test
[postcode] => tescode
[sku] => 123445
)
Seeing my form is defined as:
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('instance_form', array('legend'=>Mage::helper('instance')->__('Instance Filters')));
$fieldset->addField('carrier_code', 'text', array(
'label' => Mage::helper('instance')->__('Carrier service'),
'name' => 'carrier_code',
'after_element_html' => '<small>Leave blank for all Carriers.</small>',
));
$fieldset->addField('postcode', 'text', array(
'label' => Mage::helper('instance')->__('Postcode'),
'name' => 'postcode',
'after_element_html' => '<small>Leave blank for all Postcodes.</small>',
));
$fieldset->addField('sku', 'text', array(
'label' => Mage::helper('instance')->__('Sku'),
'name' => 'sku',
'after_element_html' => '<small>Leave blank for all Skus.</small>',
));
$fieldset->addField('start_date', 'date', array(
'label' => Mage::helper('instance')->__('Start Date'),
'after_element_html' => '<small>Comments</small>',
'tabindex' => 1,
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'format' => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)
));
$fieldset->addField('aura', 'file', array(
'label' => Mage::helper('instance')->__('Upload'),
'value' => 'Uplaod',
'disabled' => false,
'readonly' => true,
'after_element_html' => '<small>Comments</small>',
'tabindex' => 1
));
I was expecting to see output like this instead :
Array
(
[form_key] => I6jK6swe1EMl0wER
[carrier_code] => test
[postcode] => tescode
[sku] => 123445
[start_date] => someValue
[aura] => anotherValue
)
am i missing something? why would say the date field, not be added to the post, like all the other text input fields?
Cheers
You're missing the name key in your addField('start_date', ..) call.
Each field of a Varien_Data_Form you want to be submittable needs a name key/value pair.
The value you assign to your field's name key is used as value for the name attribute of the corresponding <input> element when rendering the <form>.

How to add new fields to user profile in Drupal 7 programmatically

I want to develop a module that add fields to user profile in drupal 7, like phone number and CV ...
and I don't know how to do that (using Database or using fields API)
pls help me.
Any clear tutorials will be appreciated.
Try to follow the following code
$myField_name = "NEW_FIELD_NAME";
if(!field_info_field($myField_name)) // check if the field already exists.
{
$field = array(
'field_name' => $myField_name,
'type' => 'text',
);
field_create_field($field);
$field_instance = array(
'field_name' => $myField_name,
'entity_type' => 'user', // change this to 'node' to add attach the field to a node
'bundle' => 'user', // if chosen 'node', type here the machine name of the content type. e.g. 'page'
'label' => t('Field Label'),
'description' => t(''),
'widget' => array(
'type' => 'text_textfield',
'weight' => 10,
),
'formatter' => array(
'label' => t('field formatter label'),
'format' => 'text_default'
),
'settings' => array(
)
);
field_create_instance($field_instance);
Hope this works... Muhammad.

Categories