Hi everybody!I'm having hard time with a weird behaviour of fileinput. This
is my form: 

/namespace Frontend\Form;

use NW\Form\Form;
use Zend\InputFilter;
use Zend\Form\Element;
use Zend\ServiceManager\ServiceManager;

use Zend\ServiceManager\ServiceManagerAwareInterface;

class EnrollStructure extends Form implements ServiceManagerAwareInterface
{
        protected $sm;
        
        public function __construct($name=null) {
        
            parent::__construct("frmEnrollStructure");
            $this->setAttribute("action", "/registrazione_struttura/submit")
                ->setAttribute('method', 'post')
                ->setAttribute("id", "iscrizione_struttura")
                ->setAttribute("class", "form fullpage");
            $this->addInputFilter();
        }
        
        public function init()
        {
            $structureFs = $this->sm->get('Structure\Form\Fieldsets\Structure');
            $structureFs->setUseAsBaseFieldset(true);
        
            $structureFs->remove("id")
                ->remove("creationTime")
                ->remove("latLon");
        
            $file = new Element\File("images");
            $file->setAttribute('multiple', true);
        
            $this->add($structureFs)->add($file);
        
            $this->add(array(
                'name' => 'submit',
                'attributes' => array(
                    'type' => 'submit',
                    'value' => 'Iscriviti',
                    'id' => 'sbmtEnrollStructure',
                    'class' => 'submit_btn'
                ),
            ));
        
            $this->setValidationGroup(
                array(
                'structure' =>
                    array(
                        'companyname',
                        'vatNumber',
                        'addressStreet',
                        'addressZip',
                        'addressCity',
                        'addressRegion',
                        'fax',
                        'publicPhone',
                        'publicEmail',
                        'website',
                        'status',
                        'ownerNotes',
                        'category',
                        'subcategory',
                        "facilities",
                        "agreeOnPolicy",
                        "agreeOnPrivacy",
                        "subscribeNewsletter",
                        "contact" => array("name", "surname", "email", "role",
"phone"),
                    ),
                    "images"
            ));
        
        }
        
        /**
         * Set service manager
         *
         * @param ServiceManager $serviceManager
         */
        public function setServiceManager(ServiceManager $serviceManager)
        {
            $this->sm = $serviceManager;
        }
        
        public function addInputFilter()
        {
            $inputFilter = new InputFilter\InputFilter();
            // File Input
            $fileInput = new InputFilter\FileInput('images');
            $fileInput->setRequired(true);
        
            $fileInput->getValidatorChain()
                ->attachByName('filesize',      array('max' => "2MB"))
                ->attachByName('filemimetype',  array('mimeType' =>
'image/png,image/x-png,image/jpg,image/jpeg'))
                ->attachByName('fileimagesize', array('maxWidth' => 2048,
'maxHeight' => 2048));
        
            $inputFilter->add($fileInput);
        
            $this->setInputFilter($inputFilter);
        }

}/


Basically, I mainly use a fieldset which contains most of the data I request
to the user, plus a File input field.

This is the Structure Fieldset: (most important parts..)

use Zend\Form\Element;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;

use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Validator\Identical;
use Zend\Validator\NotEmpty;
use Zend\Validator\Regex;
use Zend\Validator\StringLength;


/class Structure extends Fieldset implements InputFilterProviderInterface,   
ServiceManagerAwareInterface
{
        protected $sm;
        
        public function __construct()
        {
            parent::__construct('structure');
        }
        
        public function init()
        {
            $this->setHydrator(new
DoctrineHydrator($this->_entityManager(),'Structure\Entity\Structure'));
           
$this->setObject($this->sm->getServiceLocator()->get("Structure_Structure"));
            $id = new Element\Hidden("id");
        
            $name = new Element\Text("companyname");
            $name->setLabel("Ragione Sociale");
        
               ...........
        }
        
        public function getInputFilterSpecification()
        {
            return array
            (
                "id" => array(
                    "required" => false,
                ),
                "companyname" => array(
                    "required" => true,
                    "validators" => array(
                        array('name' => "NotEmpty", 'options' => 
array("messages"
=> array( NotEmpty::IS_EMPTY => "Inserire la ragione sociale")))
                    ),
                ),
              
              // etc etc etc
         }

}/


and this my controller action:


/public function submitAction()
{
    try {
        $this->layout("layout/json");

        $form = $this->getForm('Frontend\Form\EnrollStructure');
        //$form->addInputFilter();
        $structure = $this->getServiceLocator()->get("Structure_Structure");

        $viewModel = new ViewModel();
        $request = $this->getRequest();

        if ($request->isPost())
        {
            $post = array_merge_recursive
            (
                $request->getPost()->toArray(),
                $request->getFiles()->toArray()
            );

            $form->setData($post);

            if ($form->isValid())
            {
                $structure = $form->getObject();
                $contact = $structure->getContact();
               
$this->getServiceLocator()->get('Structure_ContactService')->save($contact);
                $files = $request->getFiles()->toArray();

                if(isset($files['images']))
                {
                    $count = 3;
                    foreach($files['images'] as $pos => $file)
                    {
                        $fpath =
$this->getServiceLocator()->get('RdnUpload\Container')->upload($file);
                        if(!empty($fpath))
                        {
                            if(--$count ==0) break;
                            $asset =
$this->getServiceLocator()->get("Application_AssetService")->fromDisk($fpath,
$file['name']);
                           
$this->getServiceLocator()->get("Application_AssetService")->save($asset);
                            $structure->addImage($asset);
                        }
                    }
                }

               
$this->getServiceLocator()->get('Structure_StructureService')->save($structure);
                $retCode = RetCode::success(array("iscrizione_struttura!" =>
array("form_submit_successfull")), true);
            }
            else
            {

                $messages = $form->getMessages();
                if(empty($messages))
                    $retCode = RetCode::error(array("iscrizione_struttura"
=> array("need_at_least_one_file" => "missing file")), true);
                else
                    $retCode = RetCode::error(array("iscrizione_struttura"
=> $messages), true);
            }


            $viewModel->setVariable("retcode", $retCode);
            return $viewModel;
        }
    } catch(Exception $e)
    {
        throw $e;
    }
}/

The strange thing is that if i remove from the field "images" the "multiple"
attribute everything works fine, causing the form not to validate (if I do
not choose a file to be uploaded) and i get the message:

/
[images] => Array
    (
        [fileUploadFileErrorFileNotFound] => File was not found
    )/



But, if i set the attribute multiple, and the user does not upload a file i
get no error, but the form gets invalidated and I get no message from the
form "getMessage()" (that's why I do this:

/
$messages = $form->getMessages();
if(empty($messages))
    $retCode = RetCode::error(array("iscrizione_struttura" =>
array("need_at_least_one_file" => "missing file")), true);
else
    $retCode = RetCode::error(array("iscrizione_struttura" => $messages),
true);/

on my controller action)


I'm banging my head against the wall for 3 days, I've posted this to
stackoverflow too.. any help would be greatly appreciated!
Thanks and have a nice monday
A.







--
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/nputFilter-setRequired-not-working-for-html5-multiple-tp4661934.html
Sent from the Zend Framework mailing list archive at Nabble.com.

-- 
List: fw-general@lists.zend.com
Info: http://framework.zend.com/archives
Unsubscribe: fw-general-unsubscr...@lists.zend.com


Reply via email to