How to use form validation with the file uploader to make sure a file is uploaded - forms

Can anyone suggest how using the form validation rules I can say the following:-
If no file is uploaded - then create a rule to say 'no file uploaded' using the form validator library.
I am using CodeIgniter 2.
For instance - it is simple to validate on a text input field using the following, but I cannot understand how this is done with upload (that uses the $_FILES array rather than $_POST)
eg.
$this->form_validation->set_rules('title', 'Title', 'required'); // input field named 'title' is required

CodeIgniter's File Uploading class handles its own validation - no need to use the Form Validation class.
Per the documentation,
$this->upload->display_errors()
Retrieves any error messages if the do_upload() function returned
false. The function does not echo automatically, it returns the data
so you can assign it however you need.
In your example above, if the file input is left empty, $this->upload->display_errors() will return the following:
You did not select a file to upload.
So, set your preferences (see the "Setting Preferences" section of the documentation for exactly what is available):
// The following are just examples from the documentation...
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// ... other preferences as necessary
If any of the above fail during the attempted upload, $this->upload->display_errors() will retrieve the appropriate error message. Simply pass it to your view to display the error(s).
Hope that helps.

Related

Joomla 3.9.19 get the constants from global configuration in component display function

I have created a component and need to get a constant value in the component administrator and list the item according to it. I have tried different ways to get the constant value in the 'display()' function in 'view.html.php'. I am expecting the value will be available in the default.php with $this.
I have defined the constant in the configuration.php file, A URL is supposed to define.
define('CONSTANTVALUE', 'the URL string');
in the display() function,
$config = JFactory::getConfig();
$constantvalue = $config->get('CONSTANTVALUE');
$this->constantvalue= $constantvalue;
Seems this is not working.
Then, I have tried
JFactory::getApplication()->get('CONSTANTVALUE');
That also not working.
I have referred to this thread, Joomla 3 - How to get value from configuration file?
How can I get the constant from configuration to component view file?

Moodle 3.3 - check if a user is authenticated in Mustache template

I'm creating a mustache template for a Moodle site and want to display some content on the front page but only if the user is not yet logged in. I was hoping I could do something like this in the template:
{{^usernotloggedin}}
My content for users not logged in.
{{/usernotloggedin}}
However I can't find any documentation on the user variables available to mustache to test if a user is authenticated or not.
Any advice of where to look or how to implement this would be great.
You would need to adjust the code that calls the template to add that data to the context that is passed to the template. In that case you can use the isloggedin() function to set the value you want to pass to the template.
I hope I'm not quite late to this question by anyhow I'd like to add my answer to this as well
Your mustache files are simple templates which cannot perform logic. What you can do is
in your controller or the file which calls the render function (which renders the mustache file and gives HTML), apply a check there
require( '/path/to/moodle/config.php' );
if (isloggedin()) {
echo "you are logged in";
}
Once you have applied the check you can send the array with a flag identifier (ONLY TRUE or FALSE) which the mustache files can understand.
for eg
in your controller/block etc you can do the following
$tagcloud = core_tag_collection::get_tag_cloud($this->config->tagcoll, $this->config->showstandard == core_tag_tag::STANDARD_ONLY, $this->config->numberoftags, 'name', '', $this->page->context->id, $this->config->ctx, $this->config->rec);
$content = $tagcloud->export_for_template($OUTPUT);
require( '/path/to/moodle/config.php' );
$flag = isloggedin() ? TRUE : FALSE;
array_push($content, $flag);
$this->content->text = $OUTPUT->render_from_template('core_tag/search_course_by_tags', $content);
and in your mustache file
{{#flag}}
your fancy code here which will onyl work if the user is logged in
{{/flag}}

Symfony FileFormField - Testing (WebTestCase) multiple file upload

In my Symfony web application I have a form allowing multiple file upload (easily done by setting the multiple property of the FileType equal to true). And this works fine: I can select multiple files and upload them. Processing the form and getting all uploaded files also goes fine. But of course, I want to foresee an integration test (WebTestCase) but I don't find any possibility to simulate a multiple file upload.
What I have now:
...
$uploadedFile = new UploadedFile(...);
$form = ...; // get the form from the crawler
$form['formtype[filename]'][0]->upload($uploadedFile);
$this->client->submit($form);
...
That works fine.
But now I want to upload 2 files by 1 form submission (because the processing logic can behave differently when multiple files are uploaded at once). How can I do this? When I look at http://api.symfony.com/3.0/Symfony/Component/DomCrawler/Field/FileFormField.html I don't see any way to pass in, for example, an array of UploadedFile objects. Anyone experience with this?
If the multiple property is set, crawler creates a file form field array with single FileFormField field. One field can hold a single file so you need multiple fields for multiple files. I came to a solution by manually adding more FileFormField to the form.
$form = ...
// get file field node in DOM
$node = $crawler->filter("input[name='formtype[filename][]']")->getNode(0);
// add additional fields to form (you can create as many as you need)
$newField = new FileFormField($node);
$form->set($newField);
...
// set files with upload()
$form['formtype[filename]'][0]->upload($uploadedFile1);
$form['formtype[filename]'][1]->upload($uploadedFile2);
...
//or with submit values
$crawler->submit($form, [
...
'formtype[filename]' => [$uploadedFile1, $uploadedFile2]
]);

Trying to add a logic hook to suiteCRM when creating or updating a task

This is my first try into coding for sugarCRM / suiteCRM.
I should say I've been coding for Wordpress for nearly 10 years now, but I'm completely lost now I'm starting to dig into suiteCRM.
I've read that you can add a logic hook to modify the data after saving it to the database, but I don't know where to start...
Imagine I create a task for today, july 7th, related to a client I use to visit every 2 months, so there's a field in Accounts named "Visiting frequency". I'd like to add a future date (july 7th + 60 days = september 7th aprox) into the task's "Future Visiting Date" field, so I can use it to create that particular future task via Workflow.
What I'm trying to do is to calculate a field in tasks (Future visiting date), that equals to the amount of days on the accounts module's field (Visiting frequency) added to the task's own Date field.
I've been able to make it work, using the following layout:
Inside \custom\modules\Tasks\logic_hooks.php
<?php
$hook_version = 1;
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(
1, //Processing index. For sorting the array.
'future_task_date_on_task_creation', //Label. A string value to identify the hook.
'custom/modules/Tasks/future_visit_date.php', //The PHP file where your class is located.
'before_save_class', //The class the method is in.
'future_visit_date' //The method to call.
);
?>
Inside \custom\modules\Tasks\future_visit_date.php
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class before_save_class {
function future_visit_date($bean, $event, $arguments) {
$bean->rhun_fecha_sig_c = date("Y-m-d H:i:s", $date);
}
}
?>
With this setup, the Future Visiting Date gets filled with the calculated date.
I've also read that this setup is not advised, and that I should use the Extension Framework and put the first file in this path:
/custom/Extension/modules/Tasks/Ext/LogicHooks/<file>.php
But I can't make it work.
Do I have to create the LogicHooks folder if it's not there?
Which filename should I assign to this file?
Do I have to change something else inside the code?
Yes, create the LogicHooks directory if it doesn't exist. The PHP file can be called anything you like.
/custom/Extension/modules/Tasks/Ext/LogicHooks/MyLogicHookFile.php
Define your logic hooks in this file as before.
<?php
$hook_version = 1;
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(
1, //Processing index. For sorting the array.
'future_task_date_on_task_creation', //Label. A string value to identify the hook.
'custom/modules/Tasks/future_visit_date.php', //The PHP file where your class is located.
'before_save_class', //The class the method is in.
'future_visit_date' //The method to call.
);
Then run a repair and rebuild from the Admin panel.
The main advantage to using the Extension framework is that it allows multiple developers to add components to a Sugar instance without worrying about overwriting existing code.
More info can be found about it in the Developer Guide

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....