rick Posted January 14, 2022 Share Posted January 14, 2022 Howdy y'all, I have created a set of fields via the api, added them to a fieldgroup, then assigned that fieldgroup to a template. One field is FieldtypeImage. When I attempt to create a form using $page->getInputFields() the following error is displayed, New page '//' must be saved before files can be accessed from it This only happens when fields relating to files are present. All the other field types are created correctly on a form. How can I 'bypass' this 'must be saved' issue? Link to comment Share on other sites More sharing options...
Zeka Posted January 15, 2022 Share Posted January 15, 2022 ..... Link to comment Share on other sites More sharing options...
rick Posted January 16, 2022 Author Share Posted January 16, 2022 14 hours ago, Zeka said: ..... Did I miss something? ? Link to comment Share on other sites More sharing options...
Zeka Posted January 16, 2022 Share Posted January 16, 2022 Hi @rick As error says, a page should be saved before adding to or accessing files from it. Take a look at this thread Probably you have to check for $page->isNew() and $page->id or just save a page before doing somting with files field like $uploadpage = new Page(); $uploadpage->template = "upload-entry"; $uploadpage->title = date("d-m-Y H:i:s") . " - " . uniqid(); $uploadpage->save(); //save page before adding files $uploadpage->images->add($upload_path . $file); // save uploaded files to new page $uploadpage->save(); I don't know the context of your code as the code itself, so it quite hard to say someting more. 2 Link to comment Share on other sites More sharing options...
rick Posted January 16, 2022 Author Share Posted January 16, 2022 6 hours ago, Zeka said: I don't know the context of your code as the code itself, so it quite hard to say someting more. Hi @Zeka, Thanks for the help. I can check whether the page is present or not. The issue is imho one of consistency. Let me elaborate... Only the 'file' field types have this 'gotcha'. For example, in PHP the procedure is as follows: Create a form User completes the form and submits (including any uploaded files) Data is then sanitized, uploaded tmp files are moved, and data written to the database or The user cancels the operation Data and form are destroyed The PW way: Create and save some dummy page Create a form User completes the form and submits (including any upload files) Data is then saved and files are moved to that page asset or The user cancels the operation (No cancel function, so navigate away) Data and form are destroyed *Page is now orphaned* You get the notice that a page with that name already exists when attempting to create another page. Interesting is that page doesn't have a title field (which is required). Using any other field type, I can perform the following procedure within my process module: Create a form User completes the form and submits Page is saved with user data and user is redirected to previous page The steps are: 1) Create blank new Page. 2) Assign Parent. 3) Assign Template. 4) Build form from Page->getInputFields() or User cancels operation Data and form are discarded and user is redirected to previous page The problem is pre-defining a page just so an empty input field has a reference before any data is provided. This only occurs when there are 'file' fields present in the template. Link to comment Share on other sites More sharing options...
kongondo Posted January 17, 2022 Share Posted January 17, 2022 (edited) 7 hours ago, rick said: Only the 'file' field types have this 'gotcha'. For example, in PHP the procedure is as follows: Create a form User completes the form and submits (including any uploaded files) Data is then sanitized, uploaded tmp files are moved, and data written to the database or The user cancels the operation Data and form are destroyed The PW way: Create and save some dummy page Create a form User completes the form and submits (including any upload files) I don't think this is a fair comparison. You can easily create a form, have it submitted (including uploads -> to some tmp folder), create a page if happy with the uploads, save it, move your uploads to the file/image fields, then save again. No orphan pages. I believe your issue is because you are calling $page->getInputfields(). This calls all the fields on that page's template, including your image field. Being a new page, you cannot add (as has been pointed out) to that field unless the page is saved. One reason is that ProcessWire stores a page's files in the page's assets folder (site/assets/files). E.g., site/assets/files/1234/some_image.jpg. Without saving the page first, there is no folder 1234, hence there is no destination for your image. Unless I've forgotten something, this is one of the explanations. Above means that to get around this, create your own form with your own file upload field instead of relying on $page->getInputfields(). This way you can handle the uploads yourself and only create a new page when satisfied with the sanitisation and validation. Alternatively, call $page->getInputfields() with the $fieldName argument (in your case an array) that exclude your image field. I haven't tested this approach, but I think it should work. Edited January 17, 2022 by kongondo 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now