How to merge two arrays into one in CodeIgniter? - php

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

Related

How to add condition in array

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,
)); ?>

How to create fieldset collection using form factory in zf2

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.

Codeigniter: Parsing array data in view in a loop

i want to multidimensional array to my view and then use this array to build form
This is what i've in my controller
function signin(){
$attributes = array(
'name' =>
array(
'name' => 'name',
'type' => 'text',
'placeholder' => '' ,
'value' => 'value'
),
'password' =>
array(
'name' => 'name',
'type' => 'password',
'placeholder' => '',
),
'gender' =>
array(
'name' => 'name',
'type' => 'select',
'value'=>
array(
'male','female'
),
),
'usertpye'=>array(
'type' => 'radio',
'seller' => 'seller',
'buyer' => 'buyer'
),
'upload'=>array(
'type' => 'file',
'name' => 'file'
),
'submit'=>array(
'type' => 'submit',
'name' => 'submit',
'value' => 'submit'
)
);
$this->load->view('login',$attributes);
}
in my view login i can access these items like $name or $password but i want to fetch in a loop.really have no idea how can i do it please help.
The load function receives an array, which keys then parses as variables in the view. Thus, you get variables like $name, $password etc.
Just add another layer before calling the load function like:
$data['attributes'] = $attributes;
And then, when loading the view do
$this->load->view('login',$data);
Here is the array a bit adjusted:
$attributes = array(
'name' =>
array(
'name' => 'name',
'type' => 'text',
'placeholder' => '' ,
'value' => 'value'
),
'password' =>
array(
'name' => 'name',
'type' => 'password',
'placeholder' => '',
),
'gender' =>
array(
'name' => 'name',
'type' => 'select',
'options' => array(
'male' => 'Male',
'female' => 'Female'
),
),
'usertpye'=>array(
'type' => 'radio',
'values' => array(
'seller' => 'seller',
'buyer' => 'buyer'
)
),
'upload'=>array(
'type' => 'file',
'name' => 'file'
),
'submit'=>array(
'type' => 'submit',
'name' => 'submit',
'value' => 'submit'
)
);
Here is how it would look like with the form helper of CI (this would go in the view, remember to first load the helper in the controller):
echo form_open('email/send');
foreach($attributes as $key=>$attribute) {
echo form_label($key).'<br/>';
if($attribute['type'] == 'select') {
echo form_dropdown($attribute['name'],$attribute['options']).'<br/>';
} elseif($attribute['type'] == 'radio') {
foreach ($attribute['values'] as $value) {
echo form_label($value);
echo form_radio(array('name' => $key, 'value' => $value)).'<br/>';
}
} else {
echo form_input($attribute).'<br/>';
}
}
Note I did some adjustments to your initial attributes array to make it work, but you'd still need to improve its structure, add unique names for all items etc.

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)

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.

Categories