Symfony form and iterator input checkbox / text - forms

I have this form:
->add('task', 'text')
->add('dueDate', 'date')
->add('save', 'submit')
I would like to iterate an input text / checkbox using data from database.
[update] in other words I want to print fields according to data inside my database.
Like a foreach but all using the form symfony ($result as $val){ print'input type="checkbox" value="valore"' }
Can you help me ?

What you are looking for is likely the form bulder: http://symfony.com/doc/current/book/forms.html#building-the-form
//In your controller
$results = //get data from the database here
$form = $this->createFormBuilder();
foreach($results as $result){
$form->add($result['field_name'],$result['field_type'],array('data=>$result['field_value']'));
//Where field_name would be something like task, dueDate, save, etc.
// field_type would be something like, text, choice, hidden
//then to set the data for the field you pass it in the data option aka field_value
}
$form->getForm()->createView;

Related

CakePHP 3 - Exclude fields by default in query unless specifically selected

Title pretty much says it all. I have some tables with fields that contain a lot of data. To save some performance I would like to not SELECT these by default.
The emphasis on the new default behaviour, differentiating the question from e.g. Select all except one field in cakephp 3 query
Example:
$cities = $this->Cities->find();
// A $city does not include the field `shape` (which is a huge polygon)
$cities = $this->Cities->find(['id', 'name', 'shape']);
// A $city now does include the `shape` property
I looked at the accessible and hidden properties of an entity, but these don't seem to affect the SELECT statement.
EDIT: The selectAllExcept query seems usefull. I combined this with the beforeFilter event like this:
public function beforeFind($event, $query, $options, $primary)
{
$query->selectAllExcept($this, ['shape']);
}
This works well for empty queries, shape is now excluded. But now I have no control over the other fields that might want to include or not:
$this->Cities->find()->select(['id', 'shape']) will then also select the other fields because the selectAllExcept().
You can simple overwrite find('all') method in your table.
For example in UsersTable:
public function findAll(Query $query, array $options)
{
$query->selectAllExcept($this, ['password']);
return $query;
}
then in your controller:
// select all except password
$users = $this->Users->find();
debug($users);
OR
// we try to select some fields, without success
$users = $this->Users->find()->select(['id', 'username', 'password']);
debug($users);
OR
// we try to select some fields incl. password, with success
$users = $this->Users->find()->select(['id', 'username', 'password'], true); // <-- this overwrite select / selectAllExcept in custom finder
debug($users);

Gravity Forms - Dynamically populate a drop down based on entries from another gravity form

So I am looking at dynamically populating a drop down field in a form with entries from another gravity form. Based on that choice, I will populate a second drop down dynamically based on entries from a gravity form.
More or less I am creating the ability to submit a work order. In that work order I want users to select a piece of equipment then based on that piece of equipment they can select a part.
I have looked at gform_get_entries_args_entry_list as a way of snagging entries, but I am not overly sure how to set it so it pulls from a particular form.
add_filter( 'gform_get_entries_args_entry_list', 'machine' );
function populate_posts( $form ) {
foreach ( $form['fields'] as &$field ) {
$items = gform_get_entries_args_entry_list( 'NOT SURE WHAT TO PUT HERE' );
$choices = array();
foreach ( $items as $GRAVITY FORM ENTRY VARIABLE) {
$choices[] = array( 'text' => $GFEV->THE MACHINE NAME, 'value' => $GFEV ->The Name Field of the machine );
}
$field->placeholder = 'Select a Post';
$field->choices = $choices;
}
return $form;
}
The Gravity Forms Dynamic Population Pro plugin uses the GFAPI:get_entries function, which you can just directly pass the form ID.
Here's the definition:
public static function get_entries( $form_ids, $search_criteria = array(), $sorting = null, $paging = null, &$total_count = null )

Entity mapping in a Symfony2 choice field with optgroup

Suppose to have an entity in Symfony2 that has a field bestfriend, which is a User entity selected from a list of User entities that satisfy a complex requirement.
You can render this field in a form by specifying that it is an entity field type, i.e.:
$builder->add('bestfriend', 'entity', array(
'class' => 'AcmeHelloBundle:User',
'property' => 'username',
));
This form field is rendered as a <select>, where each one of the displayed values is in the form:
<option value="user_id">user_username</option>
So, one would render the field by using the <optgroup> tags to highlight such special feature of the friends.
Following this principle, I created a field type, namely FriendType, that creates the array of choices as in this answer, which is rendered as follows:
$builder->add('bestfriend', new FriendType(...));
The FriendType class creates a <select> organized with the same <option>s but organized under <optgroup>s.
Here I come to the problem! When submitting the form, the framework recognize that the user field is not an instance of User, but it is an integer. How can I let Symfony2 understand that the passed int is the id of an entity of type User?
Here follows my solution.
Notice that it is not mentioned in the Symfony2 official docs, but it works! I exploited the fact that the entity field type is child of choice.
Hence, you can just pass the array of choices as a param.
$builder->add('bestfriend', 'entity', array(
'class' => 'AcmeHelloBundle:User',
'choices' => $this->getArrayOfEntities()
));
where the function getArrayOfEntities() is a function that fills the choice list with the friends of my friends, organized by my friends:
private function getArrayOfEntities(){
$repo = $this->em->getRepository('AcmeHelloBundle:User');
$friends = $repo->findAllFriendByComplexCriteria(...);
$list = array();
foreach($friends as $friend){
$name = $friend->getUsername();
if(count($friend->getFriends())>0){
$list[$name] = array();
foreach($friend->getFriends() as $ff){
$list[$name][$ff->getUsername()] = $ff;
}
}
}
return $list;
}
I know the example could be meaningless, but it works...
PS: You need to pass the entity manager to let it working...

Does the symfony form fields initialize to null if not visible

I have few fields on the form like name, description, timestamp.
Now in the form I am only displaying name and description but not timestamp.
public function __construct()
{
$this->setTimestamp(new \DateTime());
}
Now in my database, it is coming as null.
Either doctrine is not executing constructor or those fields are set to null when displayed in form.
Even though I am not displaying them.
You need put the timestamp field in your FormType.
If you don't need to show it, just hide the field and set default value.
Something like that:
$builder->add('timestamp', 'hidden', array(
'data' => new \DateTime(),
));
Don't forget {{form_rest(form)}} at end of the twig template to send all hidden fields.

How to use input hidden fields in smarty form

I want to use hidden field in smarty form
tell me the how i use this
i know how to add element like this
$form -> addElement('submit', 'submit_order', Pay, 'class = "flatButton"');
but i want to used hidden fields with this
Have you tried
$form->addElement('hidden', 'fieldname', 'Text', '');
?