Array:
$forms = array (
'title' => 'Form Title',
'subtext' => 'Sub Text',
'fields' => [
[
'name' => 'Question A',
'type' => 'input',
'placeholder' => 'Eg. Test',
'value' => '',
'required' => true,
'size' => '6'
],
[
'name' => 'Question B',
'type' => 'textarea',
'placeholder' => 'Eg. Test',
'value' => '',
'required' => true,
'size' => '6'
]
]
);
Code:
<?php
$forms_keys = array_keys($forms['fields']);
foreach ($forms_keys as $forms_key)
{
?>
<div class="form-group col-md-12">
<label for="question3"><?php echo $forms_key['name']; ?></label>
</div>
<?php
}
?>
Im trying to get the fields => name to display inside the label. I tried the above but cant get it working, When I echo out $forms_key, I get "0" and "1"
From PHP Manual:
array_keys — Return all the keys or a subset of the keys of an array
You have an array of keys but no associated data as you've stripped this out. Remove array_keys from your first line:
<?php
$forms_keys = $forms['fields'];
foreach ($forms_keys as $forms_key)
{
?>
<div class="form-group col-md-12">
<label for="question3"><?php echo $forms_key['name']; ?></label>
</div>
<?php
}
?>
Your $forms['fields'] array does not have keys, it contains two array elements which contains keys for their inner elements. You can check them with $forms["fields"][0]["name"] or $forms["fields"][1]["name"].
Remove the array_key and set it as below and fields is a sub array. You need to take of it also.
<?php
$forms_keys = $forms['fields'];
for ($i = 0; $i < count($forms_keys); $i++) {
?>
<div class="form-group col-md-12">
<label for="question3"><?php echo $forms_keys[$i]['name']; ?></label>
</div>
<?php
}
?>
Related
I'm trying to make search form using cake3 , to get the values between 2 dates . But I'm getting error , and don't know why .
Here's my form :
<?php echo $this->Form->create('CustodyKeys', array('type' => 'get',
'url' => array(
'controller' => 'CustodyKeys','action' => 'resultypeLongTerm'
)));?>
<div class="row">
<div class="form-group col-md-12">
<?php
echo $this->Form->input('keys_custody_status_name', ['placeholder'=>__('Enter Custody statue'),'class' => 'form-control','label'=>__('Custody statue')]);
?>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<?php
echo $this->Form->input('received_date_from', ['type' => 'text', 'placeholder' => __('Received Date From'), 'label' => __('Received Date From'), 'class' => 'form-control hasGorgianDatePicker','value'=>'from']);
?>
</div>
<div class="form-group col-md-6">
<?php
echo $this->Form->input('received_date_to', ['type' => 'text', 'placeholder' => __('Received Date to'), 'label' => __('Received Date to'), 'class' => 'form-control hasGorgianDatePicker']);
?>
</div>
And here's my function at the controller :
$date_start = 'received_date_from';
$date_end = 'received_date_to';
$conditions = $this->CustodyKeys->find(
'all',array('conditions'=>array('CustodyKeys.received_date_from >=' => array($date_start),
'CustodyKeys.received_date_to <=' => array($date_end)))
) ;
$this->set('data', $conditions);
and here's the ctp to show the result of the search :
<?php foreach ($data as $custodyKey): ?>
<tr>
<td><?= $custodyKey->id ?></td>
The debug giving me that result :
'(help)' => 'This is a Query object, to get the results execute or iterate it.',
'sql' => 'SELECT CustodyKeys.id AS CustodyKeys__id, CustodyKeys.holders_keys_id AS CustodyKeys__holders_keys_id, CustodyKeys.keys_management_id AS CustodyKeys__keys_management_id, CustodyKeys.keys_custody_type_id AS CustodyKeys__keys_custody_type_id, CustodyKeys.keys_custody_status_id AS CustodyKeys__keys_custody_status_id, CustodyKeys.received_date AS CustodyKeys__received_date, CustodyKeys.return_date AS CustodyKeys__return_date, CustodyKeys.last_status_date AS CustodyKeys__last_status_date, CustodyKeys.notes AS CustodyKeys__notes FROM custody_keys CustodyKeys WHERE (CustodyKeys.received_date_from >= :c0 AND CustodyKeys.received_date_to <= :c1)',
'params' => [
':c0' => [
'value' => [
(int) 0 => 'received_date_from'
],
'type' => null,
'placeholder' => 'c0'
],
':c1' => [
'value' => [
(int) 0 => 'received_date_to'
],
'type' => null,
'placeholder' => 'c1'
]
],
To convert a query object into an array you can work with use:
$data->toArray()
try this
$conditions = $this->CustodyKeys->find('all',
array('conditions'=>
array('CustodyKeys.received_date_from >= '. $date_start,
'CustodyKeys.received_date_to <= '. $date_end))
);
I need to get the real value, instead of the number position of a Select form field
I explain it with one example:
first in the form
$procesa=new Querys();
$datosAsignaturas=$procesa->getDatosAsignaturas();
$groups = array();
foreach ($datosAsignaturas as $id => $list) {
$groups[$id] =$list["nombreA"];
}
$selection= $factory-> createElement(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'subjects',
'attributes' => array(
'id' => 'subjects',
'options' => $groups
),
));
$this->add($selection);
Second, the view
<div class="form_element">
<?php $element = $form->get('subjects');
?>
<label>
<?php echo $element->getOption('value'); ?>
</label>
<?php echo $this->formSelect($element); ?>
</div>
third
["subjects"]=> string(1) "1"
i need something like this
["subjects"]=> string(1) "Maths"
Proper assignement of values to a Zend\Form\Element\Select is through the means of value_options inside options or using the element function setValueOptions(). Here a simple example:
$form->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'language',
'options' => array(
'label' => 'Which is your mother tongue?',
'empty_option' => 'Please choose your language',
'value_options' => array(
'0' => 'French',
'1' => 'English',
'2' => 'Japanese',
'3' => 'Chinese',
),
)
));
Now if you need to access the value options like this, you simply call getValueOptions() on the element and you'll receive exactly the same array as above. Then you could do something like this (which I'm assuming is what you're trying to do):
$elemLanguage = $form->get('language');
echo "<select name='{$elemLanguage->getName()}'>\n";
foreach($elemLanguage->getValueOptions() as $id => $language) {
echo "<option value='{$id}'>{$id} - {$language}</option>\n";
}
echo '</select>';
Maybe you mean :
<div class="form_element">
<?php $element = $form->get('subjects');
?>
<label>
<?php echo $element->getOption('label'); //-------- label instead value?>
</label>
<?php echo $this->formSelect($element); ?>
</div>
And about the form
$procesa=new Querys();
$datosAsignaturas=$procesa->getDatosAsignaturas();
$groups = array();
foreach ($datosAsignaturas as $id => $list) {
$groups[$id] =$list["nombreA"];
}
$selection= $factory-> createElement(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'subjects',
'attributes' => array(
'id' => 'subjects',
'options' => $groups
),
'options'=>array(
'label'=>"Your label",
'description' => 'your description',
'value_options' => array(
'0' => 'French',
'1' => 'English',
'2' => 'Japanese',
'3' => 'Chinese',
),
)
));
$this->add($selection);
I have a foreach that show many forms with the same action ending with diferente id's.
But, the tag <form> just appears in the first form. All others, the fields appears, but don't the <form>
I tried to put the id for the form different in the loop. But doesn't work.
The code:
<?php echo $this->Form->create(null, array(
'url' => array('controller' => 'menus', 'action' => 'aprovar', $procuracao['Attorney']['id']), 'id' => $procuracao['Attorney']['id']
)); ?>
<div class="control-group">
<label class="control-label">Alçada:</label>
<div class="controls">
<?php echo $this->Form->input ('alcada', array('type' => 'select', 'label' => FALSE, 'options' => array(
'Até 10.000' => 'Até 10.000',
'Até 50.000' => 'Até 50.000',
'Acima de 100.000' => 'Acima de 100.000',
'Acima de 500.000' => 'Até 500.000',),
'empty' => 'Selecione')); ?>
</div>
</div>
<div class="control-group">
<label class="control-label">Validade:</label>
<div class="controls">
<?php echo $this->Form->input('validade', array('label' => FALSE, 'type' => 'text')); ?>
</div>
</div>
<?php echo $this->Form->submit('Ok', array('class' =>'btn btn-success pull-left', 'div' => false)); ?>
</div>
The field "Alçada" and "Validade" appears correctly. But the tag <form> just appears in the first element.
You are not ending the form.
echo $this->Form->create(null, array(
'id' => 'your-form-'.$i, //that $i is the index of the foreach, for example
'url' => array('controller' => 'menus', 'action' => 'aprovar', $procuracao['Attorney']['id']), 'id' => $procuracao['Attorney']['id']
));
//all inputs and other stuff
echo $this->Form->end(array('label'=>'Ok', 'class' =>'btn btn-success pull-left', 'div' => false));
all that inside the foreach you're using.
Here is the reference of that function in the docs. But basically, it does this
Closes an HTML form, cleans up values set by FormHelper::create(), and
writes hidden input fields where appropriate
I just want to create a Label/value row like this:
<label>
<span>My Label:</span><span>My value from my object</span>
</label>
So what should I put in the type attribute ?
$this->add(array (
'name' => 'myFieldName',
'attributes' => array(
'type' => '???????',
),
'options' => array (
'label' => 'My Label:',
),
));
$this->add(array (
'name' => 'myFieldName',
'type' => 'hidden',
'options' => array (
'label' => 'My Label:',
),
));
And in view
<?php $element = $form->get('myFieldName') ?>
<label>
<span><?php echo $element->getLabel() ?></span>
<span><?php echo $this->escapeHtml($element->getValue()) ?></span>
</label>
All my other form elements validate BUT my text area. Thoughts onto maybe why with the following code?
<?php $attributes = array('cols' => '30', 'rows' => '5', 'id' => 'article', 'class' => 'required') ?>
<div>
<?php echo form_textarea('article', '', $attributes); ?>
</div>
EDIT:
I'm using the jQuery validation class.
<div class="section _100">
<label for="article">Article</label>
<div>
<textarea name="article" cols="40" rows="10" Array></textarea>
</div>
</div>
SO now I"m wondering why its showing that Array like that when it should be finishing putting the attributes like the class part.
Per the Form_helper documentation, you can generate a textarea by passing a name and attribute value as two arguments or by passing all your attributes as an array as a single argument.
Notice in your code, you have a 3rd argument. That is reserved for additional data, like JavaScript. It's just appended within the textarea tag as a string - which is why you're seeing Array.
Create your textarea like this, but including your name in the $attributes array:
$attributes = array(
'name' => 'article',
'cols' => '30',
'rows' => '5',
'id' => 'article',
'class' => 'required'
);
echo form_textarea($attributes);
When you pass array of attributes, then it should be the only form_textarea function. Try this:
<?php $attributes = array('name' => 'article', 'cols' => '30', 'rows' => '5', 'id' => 'article', 'class' => 'required') ?>
<div>
<?php echo form_textarea($attributes); ?>
</div>