Related
I'm trying to create a form that contains a collection of fieldsets using only array specs and Zend\Form\Factory.
Here is how I create the form using the factory:
$factory = new Zend\Form\Factory();
$fieldset = $factory->createFieldset(array(
'elements' => array(
array(
'spec' => array(
'name' => 'name',
'type' => 'Text',
'attributes' => array(
'class' => 'form-control input-sm',
),
'options' => array(
'label' => 'Name',
),
),
),
array(
'spec' => array(
'name' => 'driverClass',
'type' => 'Text',
'attributes' => array(
'class' => 'form-control input-sm',
),
'options' => array(
'label' => 'Driver',
),
),
),
),
'input_filter' => array(
'name' => array(
'required' => true,
),
),
));
$form = $factory->createForm(array(
'name' => 'application-form',
'attributes' => array(
'role' => 'form',
),
'elements' => array(
array(
'spec' => array(
'type' => 'Collection',
'name' => 'connection',
'options' => array(
'label' => 'Connections',
'allow_add' => true,
'allow_remove' => true,
'should_create_template' => true,
'count' => 2,
'target_element' => $fieldset,
),
),
),
array(
'spec' => array(
'name' => 'security',
'type' => 'Csrf',
'attributes' => array(
'required' => 'required',
),
),
),
array(
'spec' => array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'class' => 'btn btn-sm btn-primary',
),
'options' => array(
'label' => 'Apply',
),
),
),
),
));
The resulting form works fine when I try to set data and render form elements. But when I validate it and retrieve data, like so (in a controller):
$form->setData($this->getRequest()->getPost());
if ($form->isValid() === true) {
$data = $form->getData();
var_dump($this->getRequest()->getPost());
var_dump($data);
}
With this set of data as POST:
object(Zend\Stdlib\Parameters)[141]
private 'storage' (ArrayObject) =>
array (size=3)
'connection' =>
array (size=2)
0 =>
array (size=2)
'name' => string 'orm_default' (length=11)
'driverClass' => string 'Doctrine\DBAL\Driver\PDOMySql\Driver' (length=36)
1 =>
array (size=2)
'name' => string 'blog' (length=4)
'driverClass' => string 'Doctrine\DBAL\Driver\PDOMySql\Driver' (length=36)
'submit' => string '' (length=0)
'security' => string '20d5c146d8874dc804948e962d5de91b-87c9e4097f9140d259efb5c589a05d6b' (length=65)
The array returned by the call to $form->getData() shows an empty collection:
array (size=3)
'security' => string '20d5c146d8874dc804948e962d5de91b-87c9e4097f9140d259efb5c589a05d6b' (length=65)
'submit' => string '' (length=0)
'connection' =>
array (size=0)
empty
What am I missing?
The expected result is a collection, named 'connection' in this example, containing two arrays representing the two fieldsets as specified by the POST data. I have a feeling this has to do with a missing InputFilter (or at least its specs) because I have managed to obtain the expected result when I implement a fieldset class that extends Zend\Form\Fieldset and implements Zend\InputFilter\InputFilterProviderInterface.
Just discovered this class Zend\Form\InputFilterProviderFieldset which does exactly what I missed.
I added a type in the fieldset specs and changed the input filter specs (which is mandatory) like so:
$fieldset = $factory->createFieldset(array(
'type' => 'Zend\Form\InputFilterProviderFieldset',
'elements' => array(
array(
'spec' => array(
'name' => 'name',
'type' => 'Text',
'attributes' => array(
'class' => 'form-control input-sm',
),
'options' => array(
'label' => 'Name',
),
),
),
array(
'spec' => array(
'name' => 'driverClass',
'type' => 'Text',
'attributes' => array(
'class' => 'form-control input-sm',
),
'options' => array(
'label' => 'Driver',
),
),
),
),
'options' => array(
'input_filter_spec' => array(
'name' => array(
'required' => true,
),
),
),
));
And it works fine now. Hope this helped someone.
i am working with Yii2 and using Editable Widget
My code is below
Editable::widget([
'id' => 1,
'name' => 'assignTo',
'value' => 1,
'url' => 'url here',
'type' => 'select',
'mode' => 'inline',
'clientOptions' => [
'toggle' => 'dblclick',
'emptytext' => 'Unassigned',
'placement' => 'right',
'select2' => [
'width' => '124px'
],
'source' => 1,
'value' => 1,
],
]);
i want to add custom attribute on that generated html tag. i have tried as below but its throw error
Editable::widget([
'id' => 'assignTo_'.$todo->id,
'name' => 'assignTo',
'redirect_url' => 'custom_attriute', // this is custom attribute that i need
'class' => 'my own custom class', // this is custom attribute that i need
'value' => 1,
'url' => 'url here',
'type' => 'select',
'mode' => 'inline',
'clientOptions' => [
'toggle' => 'dblclick',
'emptytext' => 'Unassigned',
'placement' => 'right',
'select2' => [
'width' => '124px'
],
'source' => 1,
'value' => 1,
],
]);
and also i want to add my own class in the generated html i have tried same as above but its not working.
is there any way to make it possible what i want?
dosamigos\editable\Editable extends yii\widgets\InputWidget which has an $options variable that holds:
The HTML attributes for the input tag.
Editable::widget([
'id' => 'assignTo_'.$todo->id,
'name' => 'assignTo',
'options' => [
'redirect_url' => 'custom_attriute', // this is custom attribute that i need
'class' => 'my own custom class', // this is custom attribute that i need
],
'value' => 1,
'url' => 'url here',
'type' => 'select',
'mode' => 'inline',
'clientOptions' => [
'toggle' => 'dblclick',
'emptytext' => 'Unassigned',
'placement' => 'right',
'select2' => [
'width' => '124px'
],
'source' => 1,
'value' => 1,
],
]);
I have the following element in my form, and I tried all possible options found in the web to allow empty value for element:
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'directPractice',
'options' => array(
'label' => 'A. Check all direct practice field education assignments',
'label_attributes' => array(
'class' => 'label-multicheckbox-group'
),
'required' => false,
'allow_empty' => true,
'continue_if_empty' => false,
'object_manager' => $this->getObjectManager(),
'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments', //'YOUR ENTITY NAMESPACE'
'property' => 'directPractice', //'your db collumn name'
'disable_inarray_validator' => true,
'value_options' => array(
'1' => 'Adults',
'2' => 'Individuals',
'3' => 'Information and Referral',
'4' => 'Families',
array(
'value' => 'Other',
'label' => 'Other (specify)',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'otherDirectPracticeTxt_ID'
),
'attributes' => array(
'id' => 'otherDirectPractice_ID',
),
)
),
),
'attributes' => array(
'value' => '1', //set checked to '1'
'multiple' => true,
)
));
And I am always getting the same error message when it is empty:
Validation failure 'directPractice':Array
(
[isEmpty] => Value is required and can't be empty
)
Ok, this is a best what I could come up after researching on it. Solution is inspired by these question.
Form: hidden field and ObjectMultiCheckBox
public function init()
{
$this->setAttribute('method', 'post');
$this->setAttribute('novalidate', 'novalidate');
//hidden field to return empty value. If checkbox selected, checkbox values will be stored with empty value in Json notation
$this->add(array(
'type' => 'Hidden',
'name' => 'otherLearningExperiences[]', // imitates checkbox name
'attributes' => array(
'value' => null
)
));
$this->add(array(
// 'type' => 'Zend\Form\Element\MultiCheckbox',
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'otherLearningExperiences',
'options' => array(
'label' => 'C. Check other learning experiences',
'object_manager' => $this->getObjectManager(),
'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments',
'property' => 'otherLearningExperiences',
'label_attributes' => array(
'id' => 'macro_practice_label',
'class' => 'control-label label-multicheckbox-group'
),
'value_options' => array(
array(
'value' => 'seminars',
'label' => 'Seminars, In-Service Training/Conferences',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'seminarsTxt_ID'
),
'attributes' => array(
'id' => 'seminars_ID',
),
),
array(
'value' => 'other',
'label' => 'Other (specify)',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'otherLeaningExperiencesTxt_ID'
),
'attributes' => array(
'id' => 'otherLeaningExperiences_ID',
),
)
),
),
'attributes' => array(
//'value' => '1', //set checked to '1'
'multiple' => true,
'empty_option' => '',
'required' =>false,
'allow_empty' => true,
'continue_if_empty' => false,
)
));
Validator
$inputFilter->add($factory->createInput(array(
'name' => 'otherLearningExperiences',
'required' => false,
'allow_empty' => true,
'continue_if_empty' => true,
)));
}
View
//hidden field to be posted for empty value in multicheckbox
echo $this->formHidden($form->get('otherLearningExperiences[]'));
$element = $form->get('otherLearningExperiences');
echo $this->formLabel($element);
echo $this->formMultiCheckbox($element, 'prepend');
Here is my code
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'dsfsd',
'dataProvider' => $model->search(),
'filter' => $model,
'cssFile' => false,
'itemsCssClass' => 'table table-striped',
'beforeAjaxUpdate'=>'js:function(id,options){$.blockUI({
message: "Please wait",
//showOverlay: false,
css: {
border: "none",
padding: "15px",
"-webkit-border-radius": "10px",
"-moz-border-radius": "10px",
opacity: 1,
"z-index": "9999"
}
});}',
'columns' => array(
array(
'header' => '#',
'name' => 'id',
'type' => 'raw',
'value' => 'CHtml::link(CHtml::encode($data->id),array("abc/m/".CHtml::encode($data->id)."/info"))',
'htmlOptions' => array('width' => '40px'),
'filter' => CHtml::activeTextField($model, 'id', array('placeholder' => 'search by id')),
), array(
'header' => 'First name',
'name' => '$data.user.f_name',
'type' => 'raw',
'value' => 'CHtml::encode($data->user->f_name)',
), array(
'header' => 'Last name',
'name' => '$data.user.l_name',
'type' => 'raw',
'value' => 'CHtml::encode($data->user->l_name)'
),
array(
'class' => 'CButtonColumn',
'template' => '{update} {delete}',
//'updateButtonUrl' => 'Yii::app()->createUrl("abc/xyz?id=".$data->id."&c_id=".$data->c_id)',
'deleteButtonUrl' => 'Yii::app()->createUrl("xyv/abc",array("uid"=>$data->id))',
),
),
));
Well, what I want is that the operations under beforeAjaxUpdate will work only when delete button is clicked, not for any other ajax actions like on search or update. How is it possible?
You can add callback function to delete button:
http://www.yiiframework.com/doc/api/1.1/CButtonColumn#buttons-detail
You should set click option in buttons config array:
'columns' => array(
...
'buttons'=>array(
'class'=>'CButtonColumn',
'template'=>'{delete}{update}',
'buttons'=>array(
'delete'=>array(
'click'=>'function(){/*your code here*/}'
)
)
)
...
)
You can return false from callback function to prevent deleting.
I have been building a module in PyroCMS and have made its structure. I am using Streams API to build forms and perform my actions and also performed a dummy install to check if everything looks ok. Then I went on modifying the contents of the dummy and completing my structure. However, in the details.php file, I made a change where I created a foreign key (relationship type of field, if u know the API jargon) to retrieve a field from another stream(the other stream being defined after my current stream) and now when I install the module, it shows 'Could not install the module' error, but I can see the module has been installed. I have tried to comment out the foreign key reference, but the problem still persists.
Here's my details.php file:
class Module_Employer extends Module
{
public $version = '1.0';
public function info()
{
return array(
'name' => array(
'en' => 'Employer'
),
'description' => array(
'en' => 'Module for Employer'
),
'frontend' => true,
'backend' => true,
'menu' => 'content',
'shortcuts' => array(
'create' => array(
'name' => 'Employer:new',
'uri' => 'admin/employer/create',
'class' => 'add'
)
)
);
}
public function install()
{
// We're using the streams API to
// do data setup.
$this->load->driver('Streams');
$this->load->language('employer/emp');
// Add streams
if ( ! $this->streams->streams->add_stream(lang('Employer:employers'), 'employers', 'employer', null, 'This is the Employer Stream')) return false;
if ( ! $this->streams->streams->add_stream(lang('Employer:company'), 'company', 'company', null, 'This is the Company Stream')) return false;
if ( ! $this->streams->streams->add_stream(lang('Employer:job'), 'jobs', 'job', null, 'This is the Job Stream')) return false;
// Fields for employers table
$employer_fields = array(
array(
'name' => 'Name',
'slug' => 'name',
'namespace' => 'employer',
'type' => 'text',
'extra' => array('max_length' => 100),
'assign' => 'employers',
'title_column' => true,
'required' => true,
'unique' => true
),
array(
'name' => 'Username',
'slug' => 'username',
'namespace' => 'employer',
'type' => 'text',
'extra' => array('max_length' => 100),
'assign' => 'employers',
'title_column' => true,
'required' => true,
'unique' => true
),
array(
'name' => 'Password',
'slug' => 'password',
'namespace' => 'employer',
'type' => 'encrypt',
'extra' => array('hide_typing' => 'yes'),
'assign' => 'employers',
'title_column' => true,
'required' => true,
'unique' => true
),
array(
'name' => 'Credits',
'slug' => 'credits',
'namespace' => 'employer',
'type' => 'integer',
'extra' => array('max_length' => 10),
'assign' => 'employers',
'title_column' => true,
'required' => true,
'unique' => true
),
array(
'name' => 'Company name',
'slug' => 'company_name',
'namespace' => 'employer',
'type' => 'relationship',
'extra' => array('choose_stream' => 'company'),
'assign' => 'employers',
'title_column' => true,
'required' => false,
'unique' => false
)
);
$this->streams->fields->add_fields($employer_fields);
//Fields for company stream
$company_fields = array(
array(
'name' => 'Username',
'slug' => 'username',
'namespace' => 'company',
'type' => 'relationship',
'extra' => array('choose_stream' => 'employers'),
'assign' => 'company',
'title_column' => true,
'required' => false,
'unique' => false
),
array(
'name' => 'E-mail',
'slug' => 'email',
'namespace' => 'company',
'type' => 'email',
'assign' => 'company',
'title_column' => true,
'required' => true,
'unique' => false
),
array(
'name' => 'Company Name',
'slug' => 'company_name',
'namespace' => 'company',
'type' => 'text',
'extra' => array('max_length' => 100),
'assign' => 'company',
'title_column' => true,
'required' => true,
'unique' => true
),
array(
'name' => 'Logo',
'slug' => 'logo',
'namespace' => 'company',
'type' => 'image',
'extra' => array('folder' => 'upload'),
'assign' => 'company',
'title_column' => true,
'required' => false,
'unique' => false
),
array(
'name' => 'Designation',
'slug' => 'designation',
'namespace' => 'company',
'type' => 'text',
'extra' => array('max_length' => 100),
'assign' => 'company',
'title_column' => true,
'required' => false,
'unique' => false
),
array(
'name' => 'Contact No.1',
'slug' => 'contact1',
'namespace' => 'company',
'type' => 'integer',
'extra' => array('max_length' => 10),
'assign' => 'company',
'title_column' => true,
'required' => true,
'unique' => false
),
array(
'name' => 'Contact No.2',
'slug' => 'contact2',
'namespace' => 'company',
'type' => 'integer',
'extra' => array('max_length' => 10),
'assign' => 'company',
'title_column' => true,
'required' => false,
'unique' => false
),
array(
'name' => 'Contact No.3',
'slug' => 'contact3',
'namespace' => 'company',
'type' => 'integer',
'extra' => array('max_length' => 10),
'assign' => 'company',
'title_column' => true,
'required' => false,
'unique' => false
),
array(
'name' => 'Address',
'slug' => 'address',
'namespace' => 'company',
'type' => 'textarea',
'assign' => 'company',
'title_column' => true,
'required' => true,
'unique' => false
),
array(
'name' => 'Billing ddress',
'slug' => 'billing_address',
'namespace' => 'company',
'type' => 'textarea',
'assign' => 'company',
'title_column' => true,
'required' => true,
'unique' => false
),
);
$this->streams->fields->add_fields($company_fields);
//Fields for company stream
$job_desc_fields = array(
array(
'name' => 'Username',
'slug' => 'username',
'namespace' => 'company',
'type' => 'relationship',
'extra' => array('choose_stream' => 'employers'),
'assign' => 'jobs',
'title_column' => true,
'required' => false,
'unique' => false
),
array(
'name' => 'Key Skills',
'slug' => 'keyskills',
'namespace' => 'company',
'type' => 'textarea',
'assign' => 'jobs',
'title_column' => true,
'required' => true,
'unique' => false
),
array(
'name' => 'Position Summary',
'slug' => 'position_summary',
'namespace' => 'company',
'type' => 'textarea',
'assign' => 'jobs',
'title_column' => true,
'required' => false,
'unique' => false
),
array(
'name' => 'Experience',
'slug' => 'experience',
'namespace' => 'company',
'type' => 'text',
'assign' => 'jobs',
'title_column' => true,
'required' => true,
'unique' => false
),
array(
'name' => 'Industry',
'slug' => 'industry',
'namespace' => 'company',
'type' => 'text',
'assign' => 'jobs',
'title_column' => true,
'required' => false,
'unique' => false
),
array(
'name' => 'Functional Area',
'slug' => 'functional_area',
'namespace' => 'company',
'type' => 'text',
'assign' => 'jobs',
'title_column' => true,
'required' => false,
'unique' => false
),
array(
'name' => 'Salary',
'slug' => 'salary',
'namespace' => 'company',
'type' => 'text',
'assign' => 'jobs',
'title_column' => true,
'required' => false,
'unique' => false
),
array(
'name' => 'Start Date',
'slug' => 'start_date',
'namespace' => 'company',
'type' => 'datetime',
'assign' => 'jobs',
'extra' => array('input_type' => 'datepicker'),
'title_column' => true,
'required' => false,
'unique' => false
),
array(
'name' => 'Job Type',
'slug' => 'job_type',
'namespace' => 'company',
'type' => 'choice',
'assign' => 'jobs',
'extra' => array('input_type' => 'datepicker'),
'title_column' => true,
'required' => false,
'unique' => false
),
);
return true;
}
public function uninstall()
{
$this->load->driver('Streams');
$this->streams->utilities->remove_namespace('employer');
return true;
}
public function upgrade($old_version)
{
// Your Upgrade Logic
return true;
}
public function help()
{
// Return a string containing help info
// You could include a file and return it here.
return "No documentation has been added for this module.<br />Contact the module developer for assistance.";
}
}
I had earlier "installed" the module, but under the uninstall() function, I forgot to add the code for removing all the streams(P.S: I had 3 streams with separate namespaces defined in the install() function). I only had the following:
$this->streams->utilities->remove_namespace('employer');
Due to this, the other streams were 'existing' in the system although on the face of it, the module was uninstalled, and whenever I tried to reinstall the module, I got the error Could not install module, since while creating tables/streams in the database, the query returned FALSE as the tables already existed.
However, I then added the code to remove all the other streams, by simply including their namespaces in the call for example:
$this->streams->utilities->remove_namespace('employer');
$this->streams->utilities->remove_namespace('job');
$this->streams->utilities->remove_namespace('company');
..and the problem was fixed!