How to overwrite form method on form created by code? - forms

On a form created in AOT you can rewrite methods by right click and overwrite. How can you do the same on the form which are created by X++ code?
For example. How to change close method so it will call info("close"); before closing on this:
form = new Form();
formBuildDataSource = form.addDataSource("Table");
formBuildDesign = form.addDesign("Design");
form.design().caption("Caption");
…
args = new Args();
formRun = classfactory.formRunClass(args);
formRun.run();
formRun.detach();
I am using AX2012

Typically you will want to execute a predefined method. You can then use the registerOverrideMethod method of form controls. This is explained here.
In the call to registerOverrideMethod always provide the third argument, being the object holding the method.
formButtonControl.registerOverrideMethod(
methodStr(FormButtonControl,clicked), //method to override
methodStr(testClass,testMethod), //method to invoke
new testClass()); //object of class containing method
It is of cause also possible to save source to the AOT using class TreeNode method AOTSetSource.
An example here.
You will need to save the form to AOT and compile before executing with FormRun.

Related

Setting callback for SEditableText widget to save text

I have implemented an SEditableText widget in my editor plugin but I can't figure out a reasonable way of accessing the value in the widget. I know there is an SEditableText.OnTextChanged() function but I don't see anyway to override it or set a callback of my own. Is there a standard way to save the content of an SEditableText to a variable?
I am working in the context of FModeToolKit, not sure if that makes a difference.
When you instantiate the slate widget, you should already be making a call to SNew. As part of the property initialiser chain list, initialise SEditableText::OnTextChanged and bind to your container UObject, using BIND_UOBJECT_DELEGATE. The delegate signature should be FOnTextChanged. Your handler will receive an FText which may be stored in a variable as required.
Example:
SEditableText EditableText = SNew(SEditableText)
.OnTextChanged(BIND_UOBJECT_DELEGATE(FOnTextChanged, OnTextChanged);
then:
void UMyUObject::HandleOnTextChanged(const FText& InText)
{
// Do something with text
}
Take a look at it in action in UEditableText::RebuildWidget (EditableText.cpp:50, v4.22.1).
NB. I know you specifically asked about OnTextChanged, but also consider OnTextComitted; this is only fired when the slate widget loses focus.
Ah; BIND_UOBJECT_DELEGATE is in UMG. The macro is however just a helper to create a UObject to which you may bind:
#define BIND_UOBJECT_DELEGATE(Type, Function) \
Type::CreateUObject( this, &ThisClass::Function )
So there's two options, either use <my type>::CreateUObject, or you could bind via a shared pointer. Off the top of my head, SP method should look something like:
Edit (based on comments):
If you're not linked against UMG, you could try manually creating the UObject based on the macro defined in SlateWrapperTypes in UMG:
#define BIND_UOBJECT_DELEGATE(Type, Function) \
Type::CreateUObject( this, &ThisClass::Function )
To instead use a shared pointer, the syntax should be along the following lines:
auto OnTextChangedSP = FOnTextChanged::CreateSP(this, &MyClass::MyOnTextChangedHandler);
SEditableText EditableText = SNew(SEditableText)
.OnTextChanged(OnTextChangedSP);

When does a model get updated after the model data has changed?

There is a method named updateBindings(true?) in openui5. But I'm not sure when I have to invoke it. Sometimes, setting the modified data to a model causes view changes, which indicates the underlying model data actually gets changed. Sometimes it won't work.
The first example demonstrates that the model doesn't get changed without updateBindings(true).
http://jsbin.com/hulavutoha/edit?html,css,output
The second example derives from the first one. But the model gets updated even without updateBindings(true).
http://jsbin.com/lepuladivu/edit?html,css,output
So, what's the difference between the two examples? When do I need to invoke updateBindings(true) on a model so that it will get updated? What's the intent of the parameter true passed to updateBindings()?
If you add a console print in your formatter function
formatter : function(books) {
console.log("go!!!");
return books[0];
}
you can see that in the first example the function is not executed.
This because if you change a leaf property the linked conponent in thew view (using data-binding) receive the change event only if it bind exactly the leaf property.
P.S.
Instead to use getData
var data = oModel.getData();
data.books[0] = "my book";
oModel.setData(data);
you can use getProperty
var data = oModel.getProperty("/");
data.books[0] = "my book";
//oModel.setProperty("/", data);
In this mode the last line is not required

How to customize register and contact forms in PrestaShop?

I need to know how to customize my contact and register forms. How to add new fileds ( and ) and make the information from these fields required or not required.
I need to know which files I must edit for these forms...
I use prestashop 1.4.7.0
This is really two separate questions as there are major differences in how you would handle each case.
Answer 1
For the registration form you can write a module which contains two hook handler functions. These will be:
public function hookCreateAccountForm() {}
public function hookCreateAccount($params) {}
The first function allows you to add additional fields to the registration form (by default these are inserted at the end of the form authentication.tpl, although you could move them all as a single group elsewhere). It should simply return the additional form html you require.
The second function provides you with two parameters to handle the account creation process. This is executed after the standard fields have been validated and the new customer has been created. Unfortunately you cannot do validation on your additional fields using this (you would need to either use javascript or override AuthController to perform your own authentication in the preProcess() member function). In one of my own custom modules for a site I have the following, for example:
public function hookCreateAccount($params)
{
$id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$customer = $params['newCustomer'];
$address = new Address(Address::getFirstCustomerAddressId((int)$customer->id));
$membership_number = $params['_POST']['membership_number'];
....
....
}
$params['newCustomer'] is a standard Prestashop element in the array and contains the newly created customer object. Your fields will be in the $params['_POST'] array - in my case it was an input field called membership_number.
Answer 2
For the contact form it's a whole lot more complicated I'm afraid. The simplest method for the html is to just hard-code your additional fields in the template file contact-form.tpl.
To actually process the form you will need to create an override for the controller by ceating a file called ContactController.php in /<web-root>/<your-optional-ps-folder>/override/controller containing something like:
<?php
class ContactController extends ContactControllerCore {
function preProcess()
{
if (Tools::isSubmit('submitMessage'))
{
// The form has been submitted so your field validation code goes in here.
// Get the entered values for your fields using Tools::getValue('<field-name>')
// Flag errors by adding a message to $this->errors e.g.
$this->errors[] = Tools::displayError('I haven't even bothered to check!');
}
parent::preProcess();
if (Tools::isSubmit('submitMessage') && is_empty($this->errors))
{
// Success so now perform any addition required actions
// Note that the only indication of success is that $this->errors is empty
}
}
}
Another method would be to just copy the entire preProcess function from controllers\ContactController and just hack away at it until it does what you want....

Symfony form gets messy when calling getObject() in form configuration

I have a Strain model that has a belongsTo relationship with a Sample model, i. e. a strain belongs to a sample.
I am configuring a hidden field in the StrainForm configure() method this way:
$defaultId = (int)$this->getObject()->getSample()->getTable()->getDefaultSampleId();
$this->setWidget('sample_id', new sfWidgetFormInputHidden(array('default' => $defaultId)));
Whenever I create a new Strain, the $form->save() fails. The debug toolbar revealed that it tries to save a Sample object first and I do not know why.
However, if I retrieve the default sample ID using the table it works like a charm:
$defaultId = (int)Doctrine_Core::getTable('Sample')->getDefaultSampleId();
$this->setWidget('sample_id', new sfWidgetFormInputHidden(array('default' => $defaultId)));
My question here is what can be happening with the getObject()->getSample()... sequence of methods that causes the StrainForm to think it has to save a Sample object instead of Strain.
I tried to debug with xdebug but I cannot came up with a clear conclusion.
Any thoughts?
Thanks!!
When you call getSample its creating a Sample instance. This is automatically attached to the Strain object, thus when you save you also save the Sample.
An altenrative to calling getSample would be to chain through Strain object to the Sample table since i assume youre only doing this so your not hardcodeing the Sample's name in related form:
// note Sample is the alias not necessarily the Model name
$defaultId = Doctrine_Core::getTable($this->getObject()->getTable()->getRelation('Sample')->getModel())->getDefaultId();
Your solution probably falls over because you can't use getObject() on a new form (as at that stage the object simply doesn't exist).
Edit: Why don't you pass the default Sample in via the options array and then access it from within the form class via $this->getOption('Sample') (if I remember correctly)?

Can I do more than one form for the same model class in symfony?

Well, imagine that we have a register form of a class Customer and we only ask three fields (name,surname,email) and after, when this user logged first time we want to complete this information.
First, we have in lib/form/doctrine a file called 'CustomerForm.class.php' wich is generated automatic on command line. In this file we 'setup' only 3 fields and validators and if we wanna use we do something like that:
$this->form = CustomerForm();
Second, we create manual another form named 'CustomerFormStep1.class.php' where we can setup for validate the other fields. But when we do..
$this->form = CustomerFormStep1();
it returns error: Fatal error: Class 'CustomerFormStep1' not found
What is wrong?
Thanks.
Assuming you have the form defined as:
class CustomerFormStep1 extends sfForm
or similar (sfFormDoctrine etc), and named correctly like you say (CustomerFormStep1.class.php) and in lib/form, then Symfony should just pick the definition up fine. Did you clear the cache after creating and placing it in the right place? (symfony cc).
Create the new CustomerFormStep1 class as #richsage instructed. Then, in your actions you can write something like:
public function executeLogin(){
//before login
$this->form = new CustomerForm();
}
public function executeLoggedIn(){
$this->form = new CustomerFormStep1();
//other steps
}
Haven't you read the tutorial? Extending forms is perfectly described in context with reh admin generator and can of course be applied to any case.