YII form input with html5 required="required" - php

I have one application in Yii and its creating one field for email input,
the code in view is
<?php echo $form->textField($model, 'email', $model->getHtmlOptions('email')); ?>
I want to add required="required" in the same field.
Even I don't know from where this $model->getHtmlOptions('email')) is coming from.
I don't know how to do it. I've searched all day in google but could not find a solution.

You might be use some third part extension or behavior with getHtmlOptions() method. Concerning $form->textField signature third parameter should be an array. So to keep getHtmlOptions working and add required attribute we need to merge two arrays:
<?php echo $form->textField(
$model,
'email',
CMap::mergeArray(
$model->getHtmlOptions('email'),
array('required' => true)
);
?>
Hope it will work.

Related

SilverStripe CreditCardField bad html and submission error

I have a form which works fine without it but when I add
CreditCardField::create('CreditCard','Credit Card')->setAttribute('placeholder', '0000')
it displays the credit card field with the same name, id and other attributes. When I go to submit it of course the validation throws an error as it's expecting an array but only gets a string of the last 4 digits.
Am I creating this field correctly? I'm assuming that once I fix the display issues it will fix the validation issues.
I'm not hugely familiar with this field type, but you can see from it's template that it doesn't expect to be able to set a placeholder:
<span id="{$Name}_Holder" class="creditCardField">
<input $AttributesHTML('id', 'name', 'value', 'tabindex') name="{$Name}[0]" value="{$ValueOne}" $TabIndexHTML(0)/>
-
<input $AttributesHTML('id', 'name', 'value', 'tabindex') name="{$Name}[1]" value="{$ValueTwo}" $TabIndexHTML(1)/>
-
<input $AttributesHTML('id', 'name', 'value', 'tabindex') name="{$Name}[2]" value="{$ValueThree}" $TabIndexHTML(2)/>
-
<input $AttributesHTML('id', 'name', 'value', 'tabindex') name="{$Name}[3]" value="{$ValueFour}" $TabIndexHTML(3)/>
</span>
The attributes you can set are ID, name, value and tabindex. Placeholder may be omitted deliberately for some sort of security reason, or it may just be a missing feature.
Your best bet might be to use some javascript instead:
$('#MyCreditCard_Holder').find('input').attr('placeholder', '0000');
So there is a bug in the template that I've logged a ticket for. The work around for it at the moment is you have to have your own CreditCardForm.ss template that uses $getAttributesHTML rather than $AttributesHTML

Yii2, Form and field names

Simple question but no solution yet. As we know
<?php $form = ActiveForm::begin(['method'=>'get']); ?>
<?= $form->field($formFilter, 'keyword')
->textInput(['placeholder' => \Yii::t('', 'keyword')]); ?>
...
will create simple form and input fields. Of course we will load $_POST data in action like
if ($this->isPost() && $formFilter->load($this->post())) {
if ($formFilter->validate()) {
...
If we will look in $_POST we will see something like FormFilter[keyword] as name of field. So question is, how can I change it? I need (i think) somehow change in in form\model not in view, because we need proper loading in action.
Where it will be used? Any GET form will show ugly url with class names, for example using simple action and models we will get FormFilter[keyword] but I want change it to keyword, so url will be more understandable than 'long field names'.
Anyone know how to deal with this?
Sorry, later I found solution, I think it will help not just me...
Simple one is to redefine formName() method in our form/model. Using formName() we can even change it what ever we need or disable at all if will set such one
public function formName()
{
return '';
}
So, if forName() returns empty string we will get url :
http://site/items?keyword=&locationID=&employmentType=&educationLevel=&salaryMin=
Default one will be:
http://site/items?FormVacanciesFilter[keyword]=&FormVacanciesFilter[locationID]=6&FormVacanciesFilter[employmentType]=&FormVacanciesFilter[educationLevel]=&FormVacanciesFilter[salaryMin]=
You can change it per field in the view, e.g. I have a form based on yii\base\DynamicModel where I need to control the field names, and, for example:
echo $form->field($model, 'test')->hiddenInput(['name' => 'test'])->label(false);
will output:
<div class="form-group field-dynamicmodel-test">
<input type="hidden" id="dynamicmodel-test" class="form-control" name="test" value="{value of $model->test}">
<p class="help-block help-block-error"></p>
</div>

Cakephp hidden input field

So i have this field i want to keep hidden in my form.
For this purpose i have tried the following:
<?php echo $this->Form->input('group_id', array('hiddenField' => true, 'value'=> 2)); ?>
I also tried:
<?php echo $this->Form->input('group_id', array('options' => array('hiddenField'=> 'true'), 'value'=>2 )); ?>
How ever i still see the input field..
What am i doing wrong?
You misread the documentation, I assume.
hiddenField is to enable/disable specific hidden fields for specific form fields.
You are either looking for
$this->Form->hidden('group_id')
or
$this->Form->input('group_id', ['type' => 'hidden']);
I usually only use the latter.
See http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
BUT - that said - you shouldnt actually use either one of those. And omit any fields that serve no real purpose for the view and its form.
Instead you should inject those fields into the data array prior to saving.
See http://www.dereuromark.de/2010/06/23/working-with-forms/
If you are looking to add a hidden field that uses a related second data array that will not be passed via post or put by default, you can use this to pass it:
echo $this->Form->hidden('Group.name');
This is useful for echoing out edit page titles when the post or put encounters an error. A dynamic title can lose Group.name data array when your form is set up such as this:
<h1>Edit Group - <?php echo h($this->request->data['Group']['name']); ?></h1>
For data that is to be saved to db however, follow Mark's suggestion above.
Try following code in cakephp 3 to set hidden field
<?php
echo $this->Form->hidden('name');
?>

How to validate fields appended with id on cakephp

I have my form fields where I am appending an id within a for loop.
<?php echo $this->Form->input('Shipment.current_city'.$sh, array('label' => 'City')); ?>
I would like to know how to validate such dynamic fields. Currently Cake is not recognizing my validation rules from my model due to the appended id.
Thanks.
You can find the solution of your issue here.
http://php-dev-zone.blogspot.com/2013/08/dynamic-fields-validation-in-cakephp.html

Render Zend\Form\Element\Button with content and without label

It seems like it's impossible to render a button element automatically using the FormCollection view helper without label.
Whenever possible, I render forms this way:
<?php echo $this->form()->openTag($this->form); ?>
<?php echo $this->formCollection($this->form); ?>
<?php echo $this->form()->closeTag($this->form); ?>
However, the FormButton view helper, that is invoked by FormCollection, awaits the button's content as second param to the render() method -- or a label option. If I set the label, the button renders correctly, but, you guess it, with label.
I tried to figure out a workaround for this by browsing through the code, but I can't see one.
Note: FormButton renders <button>...</button> elements. I could also live with a <input type="button" /> element.
Am I missing something here? Thanks in advance!
When I was struggling with this problem, I've got the solution accidentally! It is so easy (or hackish?), that I wouldn't have thought to work.
Add this to your fieldset:
$this->add(array(
'name' => 'delete',
'attributes' => array('type'=>'button', 'value' => 'Delete', 'onclick'=>'delete()'),
));
That's all. It is working for me very well.
Write your own formCollection ViewHelper that uses your very own formButton ViewHelper.

Categories