I'm using the FormServiceProvider in Silex to generate my forms.
Now I want to put a default text in a generated textarea.
(not a placeholder! && the attribute 'value' I use on my textfields doens't work on a textarea)
I know the text in a textarea is between the tags,
but I don't find an option to fill this tags with my FormServiceProvider.
Is it possible?
Example code:
$form = $app['form.factory']->createNamed('form')
->add('description', 'textarea',
array('attr (??)' => array(' ??? (not placeholder or value!!) ??? ' => 'Default text')),
)
The solution:
array('data' => 'Default text')
Related
I have a FileType field in my form :
$builder->add('letter', FileType::class,[
'label'=>'DEMANDE_STATUS',
'required'=>false,
]);
And I would like to pre fill this 'letter' field when I create the form in the controller.
I've tried this so far, to no avail :
$letter = null;
if (file_exists($path.'/letter.pdf'))
$letter = new File($path.'/letter.pdf');
$demandeForm = $this->createForm('AppBundle\Form\DemandePaiementType', null, ['data'=>[
'letter' => $letter,
]]);
This method usually works when I want to pre-fill a Text field but not in this case sadly.
Any idea on how I could do that?
Don't think you can pre fill a HTML file input field. You can however display some message to the user that they already filled in this field. You can handle this in your twig template.
I'm using FPDF to generate PDF. I use a PDF template created on Adobe Acrobat that include form inputs.
I've been using the following code to generate my pdf :
<?php
***************************
Sample using a PHP array
****************************/
require('fpdm.php');
$fields = array(
'date' => '07/07/2017',
'names' => 'Something',
);
$pdf = new FPDM('Singlepage.pdf');
$pdf->Load($fields, true); // second parameter: false if field values are in ISO-8859-1, true if UTF-8
$pdf->Merge();
$pdf->Output();
?>
It's working perfectly but now i'd like to add multiples lines to my "names" form. Something like 'names' => 'Someone1 \n Someone2But I can't figure out how to do it.
On my pdf template, I added the "multiples lines" to the text input.
Do you have any idea ?
And then i'd like to format this text, so I could add bold, italic etc to the text
The problem are the single quotes, change to double quotes and it will work (if the field is multi-line, of course):
$fields = array(
'date' => '07/07/2017',
'names' => "Something \n more",
);
I found GUMP class for sanitize and validation data input and it works like this :
# Note that filters and validators are separate rule sets and method calls. There is a good reason for this.
require "gump.class.php";
$gump = new GUMP();
$_POST = $gump->sanitize($_POST); // You don't have to sanitize, but it's safest to do so.
$gump->validation_rules(array(
'title' => 'required',
'story' => 'required'
));
$gump->filter_rules(array(
'title' => 'trim|sanitize_string',
'story' => 'trim|sanitize_string',
));
$validated_data = $gump->run($_POST);
if($validated_data === false) {
echo $gump->get_readable_errors(true);
} else {
print_r($validated_data); // validation successful
}
In action this works well and sanitizes all input data. for story field in need to add html tags like <p><img><table> but this class sanitizes all $_POST and removes all html tags.
I can't find out how to add white list(<p><img><table>) for sanitize?! How can I add a white list for html tags?
A quick look at the class shows you can't do that.
Another solution would be using the builtin function strip_tags().
How can I change the attributes of a input form?
I create with this a input (productform.php):
$this->add(array(
'name' => 'categoryId',
'attributes' => array(
'id' => 'categoryId',
'type' => 'hidden',
'value' => '',
),
));
In a previous page I link to the form and set the special value in the url (....com/form/3).
In the indexcontroller.php I get the form with $form = new ProductForm(); and want edit the value and set the special value from the url.
My idea was the $form->setAttribute('categoryId', 'value'); but that not working.
Thanks.
indexcontroller.php
...
$form = new ProductForm();
$form->setHydrator(new CategoryHydrator());
$form->bind(new Product());
$form->setAttribute('categoryId', 'value');
....
productform.php
...
class ProductForm extends Form
{
public function __construct()
{
parent::__construct('productForm');
$this->setAttribute('action', 'newproduct');
$this->setAttribute('method', 'post');
$this->add(array(
........
$form->get('categoryId')->setValue("value");
Update
So if you just want to fill input, you mean placeholder attribute in html. You can use setAttribute method.
$form->get('categoryId')->setAttribute('placeholder', 'text to show');
The form view helper will not allow arbitrary HTML attributes to be set on the form. This is because it would result in invalid HTML.
If you take a look at Zend\Form\Helper\AbstractHelper there are two properties $validGlobalAttributes and $validTagAttributes which define the allowed tags.
In the case of the form view helper (Zend\Form\View\Helper\Form) The 'valid tag attributes' will be the method, action etc
As you require something custom (for JS possibly?); I would change it to a data- attribute.
$form->setAttribute('data-categoryId', 'value');
The data- is a valid HTML5 attribute which is useful for adding 'domain data' to HTML elements and is really the 'correct' way to do what you require.
I want to add a title tag to each checkbox of a Zend_Form_Element_MultiCheckbox, to use jquery tooltip.
I use zend 1.12.
here is the way I make my multicheckbox :
$orderValue = new Zend_Form_Element_MultiCheckbox(Frontend_Model_SiteDetail::CHAMP_ORDER_VALUE);
$orderValue->setLabel($translate->_(Frontend_Model_VueSiteDetail_Parametres::CHAMP_ORDER_VALUE_TITLE))
->setMultiOptions(Frontend_Model_Enum_GroupOrder::getInstance()->getAll(true))
->setDecorators($decorateurs);
my decorator is :
$decorateurs = array('Composite_Table');
I try something like this but it didn't work :
$orderValue->removeDecorator('HtmlTag');
$orderValue->addDecorator('HtmlTag', array(
'attribute' => 'title'
));
I use this method:
$orderValue->setAttrib('title', 'Tooltip');