Readonly form fields - php

So I have an PHP file that includes a form so that users can post jobs on my website. I want to make some fields of this form ReadOnly to the user. How?
public static function init_fields() {
if ( self::$fields )
return;
self::$fields = apply_filters( 'submit_job_form_fields', array(
'job' => array(
'job_category' => array(
'label' => __( 'Job category', 'job_manager' ),
'type' => 'select',
'required' => true,
'options' => self::job_categories(),
'placeholder' => '',
'priority' => 3
),
'job_description' => array(
'label' => __( 'Description', 'job_manager' ),
'type' => 'text',
'required' => true,
'placeholder' => '',
'priority' => 4
),
I know this is probably easy to do, but for some reason I can't find how to do it.

Use the disabled attribute:
'job_description' => array(
'label' => __( 'Description', 'job_manager' ),
'type' => 'text',
'required' => true,
'placeholder' => '',
'priority' => 4,
'disabled' => 'true' //html disabled input
)

This is a proprietary config file. You need to look at the code that is parsing it and converting to HTML. It may allow passing through variables like readonly, or may not.

Try passing 'readonly' => true in your array.
Remember that someone can change the value of the readonly field using an inspector like Firebug, Chrome Developer Tools, etc.

Related

Custom Class for a plugin options give error, Notice: Trying to access array offset on value of type bool in C

I have custom options for a plugin in wordpress, it work fine until I make the upgrade,
I am using a custom class that find in github
class RationalOptionPages {
/* ==========================================================================
Vars
========================================================================== */
protected $attributes = array(
'input' => array(
'autocomplete' => false,
'autofocus' => false,
'disabled' => false,
'list' => false,
'max' => false,
'maxlength' => false,
'min' => false,
'pattern' => false,
'readonly' => false,
'required' => true,
'size' => false,
'step' => false,
),
I made a another page for the options
'section-three' => array(
'title' => __( 'Block', 'sample-domain' ),
'fields' => array(
'radio' => array(
'uid' => 'asp_typing_option',
'title' => __( 'Typing Block Option', 'sample-domain' ),
'type' => 'radio',
'value' => 'option-one',
'choices' => array(
'option-one' => __( 'Active', 'sample-domain' ),
'option-two' => __( 'Deactivate', 'sample-domain' ),
),
),
),
),
The problem occur when I reset the website then output an error
Notice: Trying to access array offset on value of type bool in C:\xampp\htdocs\wordpress\wp-content\plugins\redux-framework\init.php on line 3 theerror is for all the options, then i save and the error disappear
I call the options with this way
$opt_sectionas = get_option('sample-page')['asp_section_option'];
$opt_carousel = get_option('sample-page')['asp_carousel_option'];
$opt_animate = get_option('sample-page')['asp_animate_aos_option'];
$opt_columns = get_option('sample-page')['asp_advance_columns_option'];
I solved the problem adding the next to the call options section
this is old version
$opt_sectionas = get_option('sample-page')['asp_section_option'];
$opt_carousel = get_option('sample-page')['asp_carousel_option'];
this is new version
$opt_sectionas = get_option('sample-page')['asp_section_option'] ?? '';
$opt_carousel = get_option('sample-page')['asp_carousel_option'] ?? '';

Wordpress select setting in customizer not outputing proper code

I have a customizer section inside my wordpress theme. I made a setting and a control for an icon. I want my user to be able to choose what icon he wants. I implemented the icomoon icon font with classes like icon-home.
I made a setting and a control for that icon like this:
$wp_customize->add_setting(
'service1_icon',
array(
'default' => 'icon1',
'type' => 'option',
)
);
$wp_customize->add_control(
'service1_icon',
array(
'label' => 'Service 1 Icon',
'section' => 'section_services',
'type' => 'select',
'choices' => array(
'icon1' => 'mobile',
'icon2' => 'home',
),
)
);
and in html/php:
<span class="service-icon icon-<?php echo get_theme_mod('service1_icon', 'icon1'); ?>"></span>
But when in my browser I see the output like this:
<span class="service-icon icon-icon1"></span>
instead of:
<span class="service-icon icon-mobile"></span>
I was trying to fix it and did some stuff that were illogical to me and I fixed it.
First, I removed the type from the setting:
$wp_customize->add_setting(
'service1_icon',
array(
'default' => 'icon1',
)
);
Then I renamed icon1, icon2... to the actual name of the icon:
$wp_customize->add_control(
'service1_icon',
array(
'label' => 'Service 1 Icon',
'section' => 'section_services',
'type' => 'select',
'choices' => array(
'mobile' => 'mobile',
'home' => 'home',
),
)
);

Add onKeyDown Event on Varien Data Form Element in Magento

I'd like to add event on text element
$barcodeFieldset = $form->addFieldset('form_barcode', array('legend' => Mage::helper('adminhtml')->__('Barcode Information')));
$barcodeFieldset->addField('barcode', 'submit', array(
'label' => Mage::helper('adminhtml')->__('Barcode'),
'name' => 'barcode',
'onKeyDown' => "alert('x')",
'required' => true,
'note' => 'If You want to scan barcode, focus to this textfield.',
$this->_isReadonly() => $this->_isReadonly(),
));
onKeyDown doesn't work. How to make it works?
For some reason Magento does not allow this. Here are the attributes that you can specify on a submit element: 'type', 'title', 'class', 'style', 'onclick', 'onchange', 'disabled', 'readonly', 'tabindex'. These come from Varien_Data_Form_Element_Abstract::getHtmlAttributes().
The only field type that allows onkeydown (written in lowercase) is the link field (Varien_Data_Form_Element_Link).
This is my idea.
$barcodeFieldset = $form->addFieldset('form_barcode', array('legend' => Mage::helper('adminhtml')->__('Barcode Information')));
$barcodeFieldset->addField('barcode', 'password', array(
'label' => Mage::helper('adminhtml')->__('Barcode'),
'name' => 'barcode',
'after_element_html' =>
"<script type='text/javascript'>" .
"$('barcode').setAttribute('onKeyDown', 'if(event.keyCode == 13){editForm.submit($(" . "edit_form" . ").action)};');"
. "</script>",
'required' => true,
'note' => 'If You want to scan barcode, focus to this textfield.',
$this->_isReadonly() => $this->_isReadonly(),
));

How to validate multiply select using Zend Framework 2

I am trying to validate a multiply select using input filter, but every time I see a error. The error is "notInArray":"The input was not found in the haystack".(I use ajax but it doesn`t metter).
I will show part of my code to be more clear.
in Controller:
if ($request->isPost()) {
$post = $request->getPost();
$form = new \Settings\Form\AddUserForm($roles);//
$form->get('positions')
->setOptions(
array('value_options'=> $post['positions']));
//.... more code...
When I put print_r($post['positions']); I see:
array(0 => 118, 1 => 119)
in ..../form/UserForm.php I create the multiply element
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
and in the validation file the code is:
$inputFilter->add($factory->createInput(array(
'name' => 'positions',
'required' => false,
'validators' => array(
array(
'name' => 'InArray',
'options' => array(
'haystack' => array(118,119),
'messages' => array(
'notInArray' => 'Please select your position !'
),
),
),
),
What can be the reason every time to see this error, and how I can fix it?
By default selects have attached InArray validator in Zend Framework 2.
If you are adding new one - you will have two.
You should disable default one as follow:
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'options' => array(
'disable_inarray_validator' => true, // <-- disable
),
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
And you should get rid of the additional error message.
Please let us know if that would helped you.

How to set default value for form fields- Magento?

I have an admin form field(textbox),
$fieldset->addField('ajax_time_interval', 'text', array(
'label' => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
'class' => 'required-entry',
'required' => true,
'name' => 'ajax_time_interval',
));
I need to set a default value for this text field. I tried, setting 'value' => '120', in it. But its not working.
$fieldset->addField('ajax_time_interval', 'text', array(
'label' => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
'class' => 'required-entry',
'required' => true,
'name' => 'ajax_time_interval',
'value' => '120',
));
How to set a defalt value in that field?
In my case, setValues() function was overriding the 'value' that I had set for the field.
Cause:
In my _prepareForm() function, I set the value as below:
$fieldset->addField('ajax_time_interval', 'text', array(
'label' => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
'class' => 'required-entry',
'required' => true,
'name' => 'ajax_time_interval',
'value' => '120',
));
At the end of _prepareForm() function, there was the following line of code which would reset the form values:
$form->setValues($model->getData());
Solution:
Set model data before setValues() function as below:
if (!$model->getId()) {
$model->setData('ajax_time_interval', '120');
}
$form->setValues($model->getData());
you can do that by adding "default" attribute in field configurations.
$fieldset->addField('ajax_time_interval', 'text', array(
'label' => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
'class' => 'required-entry',
'required' => true,
'name' => 'ajax_time_interval',
'default' => '120',
));
Remove the last semicolon....
Check this site help-me a lot:
http://www.excellencemagentoblog.com/magento-admin-form-field
Gl mf

Categories