Set the dynamic values in drop down in ZF2 - zend-framework2

I am using Zend Framework 2 and I would like to understand how I can add values to a drop down that is coming from a database.
The code is here:
$this->add(
array(
'name' => 'role',
'type' => 'Zend\Form\Element\Select',
'option' => array(
'label' => 'Role',
'value_option' => $roleData
)
)
);
Here the $roleData as an array and the value of array is as follows:
Array ( [0] => Array ( [id] => 1 [cell] => admin ) [1] => Array ( [id] => 2 [cell] => member ) [2] => Array ( [id] => 3 [cell] => guest ) [3] => Array ( [id] => 4 [cell] => Admina1 ) [4] => Array ( [id] => 5 [cell] => Admina1 ) )

Try 'value_options' instead of 'value_option'.
And your array has to be the following structure:
$options = array(
'1' => 'admin',
'2' => 'member',
'3' => 'guest',
'4' => 'Admina1',
'5' => 'Admina1'
);

$this->add(array(
'name' => 'yesorno',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Yes or No Label',
'value_options' => array(
0 => 'No',
1 => 'Yes'
)
),
));

Related

Skip controller and action in url generation

TYPO3 version is 7.6.0.
Generate link:
<f:link.action action="single" class="title" pageUid="{settings.pidDetails}" additionalParams="{unid: '{unid}'}" noCacheHash="true">Link text</f:link.action>
Result:
http://example.com/property/flats/?unid=12345&tx_ext_extp1%5Baction%5D=single&tx_ext_extyp1%5Bcontroller%5D=Ext
Need:
Skip controller and action in url generation - http://example.com/property/flats/?unid=12345
You can use realurl extension for clean url.
After installation, edit realurl_autoconf.php file which located in typo3conf folder.
function user_encodeSpURL_postProc(&$params, &$ref) {
$params['URL'] = str_replace('blog/post/article/', 'blog/post/', $params['URL']);
}
function user_decodeSpURL_preProc(&$params, &$ref) {
$params['URL'] = str_replace('blog/post/', 'blog/post/article/', $params['URL']);
}
$GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields'].= ',tx_realurl_pathsegment';
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl'] = array (
'_DEFAULT' => array (
'init' => array (
'enableCHashCache' => '1',
'appendMissingSlash' => 'ifNotFile',
'enableUrlDecodeCache' => '1',
'enableUrlEncodeCache' => '1'
),
'redirects' => array (
),
'preVars' => array (
'0' => array (
'GETvar' => 'no_cache',
'valueMap' => array (
'nc' => '0'
),
'noMatch' => 'bypass',
),
'1' => array (
'GETvar' => 'L',
'valueMap' => array (
'de' => '0',
'en' => '1'
),
'noMatch' => 'bypass'
),
'2' => array (
'GETvar' => 'cHash',
'noMatch' => 'bypass',
),
),
'pagePath' => array (
'type' => 'user',
'userFunc' => 'EXT:realurl/class.tx_realurl_advanced.php:&tx_realurl_advanced->main',
'spaceCharacter' => '-',
'languageGetVar' => 'L',
'rootpage_id' => '1' // your root UID
),
'fixedPostVars' => array(
),
'postVarSets' => array(
'_DEFAULT' => array(
'controller' => array(
array(
'GETvar' => 'tx_news_pi1[action]',
'noMatch' => 'bypass'
),
array(
'GETvar' => 'tx_news_pi1[controller]',
'noMatch' => 'bypass'
)
),
'authentication' => array(
array(
'GETvar' => 'tx_eventmanagementtool_ext1[action]',
),
),
'dateFilter' => array(
array(
'GETvar' => 'tx_news_pi1[overwriteDemand][year]',
),
array(
'GETvar' => 'tx_news_pi1[overwriteDemand][month]',
),
),
'page' => array(
array(
'GETvar' => 'tx_news_pi1[#widget_0][currentPage]',
),
),
),
),
'fileName' => array (
//'defaultToHTMLsuffixOnPrev' => '.html',
'acceptHTMLsuffix' => 1,
'index' => array (
'rss.xml' => array (
'keyValues' => array (
'type' => '100'
),
),
'rss091.xml' => array (
'keyValues' => array (
'type' => '101'
),
),
'rdf.xml' => array (
'keyValues' => array (
'type' => '102'
),
),
'atom.xml' => array (
'keyValues' => array (
'type' => '103'
),
),
'sitemap.xml' => array (
'keyValues' => array (
'type' => '776'
),
),
),
),
)
);
You can use bypass method to remove controller and action from url like:
'controller' => array(
array(
'GETvar' => 'tx_news_pi1[action]',
'noMatch' => 'bypass'
),
array(
'GETvar' => 'tx_news_pi1[controller]',
'noMatch' => 'bypass'
)
)
Go to your Extension folder.
In Configuration –>TypoScript –> setup.txt write below code:
plugin.tx_[your_extensionname].features.skipDefaultArguments = 1
Warning: This only works if you don’t use switchableControllerActions in your flexform.

How to load Database data in Zend Form Form Element Select?

I am trying to load the dynamic data from databse to the following zend form element. How to do that i have no idea.
$this->add(array(
'type' => 'Select',
'name' => 'catid',
'attributes' => array(
'id' =>'catid'
),
'options' => array(
'label' => 'Category',
'value_options' => array(
'1' => 'Action',
'2' => 'Comedy',
'3' => 'Sci-Fi',
'4' => 'Drama',
),
)
));
I want to add dynamic values to :
'1' => 'Action',
'2' => 'Comedy',
'3' => 'Sci-Fi',
'4' => 'Drama',
I solved it with the following codes :P
$form = new Zform('zform');
$form->get('catid')->setValueOptions($options);

select a SF2 choice option

My code for my form ( i am using Silex):
$test = array(
'Swedish Cars' => array(
'volvo' => 'Volvo',
'saab' => 'Saab',
),
'German Cars' => array(
'mercedes' => 'Mercedes',
'audi' => 'Audi'
)
);
$form = $app['form.factory']->createBuilder('form')
->add('title','text',array(
'attr' => array(
'placeholder' => 'Title of your Album'
)))
->add('description','textarea',array(
'attr' => array(
'placeholder' => 'Describe your Album'
)))
->add('groups', 'choice', array(
'choices' => $test,
'multiple' => true,
'attr' => array(
'data-placeholder' => 'Add your Groups ...'
),
))
The choices are defined as an multi-array, so I get <option> with <optgropup>. How can I enable in SF2, that some options are selected?
Use the data option:
->add('groups', 'choice', array(
'choices' => $test,
'multiple' => true,
'attr' => array(
'data-placeholder' => 'Add your Groups ...',
'data' => $selected_value
),
where $selected_value can be a single value like 'value_1' or an simple array with multiple values array('value_1', 'value_2') to select.

Zend framework 2 - How to bind form data with a simple array

I'm looking for clean solution how to bind form data with array only?
I was trying to convert in to stdClass object with no result.
Form
class Product extends Form
{
public function __construct()
{
parent::__construct('add_product');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'type',
'type' => 'Select',
'options' => array(
'label' => 'Product type:',
'value_options' => $this->getProductTypeCollection(),
//'empty_option' => '',
),
'attributes' => array(
'value' => 'simple',
'class' => 'select'
)
));
$this->add(array(
'name' => 'set',
'type' => 'Select',
'options' => array(
'label' => 'Product attribute set:',
'value_options' => $this->getAttributeSetCollection(),
),
'attributes' => array(
'class' => 'select'
)
));
$this->add(array(
'name' => 'categories',
'type' => 'MultiCheckbox',
'options' => array(
'label' => 'Categories:',
'value_options' => $this->getCategoryTree(),
),
'attributes' => array(
'class' => 'multi-checkbox'
)
));
$this->add(array(
'name' => 'websites',
'type' => 'Text',
'options' => array(
'label' => 'Website:',
),
'attributes' => array(
'class' => 'text',
'readonly' => 'readonly',
'value' => $session->magento_store_view_code,
)
));
}
}
In edit action:
$form = new ProductForm();
$form->bind($productData);
$productData is an array and it looks like this:
array (size=52)
'product_id' => string '6' (length=1)
'sku' => string '10/10/1992' (length=10)
'set' => string '4' (length=1)
'type' => string 'simple' (length=6)
'categories' =>
array (size=1)
0 => string '3' (length=1)
'websites' =>
array (size=1)
0 => string '1' (length=1)
'type_id' => string 'simple' (length=6)
'name' => string 'Front fog lights set crystal smoked VW Golf MK2, Jetta MK2' (length=58)
'description' => string 'Front fog lights set crystal smoked VW Golf MK2, Jetta MK2' (length=58)
'short_description' => string 'Front fog lights set crystal smoked VW Golf MK2, Jetta MK2' (length=58)
'weight' => string '2.0000' (length=6)
'old_id' => null
'news_from_date' => null
'news_to_date' => null
'status' => string '1' (length=1)
'url_key' => string 'front-fog-lights-set-crystal-smoked-vw-golf-mk2-jetta-mk2' (length=57)
'url_path' => string 'front-fog-lights-set-crystal-smoked-vw-golf-mk2-jetta-mk2.html' (length=62)
'visibility' => string '4' (length=1)
'category_ids' =>
array (size=1)
0 => string '3' (length=1)
'required_options' => string '0' (length=1)
'price' => string '49.9900' (length=7)
...
Regards,
change this
$form = new ProductForm();
$form->bind($productData);
to this
$product = new Product();
$product->exchangeArray(get_object_vars($productData));
$form=new ProductForm();
$form->bind($product);
$form->get('submit')->setValue('Save');

Validations in zend framework 2

I have defined fieldsets for form in zend framework 2.
$this->add ( array (
'name' => 'unitnumber',
'options' => array (
'label' => 'Unit Number:'
)
) );
$this->add ( array (
'name' => 'streetdirprefix',
'options' => array (
'label' => 'Street Direction Prefix:'
)
) );
$this->add ( array (
'name' => 'streetnumber',
'options' => array (
'label' => 'Street Number:'
)
) );
I want to set required field for unitnumber only. Not for other fields.
I have writen a function..
public function getInputFilterSpecification()
{
return array (
'name' => array(
'required' => true,
)
);
}
But, It's asking to fill all the fields. Can anyone help me out in this. Thanks.
$inputFilter->add(array(
'name' => 'artist',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
References:
http://framework.zend.com/manual/2.2/en/user-guide/forms-and-actions.html
http://framework.zend.com/manual/2.2/en/modules/zend.input-filter.intro.html
In Zend 2, the "InputFilter" works more like in "strict" mode.
i.e. It requires you to specify if the elements mentioned in the InputFilter should have the 'required' value as TRUE or FALSE.
$this->add ( array (
'name' => 'unitnumber',
'required' => true, //Code Added
'options' => array (
'label' => 'Unit Number:'
)
));
$this->add ( array (
'name' => 'streetdirprefix',
'required' => false, //Code Added
'options' => array (
'label' => 'Street Direction Prefix:'
)
));
$this->add ( array (
'name' => 'streetnumber',
'required' => false, //Code Added
'options' => array (
'label' => 'Street Number:'
)
));

Resources