How to add some text in textarea in Zend_Form? - forms

How put information in the textarea tag in Zend_Form? For example:
<textarea name="descNews" id="descNews" rows="5" class="txt_meta" cols="80">This is text!</textarea>
Zend form:
$description = new Zend_Form_Element_Textarea('descNews');
$description->addFilter('StripTags')
->setAttribs(array(
'rows' => '5',
'class' => 'txt_meta'
));

you can use setValue(mixed $value)
$description = new Zend_Form_Element_Textarea('descNews');
$description->addFilter('StripTags')
->setAttribs(array(
'rows' => '5',
'class' => 'txt_meta'
));
$description->setValue('set your default vallue');
Or you can see on zend doc
hope it will work for you.

Related

How can i create a new zend Form attribute

How can i add a new personalized attribute with zend From, exemple <input type='text' **ref='name**' name='id_commande' id='commande'/>
Use setAttrib on element for single, or setAttribs for multiple attributes you want to add
This should make a job:
$id_commande = new Zend_Form_Element_Text('id_commande');
$id_commande->setAttribs([
'ref' => 'name',
'class' => 'your_class',
'style' => 'width: 150px'
]);

Disable autocomplete in CakePHP form input box

I am creating form inputs with the CakePHP Form helper and some inputs (Most of the time 'username' and 'password') are being autocompleted on create actions, login actions, etc.. this is annoying. I am guessing those are just more common so the browser is using its cookies to try to complete the inputs.
Anyways.. how do I disable this?
In my view:
...
echo $this->Form->input('username', array(
'label' => 'Please enter your username',
'class' => 'pure-u-1-2'
));
echo $this->Form->input('password', array(
'label' => 'Please enter your password',
'class' => 'pure-u-1-2'
));
...
What am I missing?
You can specify attributes to be sent to the form helper. Specify the attribute 'autocomplete' and set its value to 'off'.
...
echo $this->Form->input('username', array(
'label' => 'Please enter your username',
'class' => 'pure-u-1-2',
'autocomplete' => 'off'
));
echo $this->Form->input('password', array(
'label' => 'Please enter your password',
'class' => 'pure-u-1-2',
'autocomplete' => 'off'
));
...
Which results in something like this for your HTML:
<input name="data[Model][username]" autocomplete="off" class="pure-u-1-2" id="ModelUsername" type="text">
You may also do this on the whole form instead of just each input. Just specify the same attribute and value in the form create like so:
...
echo $this->Form->create('Model', array(
'class' => 'class',
'autocomplete' => 'off'
));
This will give you something like this in your HTML:
<form action=".../Model/Action" class="class" autocomplete="off" id="ModelActionForm" method="post" accept-charset="utf-8">
NOTE Several browsers will now ignore autocomplete="off" or autocomplete="false". The workaround is to place a hidden text and password field before all other inputs on your form. The browsers will fill those instead of the ones you want to leave alone.
The best solution is to use autocomplete = new-password
It works great in Chrome and Firefox
Like this:
$this->Form->input('password', array('type' => 'password', 'autocomplete' => 'new-password'));

how to add form id in cakephp when creating a form

i am new in cakephp so i dont know how can i do this ..
i want to add custom form id in my form but it is not adding the id ..it is using the default one adding the 'UserIndexForm' id..
how can i add this id
i want to do like this
<form method="post" action="#" id="form-login">
here cakephp code
<?php
echo $this->Form->create('User', array(
'inputDefaults' => array(
'label' => false,
'div' => false,
'id' =>'form-login'//not working
)
));
?>
please help me if anyone know this
thankyou in advance
The inputDefaults option is only changing the input fields so you need to set the id on the root level of the array:
<?php
echo $this->Form->create('User', array(
'id' => 'form-login',
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
?>

CodeIgniter: Controller structure for forms with many inputs

I'm developing a site using CodeIgniter and am trying to adhere to the "Fat Model / Skinny Controller" paradigm, but am running into some problems when it comes to pages containing forms with a number of inputs. The code below is what I'm using to define the inputs for the address fields
Part of my Controller (where I'm defining the form inputs and their attributes):
$this->data['address1'] = array(
'name' => 'address1',
'id' => 'address1',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '10',
'value' => $this->form_validation->set_value('address1'),
'placeholder' => 'Street Address'
);
$this->data['address2'] = array(
'name' => 'address2',
'id' => 'address2',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '11',
'value' => $this->form_validation->set_value('address2'),
'placeholder' => 'Address Line 2',
);
$this->data['city'] = array(
'name' => 'city',
'id' => 'city',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '12',
'value' => $this->form_validation->set_value('city'),
'placeholder' => 'City'
);
$this->data['state'] = array(
'name' => 'state',
'id' => 'state',
'class' => 'field addr',
'tabindex' => '13',
'value' => $this->form_validation->set_value('state'),
'label' => array('class' => 'desc')
);
$this->data['zip'] = array(
'name' => 'zip',
'id' => 'zip',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '14',
'maxlength' => '20',
'value' => $this->form_validation->set_value('zip'),
'placeholder' => 'Zip / Postal Code'
);
$this->data['country'] = array(
'name' => 'country',
'id' => 'country',
'class' => 'field addr',
'tabindex' => '15',
'value' => $this->form_validation->set_value('country')
);
Part of my View (minus all the HTML to position the form inputs):
<?php
echo form_open("address/add");
echo form_input($address1);
echo form_input($address2);
echo form_input($city);
$options = array();
$options[''] = 'State / Province / Region';
foreach($province_options AS $prov)
{
$options[$prov->id] = $prov->province;
}
echo form_dropdown('state',$options,'',$state);
echo form_input($zip);
$options = array();
$options[''] = 'Country';
foreach($country_options AS $cnt)
{
$options[$cnt->id] = $cnt->country;
}
echo form_dropdown('country',$options,'',$country);
echo form_submit('submit', 'Submit & Continue');
echo form_close();
?>
I feel like my Controller is overly verbose, but I can't think of what the alternative would be for how to organize the information necessary to represent my form if I'm planning on using the Form Helper to generate the form inputs in my view. Is this the right way to be doing things, or is there a better approach?
Just because Codeigniter provides all of these helpers does not mean you have to use them!
All you need is form_open() because this adds the CRSF token (if used).
Raw HTML is much cleaner and I suspect much faster than waiting for PHP to render markup.
Edit: I would like to add, the reason its cleaner is because you have control over the output, where as CI might adere to certain specifications.
I don't see a problem in your question.
This is just silly
$options = array();
$options[''] = 'State / Province / Region';
There is a little bit of logic that can be moved to the controller or even model layer:
$options = array();
$options[''] = 'Country';
foreach($country_options AS $cnt)
{
$options[$cnt->id] = $cnt->country;
}
echo form_dropdown('country',$options,'',$country);
Could probably look like:
echo form_dropdown('country', $countries, '', $country);
...if you move the options to the controller or view.
This is a problem I run into all the time trying to keep things DRY as possible, where to define the form data? I think sometimes we forget the power of the "V" in "MVC". You could define all the view logic in the view instead.
Things like id, tabindex and placeholder are only necessary and useful in the view. Things like form validation rules and data checking/prepping belong in the Controller/Model layer.
The form helper functions are useful, but sometimes raw HTML is better. For example:
// Controller
$this->data['address1'] = array(
'name' => 'address1',
'id' => 'address1',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '10',
'value' => $this->form_validation->set_value('address1'),
'placeholder' => 'Street Address'
);
// View
echo form_input($address1);
Or simply:
<input name="address1" id="address1" tabindex="10" type="text" placeholder="Street Address" value="<?php echo set_value('address1'); ?>" class="field text addr">
I wrote a bunch of applications last year where I defined all this stuff in the Model, and now I'm regretting it as I've been going back to do maintenance on them and all the view logic is obscured away in the Model or Controller. Editing a Controller or Model to change a class attribute is just silly.

Zend Form decorators trouble

How do I achieve the following with form decorators for form elements:
<dt>
<ul>
<li>The errors</li>
<li>The errors</li>
</ul>
<label>The label</label>
</dt>
<dd>
<input type="text" value="The input field">
</dd>
In other words, in stead of Errors appended after the input field, I want them prepended before the Label. I do however want to keep the <dt> and <dd> tags as illustrated above.
Alright, I found out how to do it. Gradually the decorators are starting to make sense to me:
$decorators = array(
'Label',
array( 'Errors', array( 'placement' => 'prepend' ) ),
array( array( 'dt' => 'HtmlTag' ), array( 'tag' => 'dt' ) ),
array( array( 'ddOpen' => 'HtmlTag' ), array( 'tag' => 'dd', 'openOnly' => true, 'placement' => 'append' ) ),
array( 'ViewHelper' ),
array( array( 'ddClose' => 'HtmlTag' ), array( 'tag' => 'dd', 'closeOnly' => true, 'placement' => 'append' ) )
);
What this does is the following:
First render the Label
Then prepend (default = append) the Errors
Wrap (default) all previous content in a HtmlTag (dt)
Next, append (default = wrap) a opening HtmlTag (dd)
Then append (default) the ViewHelper
Next, append (default = wrap) a closing HtmlTag (dd)
Then set the decorators:
// be sure to only set them, after you have added the relevant elements to the form
$this->setElementDecorators( $decorators );
PS:
Be aware though that my particular example produces invaliid html. ;-) I only found out later that <ul> elements are not allowed in <dt> elements with DOCTYPE HTML 4.01 strict
In your form class, try this:
$this->setElementDecorators(array(
'Errors',
'ViewHelper',
'Label',
));