how can i change fields from text to option box? - php

i want to change the type of car_engine from text to option but i do not even understand what this code means and in which language i shall write my modification.
i have tried no thing because i had no idea where should i start.
public static function front_fields( $fields ) {
$fields[] = [
'forms' => ['car', ],
'type' => 'fieldset',
'id' => 'car',
'legend' => esc_html__( 'Car', 'listing-manager' ),
'collapsible' => true,
'fields' => [
[
'id' => LISTING_MANAGER_LISTING_PREFIX . 'car_engine',
'type' => 'text',
'label' => esc_html__( 'Engine', 'listing-manager' ),
'required' => false,
],

Try to use this way (untested):
public static function front_fields( $fields ) {
$fields[] = [
'forms' => ['car', ],
'type' => 'fieldset',
'id' => 'car',
'legend' => esc_html__( 'Car', 'listing-manager' ),
'collapsible' => true,
'fields' => [
[
'id' => LISTING_MANAGER_LISTING_PREFIX . 'car_engine',
'type' => 'select',
'label' => esc_html__( 'Engine', 'listing-manager' ),
'required' => false,
'options' => [ //options may be values
'alabama' => 'Alabama',
'alaska' => 'Alaska'
],
'placeholder' => '',
],
]
);

Related

Adding a conditional statement to an array of fields?

I need to include a conditional statement into the following array called $fields.
$fields = [
'program_id' => [
'type' => 'select',
'label' => 'Program',
'opts' => ["One", "Two", "Three"],
],
'name' => [
'label' => 'Job Name'
],
'start_date' => [
'class' => 'date-picker',
'label' => 'Job Starts' . $req,
'val' => $job->start_date ? dateToPicker($job->start_date) : null
],
'end_date' => [
'class' => 'date-picker',
'label' => 'Job Ends' . $req,
'val' => $job->end_date ? dateToPicker($job->end_date) : null
],
'section' => [
'type' => 'hidden',
'val' => 'details'
],
];
if (!$job->id && $program)
{
$fields['job_copy'] = [
'label' => 'Copy Job From',
'type' => 'select',
'textAsValue' => false,
'_comment' => 'Selecting a job here will copy all job information except the name.',
'opts' => array_replace([0 => '-- Select Job --'], $program->jobs()->lists('name', 'id')->all())
];
}
$fields[] = [
'type' => 'submit',
'label' => "Save",
'class' => 'btn btn-primary !important'
];
}
I need to move the conditional statement to the top so that it is the first thing displayed on the form. However, when I move it to the top it disappears. How can I integrate the conditional check into the top of the form as opposed to at the bottom where it currently displays?
$fields = [];
if (!$job->id && $program)
{
$fields['job_copy'] = [
'label' => 'Copy Job From',
'type' => 'select',
'textAsValue' => false,
'_comment' => 'Selecting a job here will copy all job information except the name.',
'opts' => array_replace([0 => '-- Select Job --'], $program->jobs()->lists('name', 'id')->all())
];
}
$fields2 = [
'program_id' => [
'type' => 'select',
'label' => 'Program',
'opts' => ["One", "Two", "Three"],
],
'name' => [
'label' => 'Job Name'
],
'start_date' => [
'class' => 'date-picker',
'label' => 'Job Starts' . $req,
'val' => $job->start_date ? dateToPicker($job->start_date) : null
],
'end_date' => [
'class' => 'date-picker',
'label' => 'Job Ends' . $req,
'val' => $job->end_date ? dateToPicker($job->end_date) : null
],
'section' => [
'type' => 'hidden',
'val' => 'details'
],
];
$fields = array_merge($fields,$fields2);
$fields[] = [
'type' => 'submit',
'label' => "Save",
'class' => 'btn btn-primary !important'
];

ZF2's custom validator's $context suddenly gives 'bar'=>array() when posting bar[0][foo]='xyz'

I have a custom validator using $context that has been working for over a year.
The $context contains all posted parameters except for "bar" which is an empty array although it is posted as bar[0][description]. The array used to contain one or more arrays with key-value-pairs.
This seems to be the case since updating from version 2.3.2 to 2.5.1
I can't even check the 2.5 documentation since it's not available, Google didn't turn up anything close to the issue, ... was the behaviour changed or is this a bug?
Edit:
In .../Lorem/Form/FooForm.php I include a fieldset:
public function __construct() {
// ...
$this->add(array(
'type' => 'Collection',
'name' => 'bar',
'options' => array(
'label'=> '',
'count' => 1,
'should_create_template' => TRUE,
'allow_add' => TRUE,
'use_as_base_fieldset' => TRUE,
'target_element' => array(
'type' => 'Lorem\Fieldset\BarFieldset'
)
)
));
}
In .../Lorem/Fieldset/BarFieldset.php I have:
public function __construct() {
parent::__construct('bar');
$this->setHydrator(new ReflectionHydrator())
->setObject(new Bar());
$this->add(array(
'name' => 'description',
'type' => 'Textarea',
'attributes' => array(
'rows' => 3,
'cols' => 40,
),
));
$this->add(array(
'name' => 'amount',
'attributes' => array(
'type' => 'Text',
'class' => 'input-bar-amount',
'maxlength' => 9,
),
));
// ...
}
public function getInputFilterSpecification()
{
return array(
'description' => array(
'required' => TRUE,
'filters' => array(
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => TRUE,
'options' => array(
'messages' => array(
'isEmpty' => 'Please enter a description.',
),
),
),
),
),
'amount' => array(
'required' => TRUE,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => TRUE,
'options' => array(
'messages' => array(
'isEmpty' => 'Please enter amount.',
),
),
),
array(
'name' => 'Float',
'break_chain_on_failure' => TRUE,
'options' => array(
'locale' => 'de',
'messages' => array(
'floatInvalid' => 'Invalid input. String, Integer oder Float expected.',
'notFloat' => 'Input ist not a floating point number.',
),
),
),
array(
'name' => 'Between',
'break_chain_on_failure' => TRUE,
'options' => array(
'min' => -1000000,
'max' => 1000000,
'messages' => array(
'notBetween' => "Only values between '%min%' and '%max%' EUR are allowed.",
'notBetweenStrict' => "Only values between '%min%' and '%max%' EUR are allowed.",
),
),
),
),
),
// ...
);
}

How to configure ObjectMultiCheckBox to persist an empty element?

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');

zend framework 2 with doctrine 2 $form->getData() in controller not returning all fields set using $form->setData()

I have set my form for validation using $form->setData().
After validation I am not receiving all of my properties back using $form->getData().
I am using following lines in controller
and somehow $form->getData() is not returning all fields anyone has any idea why?
if ($request->isPost())
{
$company = new Company();
$form->setInputFilter($company->getInputFilter());
$form->setData($request->getPost());
print_r($request->getPost()); // getPost shows all fields fine
if ($form->isvalid())
{
print_r($form->getData()); // is returning only select and text type fields which are in input filter. why?
}
}
my form is looks like this.
class Companyform extends Form
{
public function __construct()
{
parent::__construct('company');
$this->setAttribute ('method', 'post');
$this->setAttribute ('class', 'form-horizontal');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden'
),
));
$this->add ( array (
'name' => 'title',
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'id' => 'title',
'options' => array(
'mr' => 'Mr',
'miss' => 'Miss',
'mrs' => 'Mrs',
'dr' => 'Dr'
),
'value' => 'mr'
),
'options' => array(
'label' => 'Title'
)
));
$this->add(array(
'name' => 'fname',
'attributes' => array(
'id' => 'fname',
'type' => 'text',
'placeholder' => "First Name",
),
'options' => array(
'label' => 'First Name'
)
));
$this->add(array(
'name' => 'surname',
'attributes' => array(
'id' => 'surname',
'type' => 'text',
'placeholder' => "Surname Name",
),
'options' => array(
'label' => 'Surname Name'
)
));
$this->add(array(
'name' => 'companyName',
'attributes' => array(
'id' => 'companyName',
'type' => 'text',
'placeholder' => "Company Name",
),
'options' => array(
'label' => 'Company Name'
)
));
$this->add(array(
'name' => 'address1',
'attributes' => array(
'id' => 'address1',
'type' => 'text',
'placeholder' => "Address Line 1",
),
'options' => array(
'label' => 'Address'
)
));
$this->add(array(
'name' => 'address2',
'attributes' => array(
'id' => 'address2',
'type' => 'text',
'placeholder' => "Address Line 2",
),
'options' => array(
'label' => 'Address'
)
));
$this->add(array(
'name' => 'address3',
'attributes' => array(
'id' => 'address3',
'type' => 'text',
'placeholder' => "Address Line 3",
),
'options' => array(
'label' => 'Address'
)
));
$this->add(array(
'name' => 'btnsubmit',
'attributes' => array(
'id' => 'btnsubmit',
'type' => 'submit',
'value' => 'Add',
'class' => 'btn btn-primary'
),
));
}
}
and This is the input filter which I am using in entity company
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'companyName',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 255,
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
Every single Form-Element has to get validated! Even if the validator is empty like
$inputFilter->add($factory->createInput(array(
'name' => 'title'
)));
Only validated data get's passed from the form.

"Could not install module" error in PyroCMS

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!

Categories