0

My Image service function throws an exception and sends it to the Craft logger that the extension is invalid. But my entry is being saved even though the field is set to only allow images. I am developing an action inside my plugin to save an entry with images. It gives me no entry errors, and continues to run the code and saves the entry.

I am trying to get errors to display in my templare when it gets the errors instead of it redirecting to the confirmation page. The exception message I get in my logs is the message I want to display to the client submitting the form to inform them of why the form cannot be submitted. A validation.

The image field is required and set to only allow images.

Action

public function actionPostData() {
        $arrReturn['status'] = "OK";
        $this->requirePostRequest();
        $arrPostData = craft()->request->getParam('fields');
        $nSectionId = 18;

        $entry = new EntryModel();

        $entry->sectionId = $nSectionId; // section id for section in use.
        $entry->authorId = 1;
        $entry->getContent()->title = $arrPostData['FirstName']." " .$arrPostData['LastName'];
        foreach($arrPostData as $key=>$dataField) {
            $strKey = "prefix_".$key;
            $entry->getContent()->$strKey = $dataField;  

        }

        $arrImages = $_FILES;   
        if(count($arrImages) > 0 ){
            $arrImages = craft()->pluginName->saveImages($arrPostData['FirstName']." " .$arrPostData['LastName'],$arrImages);
            if($arrImages) {
                $arrPostData['images'] = $arrImages['images'];
                $entry->getContent()->prefix_Images = $arrImages['bilder'];
            }
        }

        // Save Entry
            if(craft()->entries->saveEntry($entry))
        {
          //  return true;
        }
        else
        {
            $error = 'Encountered the following validation errors when trying to save entry with the ID "'.$entry->id.'":';

            foreach ($entry->getAllErrors() as $attributeError)
            {
                $error .= "\n - {$attributeError}";
            }


        }
        $strEntryId = craft()->request->getParam('entryId');
        $objEntry = craft()->entries->getEntryById($strEntryId);
        $objConfirmPage = $objEntry->confirmationPage->first();

        $this->redirect($objConfirmPage->url);
    }

ImageService

 public function saveImages($strFolderName,$projectData) {

        $strFolderName = str_replace(' ', '_',$strFolderName);

        $arrFileIds = [];
        $arrFiles = [];

        $topImageFolder = false;
        $folderCriteria = new FolderCriteriaModel();


        $topFolderCriteria = new FolderCriteriaModel();
        $topFolderCriteria->setAttribute("name", "FOLDER NAME");
        $topFolder = craft()->assets->findFolder($topFolderCriteria);
        $parentFolder = craft()->assets->findFolder($folderCriteria);
        craft()->assets->createFolder($topFolder->id,$strFolderName);
        $folderCriteria->setAttribute("parentId",$topFolder->id);
        $folderCriteria->setAttribute("name",$strFolderName);
        $parentFolder = craft()->assets->findFolder($folderCriteria);


        foreach($projectData as $key=>$image) {

            if(is_object($parentFolder)){
                foreach($image['tmp_name'] as $key2=>$fileName ){


                    if(strlen($fileName) > 0 ){
                         $nFolderId = $parentFolder->id;

                        try {
                            $response = craft()->assets->insertFileByLocalPath(
                                        $fileName,
                                        $image['name'][$key2],
                                        $nFolderId,
                                        AssetConflictResolution::Replace
                            );
                        }
                        catch (Exception $e) {
                            echo 'Caught exception: ',  $e->getMessage(), "\n";
                            PluginName::log($e->getMessage(), LogLevel::Error, true);
                        }

                        if (isset($response) && $response->isSuccess()) {
                            $arrFileIds['images'][] = $response->getDataItem('fileId');
                            $arrFiles[] = $response->getDataItem('filename');
                        }
                    }

                }               



            }


        }

        return $arrFileIds;
    }
| improve this question | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.