Перейти к содержимому


Фотография
- - - - -

validateSubmission not being called for custom Element


Сообщений в теме: 4

#1 net

net

Отправлено 19 August 2015 - 22:47

Hello,

 

we have created a new custom element to allow the client to select an option from a drop down select field and to upload a pdf file to the server. However when the form is submitted the _validateSubmission method on the element class is not called.

 

Should _validateSubmission be called on each form submission as the documentation seems to imply it should. If it is not, where should I put the code to manage the file upload?

 

Using http://jbzoo.com/doc...element-exampleas a starting point we created the element in:

/media/zoo/applications/jbuniversal/elements/exam/

 

With the structure:

exam/

   tmpl/

      edit.php

   exam.php

   exam.xml

 

A simplified version of the file exam.php is as follows

App::getInstance('zoo')->loader->register('ElementRepeatable', 'elements:repeatable/repeatable.php');

class ElementExam extends ElementRepeatable implements iRepeatSubmittable {

    public function _hasValue($params = array()) {
        $examtype = JString::trim($this->get('examtype'));
        $file = JString::trim($this->get('certificate_pdf'));
        $fileExists = !empty($file) && JFile::exists(JPATH_ROOT . '/' . $file);
        return  !empty($examtype) && $fileExists;
    }
    public function getText() {
        $examtype = $this->get('examtype');
        $exampdf = $this->get('certificate_pdf');
        return $examtype.', ' . $exampdf;
    }

    public function _edit(){
        return $this->_editForm();
    }
    public function _renderSubmission($params = array()) {
        return $this->_editForm($params->get('trusted_mode'));
    }
    public function _editForm($trusted_mode = true) {
        if ($layout = $this->getLayout('edit.php')) {
            return $this->renderLayout($layout,
                array(
                    'trusted_mode' => $trusted_mode,
                    'examTypes' => $this->getExamTypes()
                )
            );
        }
    }

    public function _validateSubmission($value, $params) {

        try {
            $certificate = $this->_validateFileUpload($value->get('certificate_pdf'));
        } catch (Exception $e) {
            $e->getMessage();
            $certificate = false;
        }

        $examtype   = $validator->clean($value->get('examtype'));

        $this->next();

        return compact('examtype', 'certificate');
    }
}

All and any help is appreciated, thanks.

Stuart


  • 0

#2 SmetDenis

SmetDenis

Отправлено 21 August 2015 - 07:12

Hello,

Try to rename method "_validateSubmission" to "validateSubmission"

And you should see image element - media\zoo\elements\image\image.php
You can find there method submissionBeforeSave. It uploads and moves file after validation/

It is calls by system event
$this->app->event->dispatcher->connect('submission:beforesave', array($this, 'submissionBeforeSave'));
2kdd_200x0.png
  • 0
JBZoo v4.0 и новый чудный мир Open Source GPL
Отключайте проверку лицензий как можно скорее!



— Есть два типа людей: Кто еще не делает бекапы и кто уже делает бекапы.


#3 net

net

Отправлено 21 August 2015 - 12:07

Thanks for your reply.

 

I already had tried adding in a validateSubmission method like this:

public function validateSubmission($value, $params)
    {
        // die(var_dump($value));
        $this->log('validateSubmission');
        $this->log($value);

        // connect to submission beforesave event
        $this->app->event->dispatcher->connect('submission:beforesave', array($this, 'submissionBeforeSave'));
        parent::validateSubmission($value, $params);
    }

Which calls the parent method which as far as I can see should then call _validateSubmission, am I right?

 

I've added logging calls to a simple logging method which uses JLog to write to a log file, from this I can tell what is being called, and so far I cannot see either the validateSubmission or the _validateSubmission method being called, which means the submissionBeforeSave method will not be attached to the submission::beforeSave event and so will not be called.

 

Is there something in the setting up of the class or the config xml file that I'm missing that means these methods are not being called?

 

My exam.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<element type="exam" group="JBZoo">
    <name>Exam</name>
    <author>XXX</author>
    <creationDate>Aug 2014</creationDate>
    <copyright>Copyright (C) XXX Ltd</copyright>
    <authorEmail></authorEmail>
    <authorUrl>http://www.xxx.co.uk</authorUrl>
    <version>1.0.0</version>
    <description>Repeatable exam information</description>
    <params>
        <param name="inspectortype"
               type="radio"
               default="routine"
               label="Routine or Annual"
               description="Show the exam types for routine or annual examination types?"
                >

            <option value="routine">Routine</option>
            <option value="annual">Annual</option>
        </param>
        <param name="certificate_directory" type="text" default="images/certificate-pdf/" label="JBZOO_UPLOAD_DIRECTORY"
               description="JBZOO_UPLOAD_DIRECTORY_DESC"/>
        <param name="max_upload_size" type="text" default="2048" label="JBZOO_MAX_UPLOAD_SIZE"
               description="JBZOO_MAX_UPLOAD_SIZE_DESC"/>
        <param name="upload_by_user" type="jbbool" default="0" label="JBZOO_JBIMAGE_UPLOAD_BY_USER"
               description="JBZOO_JBIMAGE_UPLOAD_BY_USER_DESC"/>
        <param name="remove_with_item" type="jbbool" default="0" label="JBZOO_JBIMAGE_REMOVE_WITH_ITEM"
               description="JBZOO_JBIMAGE_REMOVE_WITH_ITEM_DESC"/>
    </params>
</element>

Thanks


  • 0

#4 net

net

Отправлено 23 August 2015 - 16:11

Hello,

 

I cannot get the _validateSubmission or validateSubmission methods to be called during the submission of my element still. I have read all the documentation around this and have studied similar elements (e.g. media\zoo\elements\image\image.php) and I cannot see what I am not doing that they are that prevents the methods being called.

 

I have created a new element in case there was something during the install that was causing the problem. I have used the documentation and examples as guides to create a simple element that provides a single file upload, I ave created many of the methods as stubs to minimise the number of complications, but it has all that is required such that validateSubmission (and therefore as it is repeatable _validateSubmission) should be called.

 

The new element is called certificate and the files are as follows:

 

media/zoo/applications/jbuniversal/elements/certificate/certificate.xml

<?xml version="1.0" encoding="utf-8" ?>
<element type="certificate" group="PlayInspectors">
    <name>Certificate</name>
    <author>Stuart Laverick</author>
    <creationDate>23 August 2015</creationDate>
    <copyright>Copyright (C) WeAreNet</copyright>
    <authorEmail>info@wearenet.co.uk</authorEmail>
    <authorUrl>http://www.wearenet.co.uk/</authorUrl>
    <version>0.1</version>
    
</element>

media/zoo/applications/jbuniversal/elements/certificate/certificate.php

<?php
/**
 * Element to manage uploaded certificates
 *
 * @package     playinspectors
 * @version     0.1
 * @author      WeAreNet http://www.wearenet.co.uk
 * @copyright   Copyright (C) WeAreNet,  All rights reserved.
 * @license     http://jbzoo.com/license-pro.php JBZoo Licence
 * @author      Stuart Laverick
 */

// no direct access
defined('_JEXEC') or die('Restricted access');


// register ElementRepeatable class
App::getInstance('zoo')->loader->register('ElementRepeatable', 'elements:repeatable/repeatable.php');

/**
 * Manage PDF certificates
 *
 * @package playinspectors
 * @author Stuart Laverick
 **/
class ElementCertificate extends ElementRepeatable implements iRepeatSubmittable
{

    /**
     * Output and editing form for each element
     * @return bool | string HTML
     * @author Stuart Laverick
     **/
    protected function _edit($params = array())
    {
        $this->log($params);
        if ($layout = $this->getLayout('_edit.php')) {
            return $this->renderLayout($layout);
        }
        return false;
    }

    /**
     * Output data for each element during item submission
     *
     * @param array $params submission parameters
     * @return bool | string HTML
     * @author Stuart Laverick
     **/
    public function _renderSubmission($params = array())
    {
        $this->log($params);
        return $this->_edit($params);
    }

    /**
     * Validate input data for each element after item submission
     *
     * @return void
     * @author Stuart Laverick
     **/
    public function _validateSubmission($value, $params)
    {
        $this->log($value);
    }

    /**
     * Returns a dictionary of exam types based on the set Inspector Type
     *
     * @return Array
     * @author Stuart Laverick
     **/
    private function getExamTypes()
    {
        if ($this->config->get('inspectortype', 'routine') == 'routine') {
            return array (
                'none' => '- None',
                'operational-candidates-practical-report' => 'Operational Candidates Practical Report',
                'operational-candidates-written-exam' => 'Operational Candidates Written Exam',
                'operational-examiner-report-sheet' => 'Operational Examiner Report Sheet',
                'operational-examiners-master-report' => 'Operational Examiners Master Report',
                'operator-attendant-examiners-marking-sheet' => 'Operator /Attendant Examiners Marking Sheet',
                'operator-attendant-practical-exam' => 'Operator /Attendant Practical Exam',
                'operator-attendant-written-exam' => 'Operator /Attendant Written Exam',
                'routine-candidates-practical-report' => 'Routine Candidates Practical Report',
                'routine-examiner-report-sheet' => 'Routine Examiner Report Sheet',
                'routine-examiners-master-report' => 'Routine Examiners Master Report'
            );
        }

        return array (
                'none'=>'- None',
                'indoor-annual-candidates-exam-paper'=>'Indoor Annual Candidates Exam Paper',
                'indoor-annual-candidates-practical-report'=>'Indoor Annual Candidates Practical Report',
                'indoor-annual-examiners-marking-sheet'=>'Indoor Annual Examiners Marking Sheet',
                'inflatable-annual-candidates-exam-paper'=>'Inflatable Annual Candidates Exam Paper',
                'inflatable-annual-candidates-practical-report'=>'Inflatable Annual Candidates Practical Report',
                'inflatable-annual-examiners-marking-sheet'=>'Inflatable Annual Examiners Marking Sheet',
                'outdoor-annual-candidates-exam-paper'=>'Outdoor Annual Candidates Exam Paper',
                'outdoor-annual-candidates-practical-report'=>'Outdoor Annual Candidates Practical Report',
                'outdoor-annual-examiners-marking-sheet'=>'Outdoor Annual Examiners Marking Sheet'
            );
    }

    /**
     * Element specific logging
     *
     * @return void
     * @author Stuart Laverick
     **/
    private function log($value, $fullTrace = false)
    {
        jimport('joomla.log.log');
        JLog::addLogger(
            array(
                'text_file' => 'exam_certificate.log.php'
            ),
            JLog::ALL,
            array('exam_certificate')
        );
        $trace = debug_backtrace(false);
        if ($fullTrace) {
            $caller = array();
            foreach ($trace as $call) {
                $caller[] = array(
                    'file' => $call['file'],
                    'line' => $call['line'],
                    'class' => $call['class'],
                    'function' => $call['function']
                    );
            }
        } else {
            $caller = isset($trace[1]['class']) ? $trace[1]['class'] . '::' . $trace[1]['function'] : $trace[1]['function'];
        }

        $message = is_array($value) || is_object($value) ? print_r($value, true) : $value;
        $message.= "\n" . $caller;
        JLog::add(JText::_($message), JLog::DEBUG, 'exam_certificate');
    }
} // END class  iRepeatSubmittable

media/zoo/applications/jbuniversal/elements/certificate/tmpl/_edit.php

<?php
// no direct access
defined('_JEXEC') or die('Restricted access');

?>

<div style="margin-bottom: 5px;">
    <span style="float:left;width:90px;">Certificate PDF</span>
    <input type="file"
        id="<?php echo $this->getControlName('certificate_pdf'); ?>"
        name="<?php echo $this->getControlName('certificate_pdf'); ?>"
        title="<?php echo JText::_('Certificate PDF'); ?>" />
    <?php if (isset($link) && $link): ?>
        <a href="<?php echo $link; ?>">Certificate</a>
    <?php endif; ?>
</div>

I would be grateful if you could look at this and provide any clues as to where I am going wrong. I have not implemented validateSubmission on the class as this should be available on the abstract parent ElementRepeatable and this should then call _validateSubmission, but it obviously is not.

 

The class as it stands is just set up with logging calls so I can observe what happens and what gets called. The method _edit is called and writes to the log, but so far none of the other logging calls are run.

 

If you require access to the site this can be arranged if it will help to resolve this issue. We have paid for a pro licence so are hoping for some support on this matter.

 

Thanks


  • 0

#5 SmetDenis

SmetDenis

Отправлено 24 August 2015 - 12:45

Hello,

Sorry, it seems that I didn't understand you at first.
When you edit item in a Joomla CP, validateSubmission will not be used.

This function is used only for submission form.

You can add strong validation in function "bindData" (just overload it).

public function bindData($data)
{
    //
    // some actions
    //

    return parent::bindData($data);
}

  • 0
JBZoo v4.0 и новый чудный мир Open Source GPL
Отключайте проверку лицензий как можно скорее!



— Есть два типа людей: Кто еще не делает бекапы и кто уже делает бекапы.





Click to return to top of page in style!