Zend Form problem:Setting decorators for textarea and textinput length values - zend-decorators

In my Zend form code I have the following
$address = new Zend_Form_Element_Textarea('accounts_address');
$address->setLabel('Address')
->setAttrib('rows','4')
->setAttrib('cols','4')
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$address->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array(array('data'=>'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr'))
));
However in my output I do get the attributes set but the text area seems to remain the same size
Output
<tr><td id="accounts_address-label"><label for="accounts_address" class="optional">Address</label></td>
<td>
<textarea name="accounts_address" id="accounts_address" rows="4" cols="4"></textarea></td></tr>
What am I missing?

Related

Yii2 Multiple input mask on a page with same ID

<?php
echo $form->field($fModel, 'cell_phone')
->widget(\yii\widgets\MaskedInput::className(),['mask' => '(999)999-9999'])
->textInput(['placeholder' => 'Phone'])->label(false);
?>
I have 2 tab with same form with one extra field on second. Issue is that it showing masked input on one tab form and not on second. Anything I am doing wrong. They have same input name/id but FORM ID is different.
You should use another id for second field both for widget and text input. Try this:
<?= $form->field($fModel, 'cell_phone')
->widget(\yii\widgets\MaskedInput::className(), ['options' => ['id' => 'another-id'], 'mask' => '(299)999-9999'])
->textInput(['id' => 'another-id', 'placeholder' => 'Phone'])->label(false);
?>

cakePHP input with characters counting

I have the HTML code like this:
<label>Main Text (<span class="main_text_count">0</span><span>) Characters</span></label>
<textarea name="data[News][sentence]" id="main_text" class="form-control" required rows="8"></textarea>
And I don't know how to create a text area like this, using Form helper of cakePHP.
Simply done using the Form Helper (both for CakePHP 2 and CakePHP 3):-
echo $this->Form->textarea(
'News.sentence',
[
'id' => 'main_text',
'class' => 'form-control',
'rows' => 8,
'label' => 'Main Text (<span class="main_text_count">0</span><span>) Characters</span>'
]
);
In future make sure you've fully read Cake's documentation.

How to display cakephp 2.5.3 model validation message.By default it shows html 5 validation message

How to display the validation message which is defined in CakePHP model.When I submit the form , CakePHP model validation works.But validation message for input field is not displayed. It shows HTML 5 validation Message. I want to display CakePHP model validation message. I am using CakePHP 2.5.3
Following is my code block:
Controller:
$this->User->set($this->request->data);
if ($this->User->validates()) {
if ($this->User->save($this->request->data)) {
$Email->send();
$this->Session->setFlash(__('The user has been created'));
if ($this->Auth->login()) {
$this->Session->setFlash(__('Welcome, ' . $this->Auth->user('username')));
$this->redirect($this->Auth->redirectUrl());
}
} else {
$this->Session->setFlash(__('The user could not be created. Please, try again.'));
}
}
Model:
public $validate = array(
'username' => array(
'nonEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required',
'allowEmpty' => false
),
'unique' => array(
'rule' => array('isUniqueUsername'),
'message' => 'This username is already in use'
),
'alphaNumericDashUnderscore' => array(
'rule' => array('alphaNumericDashUnderscore'),
'message' => 'Username can only be letters, numbers and underscores'
),
)
View:
<div id="divfirstname" class="input text required">
<label for="UserFirstname">First Name <span style="color:#FF0000">*</span></label>
<?php echo $this->Form->text('firstname');?>
</div>
<div id="divlastname" class="input text required">
<label for="UserLastname">Last Name <span style="color:#FF0000">*</span></label>
<?php echo $this->Form->text('lastname');?>
</div>
<div class="input text required">
<label for="UserUsername">Username <span style="color:#FF0000">*</span></label>
<?php echo $this->Form->text('username');?>
</div>
You don't need to wrap the validation-block around your save call.
So this is enough:
if ($this->User->save($this->request->data)) {
$Email->send();
$this->Session->setFlash(__('The user has been created'));
if ($this->Auth->login()) {
$this->Session->setFlash(__('Welcome, ' . $this->Auth->user('username')));
$this->redirect($this->Auth->redirectUrl());
}
} else {
$this->Session->setFlash(__('The user could not be created. Please, try again.'));
}
CakePHP handles automatically the validation when Model->save() is called and displays the error under your input fields. So if $this->save fails because of a validation error, cakephp adds <div class="error-message">Message Text from your model validation array</div> under the right input field.
You are left with 2 options :
Either dont use validation rule in cakePHP which validates on Client side.
On DOM ready , call $("#formId").removeAttr('novalidate'); Like
$(document).ready(function(){
$("#formId").removeAttr('novalidate');
});

zend decorator easy way wrap dt label and dd input inside a div with the id of the input

My goal is to let ZendForm generate my form in this way:
a DIV wrapper that include the normal DD and DT Zend tag... My DIV need to have the ID tag with the id of the element like this:
<dl>
<div id="65-div">
<dt id="65-label"><label for="65" class="required">Nome</label></dt>
<dd id="65-element">
<input type="text" name="65" id="65" value="">
</dd>
</div>
<div id="66-div">
...... ...
</div>
</dl>
I was able to reach my goal doing that:
$Element = $this->createElement('text', $result->request_field__ID);
$Element->clearDecorators()
->addDecorator('ViewHelper')
->addDecorator('Errors')
->addDecorator(array('data'=>'HtmlTag'), array('tag' => 'dd', 'id' => $Element->getId() . '-div', 'class' => 'zendData'))
->addDecorator(array('labelDivOpen' => 'HtmlTag'), array('tag' => 'dt', 'placement' => 'prepend', 'closeOnly' => true))
->addDecorator('Label')
->addDecorator(array('labelDivClose' => 'HtmlTag'), array('tag' => 'dt', 'id' => $Element->getId() . '-label', 'class'=>'zendLabel','placement' => 'prepend', 'openOnly' => true))
->addDecorator(array('row' => 'HtmlTag'), array('tag' => 'div', 'id' => $Element->getId() . '-div'));
Is this the best way?
Why I'm doing that?
Because I generate my form dinamically... And I want to hide some fields and then display it with Jquery if some conditions happend...
In order to hide my field I use:
$Element->getDecorator('row')->setOptions(array('style' => 'display:none;'));
Is this also the best way?
This doesn't look like valid HTML to me. You'd be much better off dropping the divs and using the IDs on the dt and dd that are already there, so just display none those when you want to hide an element. Also, IDs cannot start with a number, so you might want to reverse your naming scheme so that it's element-65 instead.

Zend subform view script render

I would rather not deal with decorators as my form design is not exactly straight forward, but i would like to keep the functionality of validating the forms.
So i have it set up where sub forms are working correctly, but when i try to style it manually in my viewscript i get the name without the parent. I've seen other posts that are similar, but i haven't found a solution.
Example:
This is in my view script
<?php echo $this->form->username->renderViewHelper();?>
I then get
<input type="text" value="" id="username" name="username">
When rendered. It should be
<input type="text" value="" id="form1-username" name="form1[username]">
How do i get that form1 portion?
Thanks!
Edit
Ok, so i found one way.
By using belongTo, it works:
$form1->addElements(array(
new Zend_Form_Element_Text('username', array(
'belongsTo' => 'form1',
'required' => true,
'label' => 'Username:',
'filters' => array('StringTrim', 'StringToLower'),
'validators' => array(
'Alnum',
array('Regex',
false,
array('/^[a-z][a-z0-9]{2,}$/'))
)
))
));
Is there a better way to do this or is this the only way?
Edit2
public function prepareSubForm($spec){
if (is_string($spec)) {
$subForm = $this->{$spec};
} elseif ($spec instanceof Zend_Form_SubForm) {
$subForm = $spec;
} else {
throw new Exception('Invalid argument passed to ' .
__FUNCTION__ . '()');
}
$this->setSubFormDecorators($subForm)
->addSubmitButton($subForm)
->addSubFormActions($subForm);
return $subForm;
}
public function setSubFormDecorators(Zend_Form_SubForm $subForm){
$subForm->setDecorators(array(
'FormElements', \\<--- I tried to change this to PrepareElements before.
array('HtmlTag', array('tag' => 'dl',
'class' => 'zend_form')),
'Form',
));
return $this;
}
I believe you can get your desired output just by using:
<?php echo $this->form->username; ?>
I get the expected output when calling this without renderViewHelper. This is also without any special code for decorators or preparing sub forms. All I had to do was add belongsTo to the form element.
UPDATED:
If you set this to be your default decorator, you can eliminate the dd/dt tags from rendering, instead it will use a div. Then you may be closer to getting the custom output you want. You can change the tag in HtmlTag from div to whatever tag you would like to wrap your elements in. This is what I use mostly:
array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'div', 'class' => 'form-div')),
array('Label', array('class' => 'form-label', 'requiredSuffix' => '*'))
);
This is the default for Zend Framework:
array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'dd', 'id' => array('callback' => $getId)))
array('Label', array('tag' => 'dt'))
);
Note that file, and submit/button elements use different decorators.
Also see this answer