In CakePHP2 we use FormHelper's inputDefault options to set default values for all input within form
For Example :
echo $this->Form->create('User', array(
'inputDefaults' => array(
'required' => false,
'error' => false,
'div' => 'form-group',
'label' => false
)
));
But i am not finding any option in CakePHP3 helper like this, they haven't mentioned removed it or not?
Is anybody there, who know about this....
Thanks
According to migration guide inputDefaults option was removed.
The inputDefaults option has been removed from create().
FormHelper::inputDefaults() has been removed. You can use templates() to define/augment the templates FormHelper uses.
templates() method of FormHelper documentation you can find here.
Related
I'm developing a prestashop module and I'm trying to show a category tree in my backoffice configuration page.
I'm trying to follow this instructions below but I don't know exactly where to add this code.
It should be inside main module's php? or inside a separate .php file and call it from the main one (don't know how to do it either).
As much time I'm spending trying to figure out, how to implement the code in the link above, the more I think I'm losing my time.
I see that "use" files, and this JS, " /admin-dev/themes/new-theme/js/components/form/choice-tree.js " are not in any prestashop folders.
Well, you should invest some time and learn Symfony since this is what you need to build backend modules for Prestashop 1.7.
As a pointer, you need to create a form class extending the CommonAbstractType, add a build form method. e.g. :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->context = Context::getContext();
$parents = [
['id_category' => 2, 'name' => 'Home', 'children' => $this->getSubCategories(1, true, 2)]
];
$builder->add('category', CategoryChoiceTreeType::class, [
'choices_tree' => $parents,
'choice_value' => 'id_category',
'choice_children' => 'children',
'choice_label' => 'name',
'disabled_values' => $disabledCategories,
'label' => 'Choose a category'
])
then add methods for retrieving the data to populate the form fields.
Then use this class in your controller and display the form:
$form = $this->createForm(YourFormForm::class);
Also add a processForm to process data.
As mentioned, this is not a copy/paste situation you need to understand the Symfony workflow.
The only way that I found to "paint" the categorytree in my configuration page is adding this code to the inputs form array:
Can anyone tell me how to retrieve users selection data to my database?
It does not work as any other form field.
array(
'type' => 'categories',
'label' => $this->l('Destination Category'),
'desc' => $this->l('Select ONE Category'),
'name' => 'CATEGORY_CATEGORY_TO',
'tree' => [
// 'selected_categories' => [],
'disabled_categories' => null,
'use_search' => false,
'use_checkbox' => false,
'id' => 'id_category_tree',
],
'required' => true
),
Well, it is SOLVED!!!! Finally it was very simple, but you must get the correct info for you particular case.
#Robertino's answer might be the best implementation, I don't know, but it became impossible to solve for me,
I uses this code below, and called $categoryTree from the form input. This input must be type=> categories_select
Thanks for your time, and for the help of another post from this forum.
$root = Category::getRootCategory();
//Generating the tree
$tree = new HelperTreeCategories('categories_1'); //The string in param is the ID used by the generated tree
$tree->setUseCheckBox(false)
->setAttribute('is_category_filter', $root->id)
->setRootCategory($root->id)
->setSelectedCategories(array((int)Configuration::get('CATEGORY_1'))) //if you wanted to be pre-carged
->setInputName('CATEGORY_1'); //Set the name of input. The option "name" of $fields_form doesn't seem to work with "categories_select" type
$categoryTree = $tree->render();
And the Form:
array(
'type' => 'categories_select',
'label' => $this->l('Category'),
'desc' => $this->l('Select Category '),
'name' => 'CATEGORY_1', //No ho podem treure si no, no passa la variable al configuration
'category_tree' => $categoryTree, //This is the category_tree called in form.tpl
'required' => true
A Zend_Form like this:
class Application_Form_Registration extends Zend_Form
{
public function init()
{
/* Form Elements & Other Definitions Here ... */
$$this->setMethod('post');
//first name
$this->addElement('text', 'email', array(
'label' => 'First name',
'required' => true,
'filters' => array('StringTrim'),
));
//last name
$this->addElement('text', 'lastname', array(
'label' => 'Last name',
'required' => true,
'filters' => array('StringTrim')
));
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => 'Submit'
));
$this->addElement('hash', 'csrf', array(
'ignore' => true,
));
}
}
I read through the ZF1 1.12 API and reference document, but I can't find the meaning of the flag "ignore" in the Zend_Form::addElement() configure options.
The api doc is just this:
Surely I googled it and find it but this is not the way to work. How to I find the meaning of certain specific stuff. I don't suppose that I need to read the source code?
Just take this addElement() as an example, am I missing somewhere to look further? Nothing in Zend_Config class that I can find about ignore flag either.
As I know ignore flag defines if form values ($form->getValues()) will contain element value. If ignore is set to true for some element than form values ($form->getValues()) will not contain this element value.
ZF Documentation can be...lacking sometimes. The API docs for the ignore flag state:
getIgnore( ) : bool Get ignore flag (used when retrieving values at form level)
Which hints that the ignore flag has something to do with the behavior of Zend_Form GetValues() but it's not really spelled out.
In these cases I like to go straight to the source code so I can see for myself:
public function getValues($supressArrayNotation = false)
{
...
foreach ($this->getElements() as $key => $element) {
if (!$element->getIgnore()) {
...
}
You can see that the getValues() function in Zend_Form will check the ignore flag on each element before adding the value to the return array. If the flag is true, the value won't be included.
I need to add an additional HTML attribute to each choice of an EntityType field in Symfony 2.7.
Following this guide, I assume that EntityType inherits this feature from ChoiceType. I tried something like the following, but without effect; no mytype attribute gets added to the rendered select options.
$builder->add('customer_email', 'email')
->add('Product', 'entity', array(
'class' => 'MyBundle:Product',
'property' => 'name',
'empty_value' => 'None',
'required' => false,
'choice_attr' => function ($val, $key, $index) {
return array('mytype' => $val->getType());
}))
This is not necessarily the best answer, but I can't post comments yet.
When implementing choice_attr, choice_labels etc... on ChoiceType and EntityType, it seems like choice_attr was left behind on the latter, there are a few comments about it on github, I personally need the same feature, hopefully it'll be implemented.
https://github.com/symfony/symfony/issues/4067
P.S.: Investigated further, it is indeed inherited from ChoiceType, and it only appears in the 2.7 documentation, if you write something like
'choice_attr' => function (Product $product, $key, $index) {
return ['class' => $product->getType() ];
}
You should get the class attribute set correctly, for custom attributes I am not sure, you might need to use 'attr' => 'foo'.
P.P.S.: Tested 'foo' =>'bar' and it works, no need to nest inside 'attr'.
I use Yii 1.1.14
I use form to capture an email, not using Yii ajaxValidation/clientValidation
<?php $form = $this->beginWidget('CActiveForm', array(
'id' => 'form-newsletter',
'action' => null,
'focus' => array($model, 'email'),
'htmlOptions' => array(
'role' => 'form',
),
)); ?>
I got this at js console
Uncaught TypeError: undefined is not a function
Which is refer to this
jQuery('#form-newsletter').yiiactiveform({'validateOnSubmit':true,'validateOnChange':true,'errorCssClass':'has-error has-feedback','successCssClass':'has-success has-feedback','inputContainer':'div.form-group','attributes':[{'id':'NewsletterForm_email','inputID':'NewsletterForm_email','errorID':'NewsletterForm_email_em_','model':'NewsletterForm','name':'email','enableAjaxValidation':true,'clientValidation':function(value, messages, attribute) {
I also check that jquery.yiiactiveform.js is present.
The question is, i dont want to use the default ajaxValidation/clientValidation, why is it loaded? and why it tells that yiiactiveform is undefined?
Thank you.
Check if you are including a newer version of jQuery than the one Yii includes (2.x for example) for the validation.
If you use other custom javascripts then must use in your layout code for example:
Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl . '/js/bootstrap.min.js');
How do i use the inputDefaults to add a common class to all the input elements in my form. also pls give a brief description of the inputDefaults.
isn't it:
echo $this->Form->create('User', array(
'inputDefaults' => array(
'class' => 'someclass'
)
);
`
You should read the cookbook. theres a good example: http://book.cakephp.org/view/1639/options-inputDefaults
When you create a form you add inputdefaults key in options:
echo $this->Form->create('User', array(
'inputDefaults' => array(
'div' => array('class' => 'someclass')
)
);
After browsing the source file i didn't find anything either. So the only way is to use it explicitly for every call to the input function.