I have the code below to display a calendar input in Yii form.
<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name' => 'publish_date',
'attribute' => 'publish_date',
'model'=>$model,
'options'=> array(
'dateFormat' =>'yy-mm-dd',
'defaultDate' => '2014-3-4',
'altFormat' =>'yy-mm-dd',
'changeMonth' => true,
'changeYear' => true,
'appendText' => 'yyyy-mm-dd',
),
));
?>
The default value work on the calendar but I want to show it by default in the calendar input too when the form is rendering.
How can I do it?
You can use Html value attribute for this
<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name' => 'publish_date',
'attribute' => 'publish_date',
'model'=>$model,
'options'=> array(
'dateFormat' =>'yy-mm-dd',
'defaultDate' => '2014-3-4',
'altFormat' =>'yy-mm-dd',
'changeMonth' => true,
'changeYear' => true,
'appendText' => 'yyyy-mm-dd',
),
'htmlOptions'=>array('value'=>'2013-4-4')
));
Manquer's solution didn't work (in version 1.1.17), CJuiDatePicker extends from CJuiInputWidget and it has a value property which is used by CHtml::inputField() when rendering the element, overwriting the value field of htmlOptions property.
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name' => 'publish_date',
'attribute' => 'publish_date',
'model'=>$model,
'options'=> array(
'dateFormat' =>'yy-mm-dd',
'defaultDate' => '2014-3-4',
'altFormat' =>'yy-mm-dd',
'changeMonth' => true,
'changeYear' => true,
'appendText' => 'yyyy-mm-dd',
),
'value' => '2013-3-4',
));
Related
I need to display a default text for the dropdown but I dont want it to be selectable when user clicks on the dropdown:
$this->add(array(
'name' => 'quicksearch',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'empty_option' => 'Quick Search',
'value_options' => array(
'1' => 'Online Now',
'2' => 'Most Popular'
),
),
'attributes' => array(
'id' => 'quicksearch',
'class' => 'quicksearch selectyze1 comonserchclas',
),
));
In the above I need to display Quick Search as default but not selectable. Is there any option to not display the Quick Search text when user clicks on dropdown ?
Try creating the options array as follows:
$options = array(
array('value' => '0', 'label' => 'Quick Search', 'disabled' => 'disabled'),
array('value' => '1', 'label' => 'Online Now'),
array('value' => '2', 'label' => 'Most Popular')
);
My code for my form ( i am using Silex):
$test = array(
'Swedish Cars' => array(
'volvo' => 'Volvo',
'saab' => 'Saab',
),
'German Cars' => array(
'mercedes' => 'Mercedes',
'audi' => 'Audi'
)
);
$form = $app['form.factory']->createBuilder('form')
->add('title','text',array(
'attr' => array(
'placeholder' => 'Title of your Album'
)))
->add('description','textarea',array(
'attr' => array(
'placeholder' => 'Describe your Album'
)))
->add('groups', 'choice', array(
'choices' => $test,
'multiple' => true,
'attr' => array(
'data-placeholder' => 'Add your Groups ...'
),
))
The choices are defined as an multi-array, so I get <option> with <optgropup>. How can I enable in SF2, that some options are selected?
Use the data option:
->add('groups', 'choice', array(
'choices' => $test,
'multiple' => true,
'attr' => array(
'data-placeholder' => 'Add your Groups ...',
'data' => $selected_value
),
where $selected_value can be a single value like 'value_1' or an simple array with multiple values array('value_1', 'value_2') to select.
In \Zend\Validator\DateStep, I want to override the error message shown below:
protected $messageTemplates = array(
self::NOT_STEP => "The input is not a valid step"
);
I have an input filter connected to a form containing an element of the type 'Zend\Form\Element\Date' which automatically calls the DateStep validator.
Here is the relevant part of my form:
$this->add(array(
'type' => 'Zend\Form\Element\Date',
'name' => 'appointment-date',
'options' => array(
'label' => 'Appointment Date',
'format' => 'Y-m-d'
),
'attributes' => array(
'min' => date('Y-m-d'), // today's date
'max' => '2020-01-01',
'step' => '2', // days; default step interval is 1 day
)
));
Here is my input filter:
$inputFilter->add($factory->createInput(array(
'name' => 'appointment-date',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'DateStep',
'options' => array(
//'step' => new DateInterval("P2D"),
//'baseValue' => new DateTime(),
'messages' => array(
\Zend\Validator\DateStep::NOT_STEP => 'Must be a day in the future',
),
),
),
),
)));
The inputFilter seems to be ignored. I tried setting up the step and baseValue in the inputFilter, but that seems to not work at all. Working app can be found here: https://github.com/bickerstoff/zf2_datetest if more details are needed.
Looks like you're trying to filter a "Date" input with a "DateStep" validator.
$this->add(array(
'type' => 'Zend\Form\Element\Date',
'name' => 'appointment-date',
'options' => array(
'label' => 'Appointment Date',
'format' => 'Y-m-d'
),
'attributes' => array(
'min' => date('Y-m-d'), // today's date
'max' => '2020-01-01',
'step' => '2', // days; default step interval is 1 day
)
));
This may cause your problem.
In a Zend Framework 2 application, I have the following form in my controller action method:
public function testAction()
{
$form = new \Zend\Form\Form('test');
$date = new \Zend\Form\Element\DateSelect('date');
$date->setOptions(array(
'label' => 'Date',
'min_year' => date('Y') - 10,
'max_year' => date('Y') + 10,
)
);
$date->getDayElement()->setEmptyOption('day');
$date->getMonthElement()->setEmptyOption('month');
$date->getYearElement()->setEmptyOption('year');
$form->add($date);
$form->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'submit',
'id' => 'submitbutton',
),
));
if ($this->request->isPost()) {
$data = $this->request->getPost();
$form->setData($data);
if ($form->isValid()) {
$data = $form->getData();
// more code
}
}
return array('form' => $form);
}
Now if I submit the form I get the validation message:
'The input does not appear to be a valid date'
That is correct but I would only want to know if the field is required. If I look in the source of the DateSelect element, I see a getInputSpecification() method that sets required to false by default and there also is a getValidator() method requiring a format with which the empty date does not comply.
How can I bypass validation if the input is not required (obviously, in my real form I have more elements)?
I usually use the factory pattern and you can set whether the field is required ie
$inputFilter->add($factory->createInput(array(
'name' => 'availableDate',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'max' => 20,
),
),
),
)));
refer the field required - you can set this to false
Currently, I have this select code in ZF2. Works great, have no issues, BUT! I want to put an id or class attribute into one of the options. Is that possible currently in ZF2?
$this->add(
array(
'name' => 'number_of_rooms',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Number of Rooms',
'options' => array(
array(
'value' => '',
'label' => '- number of rooms -',
'disabled' => true,
),
array(
'value' => 'one bedrooms,
'label' => '1 Bedrooms',
),
array(
'value' => 'two bedrooms',
'label' => '2 Bedrooms',
),
array(
'value' => 'three bedrooms',
'label' => '3 Bedrooms',
),
),
),
'attributes' => array(
'id' => 'number_of_rooms',
),
)
);
You would need to do two things. First would be to write your own SelectElement extending Zend\Form\Element\Select and then you'd need to also write your own ViewHelper rendering the Select, easiest extending Zend\Form\View\Helper\FormSelect
With the inbuilt Select the only valid options are:
//FormSelect.php #43-48
protected $validOptionAttributes = array(
'disabled' => true,
'selected' => true,
'label' => true,
'value' => true,
);
You can check this from the classes linked above.