This topic contains 6 replies, has 2 voices, and was last updated by adriwan_kenoby 6 years ago.
- Topic
Hi OroTeam,
I would like to implement a dynamic form field. I follow the symfony instruction here but it not working on oro context.
In need of some advice please.
Here is my code :
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257<?phpclass Host extends ExtendHost implements OrganizationAwareInterface{use AuditableBusinessUnitAwareTrait;const DEFAULT_LOGIN = 'root';/*** @var integer** @ORM\Column(type="integer")* @ORM\Id* @ORM\GeneratedValue(strategy="AUTO")*/protected $id;/*** @var string** @ORM\Column(type="string", length=255)* @Assert\Type("string")* @Assert\NotBlank()* @ConfigField(* defaultValues={* "dataaudit"={* "auditable"=true* }* }* )*/protected $hostname;/*** @var string** @ORM\Column(type="string", length=255)* @Assert\NotBlank()* @Assert\Url(* message = "The url '{{ value }}' is not a valid url",* checkDNS = "ANY",* dnsMessage = "The host '{{ value }}' could not be resolved."* )* @ConfigField(* defaultValues={* "dataaudit"={* "auditable"=true* }* }* )*/protected $url;/*** @var string** @ORM\Column(type="string", length=45)* @Assert\NotBlank()* @Assert\Ip(version="all")* @ConfigField(* defaultValues={* "dataaudit"={* "auditable"=true* }* }* )*/protected $ip;/*** @var integer** @ORM\Column(name="ssh_port", type="smallint")* @Assert\NotBlank()* @Assert\Type("integer")* @ConfigField(* defaultValues={* "dataaudit"={* "auditable"=true* }* }* )*/protected $sshPort;/*** @var string** @ORM\Column(type="b_password")* @Assert\NotBlank()* @ConfigField(* defaultValues={* "dataaudit"={* "auditable"=true* }* }* )*/protected $login = self::DEFAULT_LOGIN;/*** @var Sqlserver* @ORM\OneToOne(targetEntity="Sqlserver", inversedBy="host", cascade={"all"}, orphanRemoval=true)*/protected $sqlserver;/*** Get id** @return int*/public function getId(){return $this->id;}/*** Set hostname** @param string $hostname** @return Host*/public function setHostname(string $hostname){$this->hostname = $hostname;return $this;}/*** Get hostname** @return string*/public function getHostname(){return $this->hostname;}/*** @param $url* @return $this*/public function setUrl($url){$this->url = $url;return $this;}/*** @return string*/public function getUrl(){return $this->url;}/*** Set ip** @param string $ip** @return Host*/public function setIp(string $ip){$this->ip = $ip;return $this;}/*** Get ip** @return string*/public function getIp(){return $this->ip;}/*** Set sshPort** @param integer $sshPort** @return Host*/public function setSshPort(int $sshPort){$this->sshPort = $sshPort;return $this;}/*** Get sshPort** @return integer*/public function getSshPort(){return $this->sshPort;}/*** Set login** @param string $login** @return Host*/public function setLogin(string $login){$this->login = $login;return $this;}/*** Get login** @return string*/public function getLogin(){return $this->login;}/*** @param Sqlserver $server* @return $this*/public function setSqlserver(Sqlserver $server){$this->sqlserver = $server;$server->setHost($this);return $this;}/*** @return Sqlserver*/public function getSqlserver(){return $this->sqlserver;}/*** @return string*/public function __toString(){return (string)$this->url;}}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150<?phpclass Sqlserver extends ExtendSqlserver implements OrganizationAwareInterface{use AuditableBusinessUnitAwareTrait;const PDO_MYSQL = 'pdo-mysql';const DRIZZLE_PDO_MYSQL = 'drizzle-pdo-mysql';const MSQLI = 'mysqli';const PDO_PGSQL = 'pdo-pgsql';const SQLSRV = 'sqlsrv';const OCI8 = 'oci8';const SQLANYWHERE = 'sqlanywhere';/*** @var integer** @ORM\Column(type="integer")* @ORM\Id* @ORM\GeneratedValue(strategy="AUTO")*/protected $id;/*** @var Host* @ORM\OneToOne(targetEntity="Host", mappedBy="sqlserver")* @ORM\JoinColumn(nullable=false)*/protected $host;/*** @var string** @ORM\Column(type="b_password")* @ConfigField(* defaultValues={* "dataaudit"={* "auditable"=true* }* }* )*/protected $login = Host::DEFAULT_LOGIN;/*** @var string** @ORM\Column(type="b_password")*/protected $password;/*** @var string** @ORM\Column(type="string", length=17)* @ConfigField(* defaultValues={* "dataaudit"={* "auditable"=true* }* }* )*/protected $driver;/*** @return int*/public function getId(){return $this->id;}/*** @return Host*/public function getHost(){return $this->host;}/*** @param Host $host* @return $this*/public function setHost(Host $host){$this->host = $host;return $this;}/*** @return string*/public function getLogin(){return $this->login;}/*** @param string $login* @return $this*/public function setLogin(string $login){$this->login = $login;return $this;}/*** @return string*/public function getPassword(){return $this->password;}/*** @param string $password* @return $this*/public function setPassword(string $password){$this->password = $password;return $this;}/*** @return string*/public function getDriver(){return $this->driver;}/*** @param string $driver* @return $this*/public function setDriver(string $driver){$this->driver = $driver;return $this;}public function __toString(){return $this->host->getHostname();}}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354<?php<?phpclass SqlserverType extends AbstractType{const NAME = "sinabs_sqlserver_type";/*** {@inheritdoc}*/public function getName(){return $this->getBlockPrefix();}/*** {@inheritdoc}*/public function getBlockPrefix(){return self::NAME;}/*** @param OptionsResolverInterface $resolver*/public function setDefaultOptions(OptionsResolverInterface $resolver){$resolver->setDefaults(array('data_class' => Sqlserver::class));}public function buildForm(FormBuilderInterface $builder, array $options){$builder->add('host')->add('login')->add('password')->add('driver', ChoiceType::class, ['choices' => [Sqlserver::PDO_MYSQL => Sqlserver::PDO_MYSQL,Sqlserver::DRIZZLE_PDO_MYSQL => Sqlserver::DRIZZLE_PDO_MYSQL,Sqlserver::MSQLI => Sqlserver::MSQLI,Sqlserver::PDO_PGSQL => Sqlserver::PDO_PGSQL,Sqlserver::SQLSRV => Sqlserver::SQLSRV,Sqlserver::OCI8 => Sqlserver::OCI8,Sqlserver::SQLANYWHERE => Sqlserver::SQLANYWHERE],// always include this'choices_as_values' => true]);}}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384<?php<?phpclass HostType extends AbstractType{const NAME = 'sinabs_host_type';/*** {@inheritdoc}*/public function getName(){return $this->getBlockPrefix();}/*** {@inheritdoc}*/public function getBlockPrefix(){return self::NAME;}/*** @param OptionsResolverInterface $resolver*/public function setDefaultOptions(OptionsResolverInterface $resolver){$resolver->setDefaults(array('data_class' => Host::class,'cascade_validation' => true));}/*** @param FormBuilderInterface $builder* @param array $options*/public function buildForm(FormBuilderInterface $builder, array $options){$builder->add('hostname')->add('url', UrlType::class)->add('ip')->add('sshPort')->add('login')->add('create_sqlserver', CheckboxType::class, ['label' => 'sinabs.hosting.host.create_sqlserver.label','required' => false,'mapped' => false,'value' => false]);$builder->addEventListener(FormEvents::POST_SUBMIT, [$this, 'onPostSubmit']);}/*** @param FormInterface $form* @param Sqlserver $server*/private function onPostSubmit(FormEvent $event){$host = $event->getData();$form = $event->getForm();if (!$host) {return;}if ($host['create_sqlserver']) {$form->add('sqlserver', SqlserverType::class, ['required' => false,'label' => 'sinabs.hosting.sqlserver.entity_label'])->remove('host');} else {if ($form->get('sqlserver')) {$form->remove('sqlserver');}unset($host['sqlserver']);$event->setData($host);}}}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104{% extends 'OroUIBundle:actions:update.html.twig' %}{% form_theme form with 'OroFormBundle:Form:fields.html.twig' %}{% if form.vars.value.id %}{% set formAction = path('sinabs_host_update', { 'id': form.vars.value.id }) %}{% else %}{% set formAction = path('sinabs_host_create') %}{% endif %}{% block navButtons %}{% if form.vars.value.id and resource_granted('DELETE', form.vars.value) %}{{ UI.deleteButton({'dataUrl': path('sinabs_api_delete_host', {'id': form.vars.value.id}),'dataRedirect': path('sinabs_host_index'),'aCss': 'no-hash remove-button','id': 'btn-remove-tag','dataId': form.vars.value.id,'entity_label': 'sinabs.hosting.host.entity_label'|trans}) }}{{ UI.buttonSeparator() }}{% endif %}{{ UI.cancelButton(path('sinabs_host_index')) }}{% set html = UI.saveAndCloseButton() %}{% if resource_granted('sinabs_host_update') %}{% set html = html ~ UI.saveAndStayButton() %}{% endif %}{{ UI.dropdownSaveButton({ 'html': html }) }}{% endblock navButtons %}{% block pageHeader %}{% if form.vars.value.id %}{% set breadcrumbs = {'entity': form.vars.value,'indexPath': path('sinabs_host_index'),'indexLabel': 'sinabs.hosting.host.entity_label'|trans,'entityTitle': form.vars.value.hostname} %}{{ parent() }}{% else %}{% set title = 'oro.ui.create_entity'|trans({'%entityName%': 'sinabs.hosting.host.entity_label'|trans}) %}{% include 'OroUIBundle::page_title_block.html.twig' with { title: title } %}{% endif %}{% endblock pageHeader %}{% block content_data %}{% set id = 'host-edit' %}{% set dataBlocks = [{'title': 'General'|trans,'class': 'active','subblocks': [{'title': '','data':[form_row(form.hostname),form_row(form.ip),form_row(form.sshPort),form_row(form.login)]}]}] %}{% set dataBlocks = dataBlocks|merge([{'title': 'sinabs.hosting.sqlserver.entity_label'|trans,'subblocks': [{'title': '','data':[form_row(form.create_sqlserver),form.sqlserver is defined ? form_row(form.sqlserver) : null]}]}] ) %}{% set data = {'formErrors': form_errors(form)? form_errors(form) : null,'dataBlocks': dataBlocks,} %}<script type="text/javascript">require(['jquery'], function ($) {var create_sqlserver = $('input[id^=sinabs_host_type_create_sqlserver-uid-]');var form = create_sqlserver.closest('form')create_sqlserver.change(function() {var data = {}data['create_sqlserver'] = create_sqlserver.val()$.ajax({url: form.attr('action'),type: form.attr('method'),data: data,success: function (res) {$('input[id^=sinabs_host_type_sqlserver]').replaceWith($(res).find('input[id^=sinabs_host_type_sqlserver]'))}})})})</script>{{ parent() }}{% endblock content_data %}
The forum ‘OroPlatform – Programming Questions’ is closed to new topics and replies.