evolution-cms / evolution

Welcome to the new evolution of MODX Evolution!
GNU General Public License v3.0
260 stars 95 forks source link

eForm to Formlister #521

Closed modxuser closed 6 years ago

modxuser commented 6 years ago

Is there an equivalent of the following for FormLister ?

&eformOnBeforeMailSent=``

I use this to run a snippet that gets the basic server information of the sender

The snippet code:

<?php
function getServer( &$fields ){
$fields['remote_addr']=$_SERVER['REMOTE_ADDR'];
$fields['remote_host']=$_SERVER['REMOTE_HOST'];
$fields['user_agent']=$_SERVER['HTTP_USER_AGENT'];
return true;
}
?>
Pathologic commented 6 years ago

I've got tired to answer questions that are answered in the docs.

modxuser commented 6 years ago

@Pathologic

I am sorry to bore you with my questions, If I could have found the information I would not have asked the question

Why don't you simply point me to the right place ? and not just say, read the docs.

Pathologic commented 6 years ago

https://github.com/Pathologic/FormLister/blob/master/assets/snippets/FormLister/docs/en/020_Parameters.md#prepare-prepareprocess-prepareafterprocess

Pathologic commented 6 years ago

https://github.com/Pathologic/FormLister/blob/master/assets/snippets/FormLister/docs/examples/ru/006_%D0%94%D0%B2%D0%B0_%D0%BD%D0%B0%D0%B1%D0%BE%D1%80%D0%B0_%D0%BF%D0%BE%D0%BB%D0%B5%D0%B9.md#prepare-сниппет-typeselector

modxuser commented 6 years ago

Thank you

Not being a PHP developer, I would never have known that that information does what the "eformOnBeforeMailSent" event does, even after reading it

We are sadly not all PHP savvy, that's something that should be taken into consideration, I am sure @dmi3yy knows this

Pathologic commented 6 years ago

Your code shall be:

<?php
$FormLister->setField('remote_addr', $_SERVER['REMOTE_ADDR']);
$FormLister->setField('remote_host', $_SERVER['REMOTE_HOST']);
$FormLister->setField('user_agent', $_SERVER['HTTP_USER_AGENT']);

or

<?php
$FormLister->setFields(array(
    'remote_addr' => $_SERVER['REMOTE_ADDR'],
    'remote_host' => $_SERVER['REMOTE_HOST'],
    'user_agent' => $_SERVER['HTTP_USER_AGENT']
));

Save it to snippet and point this snippet in prepare or prepareProcess parameter.

modxuser commented 6 years ago

Thank you very much - can I get you a coffee / beer ?

Pathologic commented 6 years ago

Some more examples:

//prepare-snippet to deny form procession if given name is foobar
$name = $FormLister->getField('name'); // $name = $data['name'] can be written as well
if ($name == 'foobar') {
    $FormLister->setValid(false);
}
//prepare-snippet to manual field validation
$name = $FormLister->getField('name');
if ($name == 'foobar') {
    $FormLister->addError('name', 'foobar', 'Your name cannot be foobar');
}
//prepareProcess-snippet to make report more nice
$data['message'] = nl2br(strip_tags($data['message']));
$data['name'] = ucfirst($data['name']);
$data['phone'] = empty($data['phone']) ? 'none' : $data['phone']; // assume that phone is not required field but we want to write 'none' in report if user has left it blank

return $data; // $FormLister->setFields($data) can be written as well

Snippets from prepare parameter are run before form validation, from prepareProcess - after form validation but before the main controller action (sending mail or creating user etc.) Sorry, but full usage of this feature requires to study PHP a bit.

Pathologic commented 6 years ago

https://github.com/Pathologic/FormLister/blob/master/assets/snippets/FormLister/docs/examples/ru/006_%D0%94%D0%B2%D0%B0_%D0%BD%D0%B0%D0%B1%D0%BE%D1%80%D0%B0_%D0%BF%D0%BE%D0%BB%D0%B5%D0%B9.md - in this examples validation rules and report depend on radio button value.

modxuser commented 6 years ago

I have set it to "prepareProcess" but tested both and both work

Pathologic commented 6 years ago

Yes, but prepare-snippets are executed every time, while prepareProcess - only once. There's no difference in this particular case, but it can be if you are working with DB or CURL.

modxuser commented 6 years ago

Thank you for the in-depth explanations, they have given me a better understanding of what I am doing - hopefully I can portray that in a further tutorial for others to use

Pathologic commented 6 years ago

In general eForm events are too limited and more difficult to understand. Prepare-snippets are closer to Revo's FormIt hooks.

modxuser commented 6 years ago

@Pathologic

How can I get you a beer ?

Pathologic commented 6 years ago

It's a big problem in case of my country, so don't mention ((:

modxuser commented 6 years ago

OK, thanks again for all your help, it's very much appreciated and it not only helps me, it will help others to understand better

Deesen commented 6 years ago

Indeed, you both are doing a great job, much appreciated! Especially when all those infos will be added into evo-docs!

modxuser commented 6 years ago

Information added to Evolution Docs, can be found here