Upload validation dependend on more data - how to setup?

Upload validation dependend on more data - how to setup?

tomekkietomekkie Posts: 30Questions: 6Answers: 1
edited December 2015 in Editor

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

Answers

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin

    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

  • tomekkietomekkie Posts: 30Questions: 6Answers: 1
    edited December 2015

    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
    ...

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin

    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 the upload field type - it would just be a bit messy).

    Allan

  • tomekkietomekkie Posts: 30Questions: 6Answers: 1

    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.

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Answer ✓

    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, a FormData object, which you can add data to submit to the server. For example:

    {
      name: 'file',
      label: 'File',
      type: 'upload',
      ajaxData: function ( fd ) {
        fd.append( 'measurement', editor.field('measurement').val() );
      }
    }
    

    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:

            data.append( 'action', 'upload' );
            data.append( 'uploadField', conf.name );
            data.append( 'upload', files[ counter ] );
    

    and simply add the following after those three lines:

            if ( conf.ajaxData ) {
                conf.ajaxData( data );
            }
    

    Thanks for bringing this up.

    Allan

  • tomekkietomekkie Posts: 30Questions: 6Answers: 1

    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.

This discussion has been closed.