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>
Related
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
}
?>
I am trying to do foreach inside array.
I am trying to generate the select box using array value everything seems ok when i give a value manually.
array(
'name' => __('Testing Selection', 'test'),
'id' => 'testing',
'css' => 'min-width:150px;',
'std' => '2',
'default' => '2',
'type' => 'select',
'options' => array(
'1' => __('Test1', 'test'),
'2' => __('Test2', 'test'),
'3' => __('Test3', 'test'),
),
),
In the above code options key contains three values like 1, 2, 3 and the above code is working. But i want to loop all product id here using foreach but it seems not working for me may be i am trying a wrong way. I know foreach inside array is invalid that's why i am trying this way.
$foos = array(
'name' => __('Testing Selection', 'test'),
'id' => 'testing',
'css' => 'min-width:150px;',
'std' => '2',
'default' => '2',
'type' => 'select',
'options' => array(),
),
After array i did foreach
$args = array('post_type' => 'product', 'posts_per_page' => '-1');
$getproducts = get_posts($args);
foreach ($getproducts as $product) {
$foos['options'][] = array(
$product->ID => $product->get_title,
);
}
I want to list 20 more products in the select box everything manually is hard to me can anyone suggest me to use the foreach inside array?
With PHP functions like array_map() or array_reduce() you can create the new array inside an array. array_map () is useful for creating the values for an array, but you can not manipulate the keys with it. Because of that we can use array_reduce() to simulate the behavior of array_map() and to create the associative array needed for options.
$foos = array(
'name' => 'Testing Selection',
'id' => 'testing',
'css' => 'min-width:150px;',
'std' => '2',
'default' => '2',
'type' => 'select',
'options' => array_reduce( get_posts( 'post_type=product&posts_per_page=-1' ), function( $result, $item ) {
$result[$item->ID] = $item->post_title;
return $result;
})
);
If you don't like the approach, you can create new function that will return the required array for options, and thus improve the readability of code.
I'm not sure exactly what you have in mind, but try this template:
<select>
<?php
$items = array(
'one' => 'Item one',
'two' => 'Item two',
'three' => 'Item three'
);
foreach(array_keys($items) as $item_id) {
echo "<option name=\"$item_id\">$items[$item_id]</option>\n";
}
?>
</select>
If you want to insert data to database , then you should go with foreach key value combination.
Here given the example
<?php
if(isset($_POST['submit'])){
$myArray = array();
$myArray = array(
'name' => $_POST['name'],
'contact' => $_POST['contact'],
'address' => $_POST['address']
);
foreach($myArray as $key=>$value){
echo $value;
}
}
?>
<html>
<head>
</head>
<body>
<form action="#" method="post">
name<input type="text" name="name"/>
contact<input type="text" name="contact"/>
address<input type="text" name="address"/>
<input type="submit" name="submit"/>
</form>
</body>
</html>
I am trying to achieve the output where I have a wrapper div which contains a label and an inner div, and within the inner div I have the form input.
My output should look like this:
<div class="form-group">
<label>Name:</label>
<div class="form-input">
<input type="text" />
</div>
</div>
Here is my current form object in php:
echo $this->Form->input('name', array(
'class' => 'form-input',
'div' => 'form-group',
'label' => array('class' => 'control-label')));
But this adds the class form-input to the actual input itself.
How would I be able to achieve this while still keeping true to the CakePHP way of doing things?
TIA!
Use input options 'before', 'after', 'between' http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options I haven't verified it, but I think it should look something like this:
echo $this->Form->input('name', array(
'between' => '<div class="form-input">',
'after' => '</div>',
'div' => 'form-group',
'label' => array('class' => 'control-label')));
What about this:
echo $this->Form->input('name', array(
'div' => 'form-group',
'before' => '<div class="form-input">',
'after' => '</div>',
'label' => array('class'=>'control-label')
));
I think this works.
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
In one of my forms I have a text input for a posts tags. At the moment, Cake is returning the tag id into this field rather than the name of the tag. I want to change it to show the name of the tag.
The posts controller is getting a result like this:
array(
'Post' => array(
'id' => '7',
'title' => 'Testing again',
'body' => 'wooooooo',
'created' => '2013-01-09 19:20:53',
'slug' => 'testing-again'
),
'Tag' => array(
(int) 0 => array(
'id' => '4',
'name' => 'tag1'
),
(int) 1 => array(
'id' => '3',
'name' => 'tag2'
),
(int) 2 => array(
'id' => '5',
'name' => 'tag3'
)
)
)
and the form is laid out like this:
<?php echo $this->Form->create('Post'); ?>
<?php echo $this->Form->input('Post.title'); ?>
<?php echo $this->Form->input('Post.body'); ?>
<?php echo $this->Form->input('Tag.Tag', array('type' => 'text', 'label' => 'Tags (seperated by space)')); ?>
<?php echo $this->Form->input('Post.slug'); ?>
<?php echo $this->Form->end('Save Changes'); ?>
Is there a way I can tell CakePHP to output the name field of the tags instead of id? Thanks.
OK, so from what I've gathered from our comment discussion, this would be what you're looking for. In your controller, just loop over all the set tags for the post. Assuming your find result is set in the $post variable, you can use the below code and save them all in a "plain" non-recursive array:
$tags = array(); // This will hold all the tags
foreach($post['Tag'] as $tag) {
$tags[] = $tag['name'];
}
// Set the tags as view variable
$this->set(compact('tags'));
Then in your view you can just implode the array with a space and set it as value for your text field:
echo $this->Form->input('Tag.Tag', array('type' => 'text', 'label' => 'Tags (seperated by space)', 'value' => implode(' ', $tags)));
The find example in your OP would then return a value of tag1 tag2 tag3.
Did you set the $displayField in the tags model?
eg.
public $displayField = "name";