Upload validation dependend on more data - how to setup?
Upload validation dependend on more data - how to setup?
This is a fragment of my editor setup:
Field::inst( 'measurement' ),
Field::inst( 'image' )
->setFormatter( 'Format::nullEmpty' )
->upload( Upload::inst( function ( $file, $id ) use( $uploadDir ) {
move_uploaded_file( $file['tmp_name'], $uploadDir.$file['name'] );
return $id;
} )
->db( 'files', 'id', array(
'filename' => Upload::DB_FILE_NAME,
'filesize' => Upload::DB_FILE_SIZE
) )
->validator( function ( $file ) { return
imagesx(imagecreatefromjpeg($file['tmp_name'])) <= 1000 ? "Files must be wider than 1000px" : null;
} )
->allowedExtensions( array( 'png', 'jpg', 'gif' ), "Please upload an image" )
)
How to access the values entered in the previous 'measurement' field, making the allowed image size dependent on it?
,
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Hi,
The validation method actually has three parameters passed into it. The second contains all of the data submitted to the server, which is where you could access the
measurement
field.Allan
Looks like I got lost somewhere, because when I put more arguments:
->validator( function ( $file, $data, $opts ) {
instead of:
->validator( function ( $file ) {
I am getting "Missing argument 2 for {closure}()", "Missing argument 3 for {closure}()" warnings
...
Oh - sorry - this is in the file upload validator. It does indeed only have one parameter - sorry.
The upload is async from the rest of the form, so the rest of the form information isn't submitted at the same time. There is in effect no way to do what you are looking for at the moment. It would require additional information to be submitted to the server (which actually, you could do using the
ajax
option of theupload
field type - it would just be a bit messy).Allan
Thanks, @allan.
Could you give some hint how to make it?
How to add that into the upload data and retrieve for use in validator?
I couln't find any example close enough.
So it can be done in the current release of Editor, but it turns out that its a bit messy. I've just made a little change which will be in Editor 1.5.4 (which I hope to release later today, possibly tomorrow depending on how things go!) which will make it much easier.
In 1.5.4 you will be able to add additional data to the upload Ajax request using a new
ajaxData
option - that is a function which takes a single parameter, aFormData
object, which you can add data to submit to the server. For example:Then at the server, in your validation function you can simply do
$_POST['measurement']
to get the value.If you want to make the change for this and use it immediately, search the Editor code for:
and simply add the following after those three lines:
Thanks for bringing this up.
Allan
Thanks a lot.
It seems a good choice to wait for the 1.5.4. release with it.
I can also check my files on preCreate event and put the results into table at the moment.