I'm trying to set a #default_value on my D7 form, but it's not working... This is my array:
'thema_30fdc789-1453-4efa-93d6-123cab52206e' =>
array
'#type' => string 'checkboxes' (length=10)
'#title' => string 'What kind of fiches??' (length=42)
'#options' =>
array
'create' => string 'New fiches' (length=13)
'update' => string 'Updated fiches' (length=13)
'delete' => string 'Deleted fiches' (length=13)
After editing this form with this line:
$form['thema_30fdc789-1453-4efa-93d6-123cab52206e']['#default_values']=
array('create' => 'Nieuwe fiches', 'update' => 'Update fiches', 'delete' => 'Delete fiches');
I get this var_dump:
'thema_30fdc789-1453-4efa-93d6-123cab52206e' =>
array
'#type' => string 'checkboxes' (length=10)
'#title' => string 'What kind of fiches??' (length=42)
'#options' =>
array
'create' => string 'Newfiches' (length=13)
'update' => string 'Updated fiches' (length=13)
'delete' => string 'Deleted fiches' (length=13)
'#default_values' =>
array
'create' => string 'Newfiches' (length=13)
'update' => string 'Updated fiches' (length=13)
'delete' => string 'Deleted fiches' (length=13)
So, the hook_form_alter did his job. Nevertheless, the checkboxes aren't checked on when reloading the page... What do I need to do to make sure they are checked on?
Nevermind. I found it myself. I needed to adjust the string to this:
$form['thema_30fdc789-1453-4efa-93d6-123cab52206e']['#default_value'] = array('create', 'update','delete');
Related
I'm creating a form date field which display day,month in drop down format. My problem is how can i add an option in it who's value is 0, so that when user dont want to select date 0 is stored in DB.
->add('monthPaymentDate', 'date', array(
'widget' => 'choice',
'empty_value' => array('year' => false, 'month' => 'Month', 'day' => 'Day'),
'format' => 'dd-MM-yyyy',
'input' => 'string',
'required' => true,
))
You can do this in your entity:
public function setMonthPaymentDate($monthPaymentDate){
if (!$monthPaymentDate instanceof Datetime){
$date = 0;
}
$this->monthPaymentDate = $monthPaymentDate;
}
You can achieve that by adding placeholder to the field.
->add('monthPaymentDate', 'date', array(
'widget' => 'choice',
'empty_value' => array('year' => false, 'month' => 'Month', 'day' => 'Day'),
'format' => 'dd-MM-yyyy',
'input' => 'string',
'required' => true,
'placeholder'=>'Choose a date'
))
Then the default value will be always null.
I'm working on a module and I would like to know how to add multiple dropdown with fields_options.
$this->fields_options = array(
'Test' => array(
'title' => $this->l('Test'),
'icon' => 'delivery',
'fields' => array(
'IXY_GALLERY_CREATION_OCCASION' => array(
'title' => $this->l('DropdownList'),
'type' => 'select',
'multiple' => true , // not working
'identifier' => 'value',
'list' => array(
1 => array('value' => 1, 'name' => $this->l('Test 1 ')),
2 => array('value' => 2, 'name' => $this->l('Test 2)'))
)
),
),
'description' =>'',
'submit' => array('title' => $this->l('Save'))
)
);
This is how I'm doin if you're meaning that :
$combo = $this->getAddFieldsValues();
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Title'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'select',
'lang' => true,
'label' => $this->l('Nom'),
'name' => 'nom_matiere',
'options' => array(
'query' => $combo[0],
'id' => 'id_option',
'name' => 'name'
)
),
array(
'type' => 'select',
'lang' => true,
'label' => $this->l('Nom'),
'name' => 'name',
'options' => array(
'query' => $combo[1],
'id' => 'id_option',
'name' => 'name'
)
),
),
),
'submit' => array(
'title' => $this->l('Save'),
'name' => $this->l('updateData'),
)
),
);
the answer are not correctly .. due its not only dfined the field in the database, also must capture and stored in special way the values, in this example i demostrate to store as "1,2,3,6,8" using a single field
THE COMPLETE CODE AND ALL THE STEPS ARE AT: https://groups.google.com/forum/m/?hl=es#!topic/venenuxsarisari/z8vfPsvFFjk
here i put only the most important parts..
as mentioned int he previous link, added a new fiel in the model definition, class and the table sql
this method permits to stored in the db as "1,2,3" so you can use only a single field to relation that multiple selected values, a better could be using groupbox but its quite difficult, take a look to the AdminCustomers controller class in the controllers directory of the prestachop, this has a multiselect group that used a relational table event stored in single field
then in the helper form list array of inputs define a select as:
at the begining dont foget to added that line:
// aqui el truco de guardar el multiselect como una secuencia separada por comas, mejor es serializada pero bueh
$this->fields_value['id_employee[]'] = explode(',',$obj->id_employee);
this $obj are the representation of the loaded previous stored value when go to edit ... from that object, get the stored value of the field of your multiselect, stored as "1,3,4,6"
and the in the field form helper list of inputs define the select multiple as:
array(
'type' => 'select',
'label' => $this->l('Select and employee'),
'name' => 'id_employee_tech',
'required' => false,
'col' => '6',
'default_value' => (int)Tools::getValue('id_employee_tech'),
'options' => array(
'query' => Employee::getEmployees(true), // el true es que solo los que estan activos
'id' => 'id_employee',
'name' => 'firstname',
'default' => array(
'value' => '',
'label' => $this->l('ninguno')
)
)
),
an then override the post process too
public function postProcess()
{
if (Tools::isSubmit('submitTallerOrden'))
{
$_POST['id_employee'] = implode(',', Tools::getValue('id_employee'));
}
parent::postProcess();
}
this make stored in the db as "1,2,3"
I'm sorry if my question is a bit dumb, but I have looked at other questions/Googled this and still didn't get it.
I have a form with default and custom elements:
class Form_Client extends Planner_Form {
public function init() {
$this->addPrefixPath('Planner_Form_Element', 'Planner/Form/Element', 'element');
$this->setAttrib('name', 'clientForm');
$this->addElement('text', 'phone', array(
'label' => 'Телефон',
'required' => true,
'filters' => array('Digits'),
'ng-model' => 'clientForm.phone',
));
$this->addElement('text', 'extra_phone_1', array(
'label' => 'Дополнительный телефон',
'required' => false,
'filters' => array(
array('Digits'),
),
'ng-model' => 'clientForm.extra_phone_1',
));
$this->addElement('text', 'name', array(
'label' => 'Имя',
'required' => false,
'ng-model' => 'clientForm.name',
));
$this->addElement('datetime', 'birthday', array(
'label' => 'Дата рождения',
'required' => false,
'ng-model' => 'clientForm.birthday',
));
I send form via AngularJs, and when I check it
if ($request->isPost()) {
$body = $this->getRequest()->getRawBody();
$data = Zend_Json::decode($body);
Zend_Debug::dump($data);
$form = new Form_Client();
if ($form->isValid($data)) {
$values = $form->getValues();
Zend_Debug::dump($values);
}
I get the following:
array(1) {
["phone"] => string(10) "9138521376"
}
array(4) {
["phone"] => string(10) "9138521376"
["extra_phone_1"] => string(0) ""
["name"] => NULL
["birthday"] => NULL
}
So my question is: why extra_phone_1 field gets empty string, and name gets NULL? Is it because of filter on extra_phone_1 field? If so, how can I set field value to empty string, when there is no data in POST for this field?
You are right, the value is'' due Digits filter and null without this filter.
To have a non-null value in the name field, for example, you can use the same technique and try to put the filter 'StringTrim'
I've got this table in my database that outputs this:
array(
(int) 0 => array(
'Price' => array(
'id' => '1',
'amount' => '20',
'price' => '180.00',
'type_id' => '1',
'active' => 'a'
)
),
(int) 1 => array(
'Price' => array(
'id' => '2',
'amount' => '30',
'price' => '232.50',
'type_id' => '1',
'active' => 'a'
)
),
...And so on.
I need a drop down in my form that displays the amount and price together (ie. "20 # 180.00"), but when selected, gets the "id" field.
I reworked a new array called $prices so it outputs like so...
array(
(int) 0 => array(
'id' => '1',
'amount' => '20',
'price' => '180.00',
'type_id' => '1',
'active' => 'a',
'display' => '20 # 180.00'
),
(int) 1 => array(
'id' => '2',
'amount' => '30',
'price' => '232.50',
'type_id' => '1',
'active' => 'a',
'display' => '30 # 232.50'
However, I'm not sure if that array is necessary.
But the main problem is that I don't know what to put in the Form options to make it select the "display" field.
echo $this->Form->input('Project.quantity', array(
'options' => $prices[?????]['display']
));
Simply adding the
'options' => $prices
displays a lot of stuff in the drop down (http://f.cl.ly/items/1e0X0m0D1f1c2o3K1n3h/Screen%20Shot%202013-05-08%20at%201.13.48%20PM.png).
Is there a better way of doing this?
You can use virtual fields.
In your model:
public $virtualFields = array(
'display' => 'CONCAT(amount, " # ", price)'
);
In the controller:
$prices = $this->Price->find('list', array(
'fields' => array('id', 'display')
));
Two ways to do this.
You said you reworked you array to $prices. Then, change that rework so the $prices array looks like this
array('1' => '4 # 6',
/*id*/ => /*price*/
/*etc*/);
and then pass it to the form
echo $this->Form->input('Project.quantity', array(
'options' => $prices
));
For a simple dropdown, retrieve the data with a find('list'). That will give you an array like the one you need to make a dropdown. To change the display field, create a virtual field like this in the model
public $virtualFields = array("display_price"=>"CONCAT(amount, ' # ' ,price)");
public $displayField = 'display_price';
And that way you don't have to rework your array.
If you have other drowpdowns of the same model in other forms, note that those will also change. That's the advantage of doing that in the model... Or disadvantage, if you only want to do it in one part... Like almost everything, it depends on what your need are :)
I have the following code that makes a sub form from a table, I have the error description saved in the database, I know how to set it for normal elements but not when it is a subform, how can I add a custom error message per subform element?
$subForm = new Zend_Form_SubForm();
foreach($configuration as $config){
$elements[] = array(
new Zend_Form_Element_Text($config->configuration_key, array(
'required' => (($config->is_required == 1) ? true : false),
'label' => $config->configuration_title,
'filters' => array('StringTrim'),
'value' => $config->configuration_value,
'Options' => array('style'=>'width:90%;'),
'Description' => $config->configuration_description,
'errorMessage' => $config->errorMessage,
))
);
}
$subForm->addElements($elements);
$this->addSubForm($subForm, 'configuration');
After playing a lot with the different options and trial/error I found out that I needed to add 'ErrorMessages' as an array, I rewrote my snippet
$elementSettings = array(
'required' => (($config->is_required == 1) ? true : false),
'label' => $config->configuration_title,
'filters' => array('StringTrim'),
'value' => $config->configuration_value,
'Options' => array('style'=>'width:90%;'),
'Description' => $config->configuration_description,
'ErrorMessages' => array($config->errorMessage)
);
$elements[] = array(new Zend_Form_Element_Text($config->configuration_key, $elementSettings));