Symfony 3 pre-fill FileType field - php

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.

Related

How to automatically fill other form fields after one field is filled?

I'm creating a form in moodle using moodleform class. I have many fields in the form. What I'm wanting to do is when the user fills in the first field, I want to get the data that he/she entered, search the relevant DB table for a field that matches that input, then populate the other fields for that record.
Please note that the user hasn't pressed any submit button yet. I've been trying to find a function that gets the entered data but all efforts have been in vain. What I found was a get_data() method but I don't even know how to use that correctly. I've been reading the moodle docs but nothing is helping. I'm not a beginner to coding but neither am I an expert.
Here's a code snippet:
class requestcourse_form extends moodleform
{
function definition()
{
global $CFG, $currentsess, $DB, $USER, $currentrecord;
$mform =& $this->_form; // Don't forget the underscore!
// Form header
$mform->addElement('header', 'mainheader','<span style="font-size:22px">'.get_string('courserequestform','block_usp_mcrs'). '</span>');
// Course Code field.
$coursecodearray = array();
$coursecodearray[0] = get_string('choosecoursecode', 'block_usp_mcrs');
$allcoursecodes = $DB->get_records_select('block_usp_mcrs_courses', 'id > 0', array(), 'id', 'id, course_code');
foreach ($allcoursecodes as $id => $coursecodeobject) {
$coursecodearray[$id] = $coursecodeobject->course_code;
}
$coursecode = $mform->addElement('select', 'coursecode', get_string('coursecode', 'block_usp_mcrs'), $coursecodearray);
$mform->addRule('coursecode', get_string('required'), 'required', null, 'client');
$mform->setType('coursecode', PARAM_RAW);
// Course Name field. TODO: Course Name to pick automatically after entering Course Code
$coursenamearray = array();
$coursenamearray[0] = get_string('choosecoursename', 'block_usp_mcrs');
$allcoursenames = $DB->get_records_select('block_usp_mcrs_courses', 'id > 0', array(), 'id', 'id, course_name');
foreach ($allcoursenames as $id => $coursenameobject) {
$coursenamearray[$id] = $coursenameobject->course_name;
}
$mform->addElement('select', 'coursename', get_string('coursename', 'block_usp_mcrs'), $coursenamearray);
$mform->addRule('coursename', get_string('required'), 'required', null, 'client');
$mform->setType('coursename', PARAM_RAW);
Any help would be appreciated.
You have to achieve this using javascript, as Moodle has no functionality to fill data in a nested way.
Add AMD module js file where you are calling your mform for display purpose.
In the file where you render your mform
$mform->render();
add below line to call you amd js file.
$PAGE->requires->js_call_amd('local_acestructure/registration', 'init');
In your amd js file make httprequest / ajax call to fetch data based on your course_code select drop down change.
Thank you.

Symfony2 - Reset form field after submission

I wonder whether there is a simple way to reset one of the form fields after form submission and then pass the form to the view - to render the same form but one of the field will be with it's default value. Something like:
$form = $this->createForm('MyForm');
$form->handleRequest($request);
if ($form->isValid()) {
// do something
// ...
// reset one of the form field to it's default value
// something like:
// $form->field->reset() or
// $form->field->setValue('');
}
return array(
'form' => $form->createView(),
);
Thanks!
You can use:
$form->get('field')->submit($defaultValue);
Check out the doc.

Codeigniter - How to populate form from database?

I have a small site which allows a user to enter values in a form and then either submit it directly or store the field values in a template to later submit it. To submit the form later, he can load the previously saved template. For that there are three buttons Load Template / Save Template / Submit form.
Because i am using the form validation built-in functionality from Codeigniter i run into problems when i want to populate the form with a template, which had been previously stored.
The form fields are all set up like
$name = array(
'name' => 'name',
'id' => 'name',
'value' => set_value('name', $form_field_values['name'])
);
The variable $form_field_values holds the values from either a loaded template in the case when a template has been loaded or the default values when the form is first loaded.
Initially the form is loaded with the default values. When i click on Load Template the values from the template are not chosen by set_value() because there were the default values in there before. What i want is to replace the values of the form fields with the ones from the template.
Do you have any idea how to do that in a clean approach? What i have done is to introduce a variable to skip the call to set_value() completely like:
$name= array(
'name' => 'name',
'id' => 'name',
'value' => $skip_form_validation ? $form_field_values['name'] : set_value('name', $form_field_values['name'])
);
Where $skip_form_validation is a variable set in the controller, based on what button was pressed. Form validation is skipped for saving/loading a template.
Codeigniter's set_value() function is a simple function which finds value in $_POST if value found then return else returns second argument, you can remove set_value() and write your own code for it. you can write $_POST['field_name'] if you want to populate value of POST data or add whatever value you want to add
Just use like this
$name = array(
'name' => 'name',
'id' => 'name',
'value' => $valueFromYourTemplate
);
You don't need to use set_value() function if you don't want to set POST values in the form
Assuming you retrieve the database fields and pass them to a data array in your controller.
$record = $this->data_model->get_record(array('uid' => $user_id), 'users');
if (!is_null($record)) {
$data['uname'] = $record->username;
$data['loc'] = $record->location;
}
where 'users' is the database table, and the uid is the id field of the table users.
In your form, do something like this
Hope it helps!

cck hidden field, fill editing user

i have a cck custom type that is created by people and the fields are filled.
Then someone else edit those nodes and adds more data. I want to save the username of the user editing the content into an hidden field.
i know i can get the user with this:
global $user;
$a = $user->name;
return array(
0 => array('value' => $a)
);
and i have put this as default code for the hidden field, but the field now is filled with the creator of the node, and then is not replaced with the editor.
How can i solve my issue?
i do research on your problem, here is a solution enjoy!!!
Create a custom module and use the following code.
//Implementation of hook_nodeapi()
function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch($op) {
case 'presave':
if($node->type == "Your content type name")
{
global $user;
//In my case
//$node->field_username[0]['value'] = $user->name;
//In your case it will be like
$node->hidden_field_name[0]['value'] = $user->name;
}
break;
}
}
When you test editing the node yourself, does the field contain your own username or the original author?
An alternative solution is to form_alter the specific node edit form and on node_save, fill in the hidden field with the user name.

To create check box in symfony using widgets and also validate it?

the code in symfony that i am using,
$this->setWidgets(array(
'mobile' =>new sfWidgetFormInput(),
'subscribetosms' =>new sfWidgetFormInputCheckbox(),
));
i want to validate the checkbox, and also code to take values from check box
to validate form fields in symfony u need to set validators like this (assuming you are in a form class):
$this->setValidators(array(
'mobile' => new sfValidatorString(array(...)),
'subscribetosms' => new sfValidatorInteger(array(...))
));
Question is, what do you want to validate? If you want some kind of value send to your php script if the checkbox is selected you need to set this value in the widget.
new sfWidgetFormInputCheckbox(array('value_attribute_value'=>'your_value' )
Now you could configure your validator to validate this value (sfValidatorString for a string, of sfValidatorInteger for an integer).
To get the value in your action after the validation:
if ($this->form->isValid()) {
$myValue = $this->form->getValue('subscribetosms');
}

Categories