$policyYear =
Array
(
[1] => 1 Year
[2] => 2 Year
[3] => 3 Year
)
<div>
<?php
$options = $policyYear;
$attributes = array('legend' => false);
echo $this->Form->radio('policy_year', $options, $attributes);
?>
</div>
This is my code which is giving three radio button having values '1 Year', '2 Year' , '3 Year'. But I want to set selected initially for value '1 Year'. How can I do that ?. I am new in cakephp. I know it in core php. Please help.
Update your $attributes and add value
$attributes = array('legend' => false,'value' => $foo);
Related
I'm using sfWidgetFormI18nDateTime in a form, and I have to add more years to it (back in time). For example 2007, 2006, 2005 etc.
sfWidgetFormI18nDate widget does support the "years" attribute, but DateTime doesn't.
Here's my code:
$this->widgetSchema['created_at'] = new sfWidgetFormI18nDateTime(array('culture' => 'hu'));
$this->validatorSchema['created_at'] = new sfValidatorDateTime(array('required' => false));
I need something like this:
$this->widgetSchema['created_at'] = new sfWidgetFormI18nDateTime(array('culture' => 'hu', 'years' => metWidgetTools::getYears()));
$this->validatorSchema['created_at'] = new sfValidatorDateTime(array('required' => false, 'years' => metWidgetTools::getYears()));
Is there a way that I can go around this?
Your widget schema should looks something like this
$years = range(2000, date('Y')); // from to 2000 to current year
$this->widgetSchema['created_at'] = new sfWidgetFormI18nDateTime(array(
'culture' => 'hu',
'date' => array(
'years' => array_combine($years, $years)
)
));
I'm trying to let users post from the front end into a nested ACF repeater field.
I've got the sub fields in the first repeater posting fine. But, I can't quite figure out how to get the nested repeater working properly.
Here's the code I have so far:
$event_field_key = 'field_535e6b9ffe3da';
$events[] = array(
'start-date' => $startDate,
'end-date' => $endDate,
'notes' => $_POST['p'.$p.'-notes'],
'start-end-times' => array(
'start-time' => '09:00', // would be dynamic
'end-time' => '17:00' // would be dynamic
)
);
update_field($event_field_key, $events, $post_id);
I'm not sure if I can just nest another array in there, or if I need to do something else.
[UPDATE]
I've just done this and it does input into the first row:
$event_field_key = 'field_535e6b9ffe3da';
$events[] = array(
'start-date' => $startDate,
'end-date' => $endDate,
'notes' => $_POST['p'.$p.'-notes'],
'start-end-times' => array(
'start-time' => '9:00',
'end-time' => ' 17:00'
)
);
update_field($event_field_key, $events, $post_id);
However, this code puts row 1 values both as 9 and row 2 values as 1.
So it looks like:
Row 1: start time: 9, end time: 9
Row 2: start time: 1, end time: 1
I can't seem to find any documentation on this, but it looks like it's possible, just a case of figuring out the syntax.
The fix was an array of arrays:
$event_field_key = 'field_535e6b9ffe3da';
$events[] = array(
'start-date' => $startDate,
'end-date' => $endDate,
'notes' => $_POST['p'.$p.'-notes'],
'start-end-times' => array(
array(
'start-time' => '09:00',
'end-time' => '17:00'
),
array(
'start-time' => '10:00',
'end-time' => '16:00'
)
)
);
update_field($event_field_key, $events, $post_id);
I am trying to create a drop down menu (option) and to fill this drop down i have sent an array list to the view:
$country = $this->country_list;
$this->set(compact('country'));
Now my question is does cake have a buildin method for me to set an input field using ($this->Form->input()) with the data of the array list?
take this example
$sizes = array(
's' => 'Small',
'm' => 'Medium',
'l' => 'Large'
);
echo $this->Form->input('size', array('options' => $sizes, 'default' => 'm'));
In the controller, set the value
$this->set('countries', $this->Country->find('list', array('fields' => 'Country.name')));
To show the dropdown box in the view
$this->Form->input('country_id');
My data comes from a database, and has been grouped by day_id, so the array is sorted by day_id.
Code + sample output provided, kind of hard to put in words.
I got my inspiration so far from this accepted answer, but I need a slight adjustment.
$array = array(
0 => array(
'id' => '5',
'day_id' => '1',
'Foo' => array(
'id' => '28',
'name' => 'My day_id is 1'
)
),
1 => array(
'id' => '6',
'day_id' => '1',
'Foo' => array(
'id' => '29',
'name' => 'My day_id is 1'
)
),
2 => array(
'id' => '7',
'day_id' => '2',
'Foo' => array(
'id' => '30',
'name' => 'My day_id is 2'
)
),
3 => array(
'id' => '8',
'day_id' => '3',
'Foo' => array(
'id' => '31',
'name' => 'My day_is 3'
)
)
);
And my code to output it:
$day = 0;
foreach($array as $a) {
// $a['day_id'] = 1, condition is true. output header once.
if($day != $a['day_id']) {
echo '<h3>The Day is ' . $a['day_id'] . '</h3>' . "\n";
}
// output the elements Foo key content.
echo $a['Foo']['name'] . "\n";
// store the current value to compare on the next iteration
// $a['day_id'] will eventually be 2, causing the condition to fire again.
$day = $a['day_id'];
}
This outputs:
<h3>The Day is 1</h3>
My day_id is 1
My day_id is 1
<h3>The Day is 2</h3>
My day_id is 2
<h3>The Day is 3</h3>
My day_is 3
I now want to wrap each "Day" in a DIV, but I cannot figure out the logic. If I replace the <h3> with an open div, I do not know where to put the closing </div>. My attempts thus far have just led to each day having an open div, but not a closed one so they are nested, rather than separate.
I can't put the </div><div class="day-X"> as there will be an extra </div> at the start which would break the layout.
Desired output:
<div class="day-1">
<h3>The Day is 1</h3>
My day_id is 1
My day_id is 1
</div>
<div class="day-2">
<h3>The Day is 2</h3>
My day_id is 2
</div>
<div class="day-3">
<h3>The Day is 3</h3>
My day_is 3
</div>
Hope this makes sense - thanks
You will need to loop twice, one for the days and one for the items that has this day. Without using any other SQL queries you will need to store the days in an array first. then you could loop the days and for each day you will echo the <div> and the day header, at the end you will echo the </div>.
inside the foreach you can loop again using the same array to check each item if it's has the same day or not. if yes then echo the name of the item, otherwise don't do anything.
I have tested this code, you can check it and let me know if you are having any problems with it.
foreach($array as $day){
$days[] = $day['day_id']; // store all the days in an array
}
// now days[] has [1,1,2,3]
$days = array_unique($days); // get unique days only no duplicated
//now days[] has [1,2,3]
foreach($days as $d){ // for each day
echo '<div class = "day-'.$d.'">';
echo '<h3>The Day is ' . $d . '</h3>' . "\n";
foreach($array as $a) { // check all other items if the item has the same day then echo
if($a['day_id'] == $d){
echo $a['Foo']['name'] . "\n";
}
}
echo '</div>';
}
Output:
<div class = "day-1">
<h3>The Day is 1</h3>
My day_id is 1
My day_id is 1
</div>
<div class = "day-2">
<h3>The Day is 2</h3>
My day_id is 2
</div>
<div class = "day-3">
<h3>The Day is 3</h3>
My day_is 3
</div>
I have a set of selects for a date of birth:
<?php echo $this->Form->input('Profile.dob', array('label' => 'Date of Birth'
, 'dateFormat' => 'DMY'
, 'minYear' => date('Y') - 100
, 'maxYear' => date('Y') - 13)); ?>
and want to set the defaults to be the words "DAY MONTH YEAR" in the selects.
I have managed to do this with the gender with:
<?php echo $this->Form->input('Profile.gender', array('label' => 'Gender', 'type' => 'select',
'options' => array('Male'=>'Male','Female'=>'Female'),'empty'=>'Select Sex')); ?>
but I don't see how to do this with the automagic date input...
Can anyone help? Thanks
Simply add:
'selected'=>date('Y-m-d')
to your array of options.
That example will show the current date. If you need to have a static date, replace it as required. eg:
'selected'=>'2011-12-10'
Obviously for the date and time, use:
'selected'=>date('Y-m-d H:i:s')
or
'selected'=>'2011-12-10 11:13:45'
This way works:
<?php
echo $this->Form->input(
'Profile.dob',
array(
'label' => 'Date of Birth',
'dateFormat' => 'DMY',
'minYear' => date('Y') - 100,
'maxYear' => date('Y') - 13,
'empty' => array(
'day' => 'DAY',
'month' => 'MONTH',
'year' => 'YEAR'
)
)
);
?>
If you don't mind 2 more lines, you can try doing this?
<?php
echo $this->Form->year('Profile.dob', date('Y') - 100, date('Y') - 13, array('empty' => "YEAR"));
echo $this->Form->month('Profile.dob', array('empty' => "MONTH"));
echo $this->Form->day('Profile.dob', array('empty' => 'DAY'));
?>
I implemented it like this in cakephp 2.0 above
echo $this->Form->dateTime('dob', 'DMY','', array(
'value'=>'1987-02-12',
'empty'=>false,
'label'=>'Date Of Birth',
'minYear'=>date('Y')-60,
'maxYear'=>date('Y')-15)
);
'value' attribute has been added after 2.0 api of cakephp and 'selected' is remove.
Cakephp manual says :The $selected parameter was removed from several methods in FormHelper. All methods now support a $attributes['value'] key now which should be used in place of $selected. This change simplifies the FormHelper methods, reducing the number of arguments, and reduces the duplication that $selected created. The effected methods are:
FormHelper::select()
FormHelper::dateTime()
FormHelper::year()
FormHelper::month()
FormHelper::day()
FormHelper::hour()
FormHelper::minute()
FormHelper::meridian()
Also, are you really sure about what you really mean?
You seem to be confusing default value and empty value. If you set default value as DAY MONTH YEAR using 'selected', your code won't work because DAY MONTH YEAR isn't a valid date.
Using
'empty' => array(
'day' => 'DAY',
'month' =>'MONTH',
'year' =>'YEAR'
);
looks like what you're looking for, inviting the user to enter a date.
Cakephp set selected to the empty value if the corresponding date request data is null, therefore simply set it to null prior to echoing the date input fields:
$this->request->data['Profile']['dob'] = null;
echo $this->Form->month('Profile.dob', array('empty' => "MONTH"));
// do the same for year and day
the empty value (in your case "DATE", "MONTH", "YEAR" fields) will be pre-selected in the input form
This is what currently works for me in cakephp 2.5:
echo $this->Form->input('fecha_pos_fijacion', array(
'label' => 'Fecha de fijaciĆ³n',
'dateFormat' => 'DMY',
'minYear' => date('Y'),
'maxYear' => date('Y')+5,
'orderYear' => 'asc',
'selected' => date('Y-m-1')
)
);
This is a bit more elaborated, it'd give you a default value set to the 1st day of current month. Possible values for year are between current and 5 years ahead, displayed in ascending order.
Even more complete:
echo $this->Form->input('fecha_transporte', array(
'label' => '',
'dateFormat' => 'DMY',
'minYear' => date('Y'),
'maxYear' => date('Y')+5,
'orderYear' => 'asc',
'selected' => date('Y-m-1', strtotime("+30 days"))
)
);
Here default is 1st day of next month