Zend Form Select, managing it using the array notation - select

I've a form in Zend that I use to manage the privileges of a series of resources (we're talking about dynamic ACLs stored in a db). So, for each of them I want to decide, through a select element (dropdown) who has the access. The controller will receive the request and handle it. My question is: how can I do to add elements to the form for having the possibility to store all the choices in one single array composed by many elements as the number of select element of form? Can I ask you a short real example? Thank you!

$myArray = array( ''=>'Select',
'Father'=>'Father',
'Mother'=>'Mother',
'Brother'=>'Brother',
'Sister'=>'Sister',
'Daughter'=>'Daughter',
'Son'=>'Son',
'Other'=>'Other',
);
'relationship' => array(
'type'=>'select',
'options'=>
array(
'label'=>'Relation',
'multiOptions' => $myArray,
)
),
OR
check this

Related

How to access backpack fields on custom (non-CRUD) page?

I've made a custom page in backpack admin panel. This page is non-CRUD (not related to any model). There are several forms on it, with date pickers, select inputs, etc. So I'am trying to find a way to use backpack fields to create these date pickers and select inputs. Because it seems to be awkward to embed custom js-controls into the project, as Backpack already has appropriate fields.
The only solution I came up with, is to create a crud controller for random model, disable all operations except create, use create operation view as custom page (backpack fields are available this way), and finally override store() method - to prevent creating new model entry in DB.
So, is there a proper way to access backpack fields on custom (non-CRUD) page?
Backpack 4.x fields aren't meant to be used outside CRUDs, but you can do that.
Option A
At their core, Backpack fields are just Blade views, so you can load them using the Blade helper #include(). Just make sure to pass along all variables that the blade file needs. I believe in 99% of the fields that will be a $field and a $crud variable, so this will work:
#php
// set the CRUD model to something (anything)
// but ideally it'd be the model of the entity that has the form
$crud = app()->make('crud');
$crud->setModel(\App\Models\Monster::class);
#endphp
#include('crud::fields.number', [
'crud' => $crud,
'field' => [
'name' => 'price',
'label' => 'Price',
'prefix' => '$'
]
])
This way, you only load the bits you actually want (the inputs), without the overhead of a CrudController. You can point the form to your custom controller and do the saving yourself. What you need to pass for a $field above is a Backpack field definition in array form.
This way is super-simple, but it has a big downside if you ask me. The field definition has to be 100% correct and complete, you lose all the magic and assumption logic that Backpack usually does to make your life easier when you add field using addField(). That's why in most cases I think it's more convenient to go with Option B.
Option B
Instead of manually loading all each field Blade view, add them using addField(), then load all of them just like Backpack does it in the Create or Update operation:
#php
$crud = app()->make('crud');
$crud->setModel(\App\Models\Monster::class);
$crud->addField([
'name' => 'price',
'label' => 'Price',
'prefix' => '$'
]);
#endphp
<form method="post">
#include('crud::form_content', [ 'fields' => $crud->fields(), 'action' => 'create' ])
</form>
The above will produce an output like this:
The benefit of this second option is that you can "forget" to mention stuff in the field definition and Backpack will assume it, you can use the fluent syntax, you can use most Backpack features, really...

How to pass params in url to a backpack create form

I'm using backpack 3.3 on a laravel 5.5 installation.
I want to prefill two create form fields with two URL passed values.
Say you create a basic backpack crud, named Image.
Normally you can go to domain/admin/image/create to view and fill in the form.
But if I add one or two params in the url, I get a 404.
I guess I should manage the routes file (admin.php)
I tried this way:
Route::group(['prefix' => 'image/{k}/{cid}'], function()
{
CRUD::resource('image', 'ImageCrudController');
});
but still get a 404.
Any suggestions?
Thanks in advance.
Almost all field types have a default option. So when you define them, you can specify a default. And you can always pass a GET parameter for that default. So you can have something like this in your EntityCrudController:
$this->crud->addField[ // Text
'name' => 'title',
'label' => "Title",
'type' => 'text',
'default' => \Request::has('title')?\Request::has('title'):false, // default value
]);
You'd then be able to send you users to yourapp/admin/entity/create?title=your+default+value and have that default value show up in the field.
Hope it helps. Cheers!
it works for me easier
http://127.0.0.1:8000/admin/resultado/create?competencia=2
$this->crud->modifyField("competencia_id",[
'label' => "Competencia",
"default"=>$this->crud->request->query->get('competencia'),
.....

Symfony2 Form : Select an entity or add a new one

I have an order and a client entity.
I am wondering if it's possible with the actual Symfony2 form system to create an order form which will allow to:
Select several clients from a dropdown (mix of collection and entity form type)
And to create new clients on the fly (the default way for the collection type) if not in the dropdown list.
I've seen some way to do it by creating multiple forms in the same page, but this is not the way I would like to achieve it.
Are there any better ways to do this?
I had a similar problem which may lead to your resolution:
I have a Category and Item relationship (Many-to-One) and I wanted to either select an existing item or create a new item.
In my Form class:
$builder->add('item', 'entity', array(
'label' => 'Item',
'class' => 'ExampleItemBundle:Item',
));
$builder->add('itemNew', new EmbedItemForm(), array(
'required' => FALSE,
'mapped' => FALSE,
'property_path' => 'item',
));
$builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
if (!empty($data['itemNew']['name'])) {
$form->remove('item');
$form->add('itemNew', new EmbedItemForm(), array(
'required' => TRUE,
'mapped' => TRUE,
'property_path' => 'item',
));
}
});
You can map two fields in a form to the same property using the property_path option. Then, using form events, use the submitted data to make a decision and modify the form so that only one of the fields has a mapped option that is true.
If I have understood, you want to create and store new clients in a Form "on fly", at the moment. I think that you have to do that using JavaScript and set an additional action in your controller.
JS -> Capture the event to add new client to you database (i.e. "Add new" button click event)
JS -> Inside this event, call via AJAX to your controller with the values of new client. (Using FOSJsRoutingBundle is easy to do)
Symfony2 -> Inside your new action, store the new client in your database.
JS -> OnSuccess event, in your AJAX call, add the new Client to your DropDownBox
(ddb.append(new element tag)
Just doing that you have your new client stored in the database and added to your dropdownbox
For my part i had the same kind of problem and i resolved it by creating 2 attribute in my formType;
For example, for you it would be:
customer->entity
new_customer-> collection
In your order entity file you will have to add 3 methods (getter, setter, and remover) getter and remover don't do anything but setter should call the setCustomer(c)
I'm not sure if it is the best way but it's the only way I figure it out!
The collection Form type allows to add and delete on the fly with allow_add and allow_delete attribute.
More informations by following these 2 links :
Official collection form field type reference
Cookbook about add and delete on the fly with collection type
If you don't like to get supplementary forms on the same page, you can integrate them in dialog boxes... But you definitely need a form to create new items...

Add a dropdown list as custom field in magento

I added custom fields as described in magento add custom input field to customer account form in admin
But I want a select list, not only a text input one. I don't know which kind of parameter I have to set and how to tell the list of possible values.
Please help :)
Thanks,
Plantex
Where you might do something like:
$setup->addAttribute('customer', 'custom_attribute', array(
'type' => 'text',
'label' => 'Customer Custom Attribute',
));
Use these values instead:
$setup->addAttribute('customer', 'custom_attribute', array(
'type' => 'int',
'label' => 'Customer Custom Attribute',
'input' => 'select',
'source' => 'eav/entity_attribute_source_boolean',
));
The type is int because you will typically be storing the index of the value chosen, not the value itself. The input is select so the admin renderer knows which control to use. The source shown here is a common example, it provides an array of "Yes" and "No" values with numeric indexes.
There are many source models already in the Magento code that you can use and you can create your own too, look at any existing one to see how it returns an array. If you make your own and if it uses text indexes instead of numeric then the type will have to be changed back to text.
Try adding this at your module setup file
'value' => array('notate_to_zero'=>array(0=>'Bleu',0=>'Rouge',0=>'Vert',0=>'Violet',0=>'Noir',0=>'Orange'))
),
or look at this --> http://inchoo.net/ecommerce/magento/how-to-create-custom-attribute-source-type/

CakePHP Form in an Element Causing Errors

I think this may be an easy solution, but I've spent an hour now investigating to no avail.
I have a registration form in an element that is being used in views belonging to different controllers. Using the "url" attribute, I've told it to submit to /users/register, but for some reason, the fields aren't submitting to the database.
Instead, there are errors for "undefined index" and a MySQL error for an undefined secondary key that I set (it's empty because it's not being submitted). Strangely, the form works fine if I include the element somewhere in the users views. Does anyone know why this is happening?
UPDATE - Here's the relevant code, sorry:
<?php
echo $form->create(array(
'id' => 'signupform',
'url' => array(
'controller' => 'users',
'action' => 'register')));
?>
The form fields are all correct, since the element works in the user controller's views anywhere. Do I need to include any other information in the creation of the form to point it more directly?
Specify the model 'User' as the first parameter to the $form->create() method.
<?php
echo $form->create('User', array(
'id' => 'signupform',
'url' => array(
'controller' => 'users',
'action' => 'register')
)
);
?>
This will ensure that the form fields are named as data[User][field_name], and prevent you from seeing that undefined index error.
Hope this works for you!
Without seeing the code, it sounds like Cake is magically assuming that the Model is the one for the controller that controls the current view. When that controller is Users, it works correctly. When it is, say, Articles, it will be trying (and failing) to fit the form fields to the Article model.
Without seeing any code, it is impossible to offer any more help.
EDIT:
If the form contains mixed models, e.g. User and Article, you must prefix the fieldnames like this:
$form->input('User.username');
$form->input('Article.title');
etc.
If you don't, the controller will assume they all belong to its own model.