EasyAdmin 3 create custom field - forms

I'd like to create custom autocomplete field with externe api call.
I'm using doc Create custom fields
But my template is not used.
public static function new(string $propertyName, ?string $label = null): self
{
return (new self())
->setProperty($propertyName)
->setLabel($label)
// this template is used in 'index' and 'detail' pages
->setTemplatePath('admin/field/address.html.twig')
// this is used in 'edit' and 'new' pages to edit the field contents
// you can use your own form types too
->setFormType(TextType::class)
->addCssClass('field-address')
// these methods allow to define the web assets loaded when the
// field is displayed in any CRUD page (index/detail/edit/new)
->addCssFiles('js/admin/field-address.css')
->addJsFiles('js/admin/field-address.js')
;
}
I've no error.
I create template in : /templates/admin/field
Anyone has an idea ?

Related

How to require javascript after construction but before template render in a SilveStripe form field

I currently have a custom form field with a bunch of optional parameters in the constructor. I want to change this and have the field use setter functions but I can't find any way to include my templated JavaScript except during construction
class CustomField extends FormField {
protected $myField;
public function __construct($name, $title = null, $myField = null)
{
parent::__construct($name, $title);
$this->setMyField($myField);
Requirements::javascriptTemplate('path/to/script.js', ['Field' => $this->myField]);
}
/**
* I can update the value of myField but the value is already baked into the JavaScript and wont be updated
*/
public function setMyField($value) {
$this->myField = $value;
return $this;
}
I found a solution but it does feel a little hacky. I added a RequireJs() function to the form field as such:
<?php
function RequireJs() {
Requirements::javascriptTemplate('path/to/script.js', ['Field' => $this->myField]);
}
The added $RequireJs to the top of my template file so it would be called when the template is being rendered.

How and when to use createForm vs createFormBuilder

I am working with Symfony 3.4 and wonder how and when to use createForm vs createFormBuilder
In my case I need to create two different forms for the same data class, e.g. an user object User. The forms only differ in some additional fields.
For example one registration form for normal users with additional fields to accept the legal terms, subscribe to the newsletter, etc. And one registration form for admin users with some other additional fields. In both cases the same User class is used and the additional fields are not mapped:
public function regUserAction(Request $request) {
// Create and show user reg form
}
public function regAdminAction(Request $request) {
// Create and show admin reg form
}
Since the User form is needed/used in two different places it would be good idea to create a UserFormType and create the form using createForm
public function regUserAction(Request $request) {
$form = $this->createForm(UserFormType::class, $user, array());
}
public function regAdminAction(Request $request) {
$form = $this->createForm(UserFormType::class, $user, array());
}
class UserFormType extends AbstractType {
// setup of form fields, FormEvents handlers, etc.
}
Pro: The setup of the form has to implemented only once in UserFormType
Con: It is not possible to add custom fields to the created form, in regUserAction and regAdminAction, is it?
Since the User form needs to be customized it would be a good idea to use createFormBuilder instead:
public function regUserAction(Request $request) {
$formBuilder = $this->createFormBuilder($user, array())
->add(... common form setup ...)
->add(... custom user fields ...)
}
public function regAdminAction(Request $request) {
$formBuilder = $this->createFormBuilder($user, array())
->add(... common form setup ...)
->add(... custom admin fields ...)
}
Pro: Possible to add custom fields to the form
Con: Common setup of the form has to be implemented again for each form.
Is there any way to mix these two approaches? To implement the common form setup only once AND to add custom fields per form?
You can modify the form in the FormType using FormEvents like described here in the Symfony Docs. There is an example modifying a form based on user data.
Maybe you can implement the code in a service and return the $formBuilder object to customise it in your actions. Or you can implement a sort of factory pattern and build your $formBuilder based on the params you will pass to it.

How to integrate web services with custom module in SugarCRM?

I'm using SugarCRM to develop a software for customers management. I created a custom module from basic template with custom fields. Is it possible to get rid of SugarCRM db and perform CRUD operations through external web serivices? Actually I was able to show web services data in the datailview by setting the bean property of a custom controller.
class CustomerController extends SugarController{
public function action_detailview(){
$customer = new Customer();
$customer = getCustomerFromWebService();
$this->bean = $customer;
$this->view = "detail";
}
}
I would like to do the same thing with listview, but I don't know how set the records of the list (if it exists) used by the default listview.
You can change list view by customizing view.list.php in custom/modules/modulename/views/view.list.php using following code:
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('include/MVC/View/views/view.list.php');
// name of class match module
class modulenameViewList extends ViewList{
// where clause that will be inserted in sql query
var $where = 'like 'htc'';
function modulenameViewList()
{
parent::ViewList();
}
/*
* Override listViewProcess with addition to where clause to exclude project templates
*/
function listViewProcess()
{
$this->lv->setup($this->seed, 'include/ListView/ListViewGeneric.tpl', $this->where, $this->params);
echo $this->lv->display();
}
}
?>

Sortable lists with Symfony2 reusable

I've rewritten this entry of the Symfony1 Cookbook http://symfony.com/legacy/doc/cookbook/1_2/en/sortable to Symfony2. My first objetive is the classic sortable list:
All the documents(MongoDB) have a position field which is integer type with the order they will be show.
I've created a service class Sortable where are the following method:
class Sortable
{
//Return the position of the document
public function getByPosition($position = 1, $document, $site, $item = null){ /**/ }
//Return all elements sorted by position
public function getAllByPosition($document, $site){/**/}
//Return the max position of the items within a parent item
public function getMaxPosition($document, $site, $parent = null){/**/}
//Swap the position between $itemOne and $itemTwo
public function swapWith($itemOne, $itemTwo, $document){/**/}
//Set the new position => +1
public function executeUp($document, $id){/**/}
//Set the new position => -1
public function executeDown($document, $id){/**/}
//Persist document with the new position
public function save($item, $document){/**/}
}
This works fine but the main problem is that this class is hardly reusable.
-$document var is the name of Document in database to use for example on createQueryBuilder('MyBundle'.$document)
-$site var is the site of my app because each user has a site.
-$parent var is of type $document and it's the parent of the documents of the same type.
The problem for me, this is hardly reusable and I've to call all methods above in the controller action and then checking the position in Twig template. I want to achieve a twig extension which call to my service and do all the logic that I do in the controller and twig. Also that it works with whatever Document.
How I can get this?

Symfony: How to hide form fields from display and then set values for them in the action class

I am fairly new to symfony and I have 2 fields relating to my table "Pages"; created_by and updated_by. These are related to the users table (sfGuardUser) as foreign keys. I want these to be hidden from the edit/new forms so I have set up the generator.yml file to not display these fields:
form:
display:
General: [name, template_id]
Meta: [meta_title, meta_description, meta_keywords]
Now I need to set the fields on the save. I have been searching for how to do this all day and tried a hundred methods. The method I have got working is this, in the actions class:
protected function processForm(sfWebRequest $request, sfForm $form)
{
$form_params = $request->getParameter($form->getName());
$form_params['updated_by'] = $this->getUser()->getGuardUser()->getId();
if ($form->getObject()->isNew()) $form_params['created_by'] = $this->getUser()->getGuardUser()->getId();
$form->bind($form_params, $request->getFiles($form->getName()));
So this works. But I get the feeling that ideally I shouldnt be modifying the web request, but instead modifying the form/object directly. However I havent had any success with things like:
$form->getObject()->setUpdatedBy($this->getUser()->getGuardUser());
If anyone could offer any advice on the best ways about solving this type of problem I would be very grateful.
Thanks,
Tom
After processing and saving the form you could set those fields on the object and re-save:
protected function processForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()));
if ($form->isValid())
{
$page = $form->save();
$user = $this->getUser()->getGuardUser();
$page->setUpdatedBy($user);
if (empty($page->created_by))
{
$page->setCreatedBy($user);
}
$page->save();
$this->getUser()->setFlash('notice', 'Successfully saved page.');
$this->redirect('#homepage');
}
}
There's also a Doctrine extension called Blameable that automatically sets edited_by and created_by fields on specified models. The Doctrine website is undergoing some reorganization but here is the cached page for the extension.
To process your form create a new object, set the fields then save.
$article = new Article();
$article->setName($request->getParameter($form->getName());
$article->setDescription($request->getParameter($form->getDescription());
$article->setMetaKeywords($request->getParameter($form->getMetaKeywords());
$article->save();
What you want to do is customize your form and unset the 'created_at' and 'updated_at' pieces of the form in configure
class SampleForm extends BaseSampleForm
{
public function configure()
{
unset(
$this['created_at'],
$this['updated_at']
);
}
}
Then they won't show up in the form and will get the values setup by the "Timestampable" behavior before being saved
http://stereointeractive.com/blog/2010/04/07/symfony-forms-hide-created_at-updated_at-columns/