How to add condition in array - php

I want to add an array condition in input field. if flag == 1 then it is a required field otherwise it is not required field
<?php echo $this->Form->input('course_workbook_answer_file', array(
'type' => 'file',
'label' => false,
'id' => 'course_workbook_answer_file',
'class' => 'form-control',
'name' => 'data[CourseWorkbook][0][course_workbook_answer_file]',
$port_flag == 0 ? 'required' => 'required' : Null
)); ?>

You should change your condition to value instead of complete index.
<?php echo $this->Form->input('course_workbook_answer_file', array(
'type' => 'file',
'label' => false,
'id' => 'course_workbook_answer_file',
'class' => 'form-control',
'name' => 'data[CourseWorkbook][0][course_workbook_answer_file]',
'required' => ($port_flag==0)?'required':Null,
)); ?>

Related

Preselecting an option from a select field in a fields array?

Observe the following $fields array, which is used as an input form in a blade:
$fields = [
'user_id' => [
'label' => 'Opportunity Owner' . $req,
'type' => 'select',
'class' => 'select2',
'opts' => User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()
],
'name' => [
'label' => 'Opportunity Name' . $req,
],
'agent_id' => [
'label' => 'Agent',
'type' => 'select',
'class' => 'select2',
'textAsValue' => false,
'opts' => array_replace([0 => '-- Select Sales Rep --'],
User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()),
],
'description' => [
'label' => 'Description',
'type' => 'textarea'
],
[
'type' => 'submit',
'label' => 'Save',
'class' => 'btn btn-primary !important'
]
];
In the 'agent_id' part, I'd like to pre-select a value if the user has a value preassigned. I know how to get the info from the user, but I am lost as to how to 'select' an option in the array within the 'agent_id' field. I need all options to show in the select, but I want to be able to have one 'selected' based on the agent_id number linked to the user. I tried the following:
'agent_id' => [
'label' => 'Agent',
'type' => 'select',
'class' => 'select2',
'textAsValue' => false,
'opts' => array_replace([0 => '-- Select Sales Rep --'],
User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()),
'selected' => {{appropriate number here}}
],
But that did not work. How could I go about doing this?
Add selected value without any index.
Try this:
'agent_id' => [
'label' => 'Agent',
'type' => 'select',
'class' => 'select2',
'textAsValue' => false,
'opts' => array_replace([0 => '-- Select Sales Rep --'],
User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()),
{{appropriate number here}}
],

CakePHP input stopped working

I'm completely new with cake, so I have no clue what happened.
this code works :
echo $this->Form->input('phone',
array(
'required' => false,
'div' => 'form-group',
'class' => 'form-control',
'label' => array(
'class' => 'control-label',
'text' => __d('admin', 'Tel. numeris')
),
'placeholder' => __d('admin', 'Tel. numeris'),
'error' => array(
'attributes' => array(
'class' => 'alert alert-danger'
)
)
)
);
this one doesn't :
echo $this->Form->input('email',
array(
'required' => false,
'div' => 'form-group',
'class' => 'form-control',
'label' => array(
'class' => 'control-label',
'text' => __d('admin', 'El.paštas')
),
'placeholder' => __d('admin', 'El.paštas'),
'error' => array(
'attributes' => array(
'class' => 'alert alert-danger'
)
)
)
);
and for example if I change email to, lets say, emailas, it works too(but then it doesnt do anything) :
echo $this->Form->input('emailas',
array(
'required' => false,
'div' => 'form-group',
'class' => 'form-control',
'label' => array(
'class' => 'control-label',
'text' => __d('admin', 'El.paštas')
),
'placeholder' => __d('admin', 'El.paštas'),
'error' => array(
'attributes' => array(
'class' => 'alert alert-danger'
)
)
)
);
could someone please help me or at least tell me where to look? The input stopped working out of the blue, so maybe theres a possibility to somehow restart whole plugin? THANK YOU IN ADVANCE
P.S.
This is how the input field looks atm vs how it should look like : http://imgur.com/bBrkgxM
Try this:
echo $this->Form->input('email',
array(
'required' => false,
'div' => 'form-group',
'class' => 'form-control',
'type' => 'email',
'label' => array(
'class' => 'control-label',
'text' => __d('admin', 'El.paštas')
),
'placeholder' => __d('admin', 'El.paštas'),
'error' => array(
'attributes' => array(
'class' => 'alert alert-danger'
)
)
)
);
And make sure you have email field in database and nowhere is overriding email field (in controller / model)

Set default radio button in Cakephp

The below code creates some radio buttons and html to go with them, it works well however I now want to set the first radio button as the default selected one and im not sure what needs to be added.
<?php
for ($i =0; $i < count($packages); $i++){
echo "<div class='package-outter'>";
echo "Name: ".$packages[$i]['Package']['name']."<br>";
echo "Number of Campaigns (per month): ".$packages[$i]['Package']['quantity']."<br>";
echo "Price: ".$packages[$i]['Package']['price']."<br>";
if ($i == 0){
echo $this->Form->input('package', array(
'type' => 'radio',
'options' => array($packages[$i]['Package']['id'] => $packages[$i]['Package']['name'],),
'class' => 'testClass',
));
}else{
echo $this->Form->input('package', array(
'type' => 'radio',
'options' => array($packages[$i]['Package']['id'] => $packages[$i]['Package']['name'],),
'class' => 'testClass',
'hiddenField' => false, // added for non-first elements
));
}
echo "</div>";
}
?>
I'm not really familiar with CakePHP but a quick Google Search gave me this
$options = array(
'standard' => 'Standard',
'pro' => 'Pro'
);
$attributes = array(
'legend' => false,
'value' => $foo
);
echo $this->Form->radio('type', $options, $attributes);
So you should add the attribute 'value' and set it as your default selected radio
If you try the associated value option it should work.
<?php
echo $this->Form->input('status', array(
'div' => true,
'label' => true,
'type' => 'radio',
'legend' => false,
'required' => false,
'hiddenField'=>false,
'options' => array(
1 => 'High',
2 => 'Medium',
3 => 'Low'
)
'value' => 'Medium'
) ); ?>
<?php
echo $this->Form->input('status', array(
'div' => true,
'label' => true,
'type' => 'radio',
'legend' => false,
'required' => false,
'hiddenField'=>false,
'options' => $status,
'value' => 1 => default value of input
));
Try this
$this->Form->control('company_id', [
'label' => 'Escolha a empresa',
'options' => $companies ,
'value'=> key($companies),
'type' => 'radio',
'required' => true
])

How to merge two arrays into one in CodeIgniter?

This is my config file which contains form attributes in CodeIgniter.
$config['reg_attribute'] = array(
'form' => array(
'id' => 'reg-form',
'class' => 'form-horizontal',
'role' => 'form'
),
'name' => array(
'id'=>'reg-name',
'class' => 'form-control',
'name'=>'name',
'placeholder' => 'Enter name',
'value'=>set_value('name')
),
'gender' => array(
'id'=>'reg-reg',
'class' => 'form-control',
'name'=>'gender',
'value'=>set_value('gender')
),
'contact_no' => array(
'id'=>'reg-reg',
'class' => 'form-control',
'name'=>'contact_no',
'placeholder' => 'Enter Phone number',
'value'=>set_value('contact_no')
),
'email'=> array(
'id'=>'reg-email',
'class' => 'form-control',
'name'=>'email',
'placeholder' => 'Enter Email',
'value'=>set_value('email')
),
'password' =>array(
'id'=>'reg-password',
'class' => 'form-control',
'name'=>'password',
'placeholder'=>'Enter Password',
'value'=>set_value('password')
),
'confirm_password' =>array(
'id'=>'reg-password',
'class' => 'form-control',
'name'=>'confirm_password',
'placeholder'=>'Enter Confirm Password',
'value'=>set_value('confirm_password')
),
'submit' =>array(
'id' => 'btn-login',
'class' => 'btn btn-success',
'name' => 'submit',
'value' => 'Register'
)
);
Here I'm loading the form attributes from config and storing it into
an array $data["reg_attrib"]
$this->config->load('reg_rules');
$data["reg_attrib"] = $this->config->item("reg_attribute");
I have an another array which is $add_form
$add_form = array(
'action' => base_url('admin/user/insert'),
'title' => 'Add User',
);
Now I want to merge both the array's into a single array
and send it to view i.e to $add_attributes.
$this->admin_layout->view('admin/add_user',$add_attributes);
Simply merge them with array_merge(). Try this -
$add_attributes = array_merge($data["reg_attrib"], $add_form);
use array_merge() function like
$merged_arr = array_merge($arr1,$arr2);
in your case it will be same as #BOSE suggested:
$add_attributes = array_merge($data["reg_attrib"], $add_form);

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

Categories