Standalone Bubble editing - On submitting edit, PHP code is running custom validator on other field

Standalone Bubble editing - On submitting edit, PHP code is running custom validator on other field

VK2AZVK2AZ Posts: 4Questions: 1Answers: 0

I am using the editor in standalone mode.
Everything works ok, but when I include a custom validator in any field as below, on submission the validator always seem to be run for that field even though I am editing a different field.

        Field::inst( 'grid' )
            ->validator( Validate::notEmpty() )
            ->validator( function ( $val, $data, $field, $host ) {
                return (validate_locator( $val )) ? true : 'Enter a 6 digit grid square. No hyphens. E.G QF56IF. 6 characters max';
            })

            ->setFormatter( function ( $val, $data ) {
                return strtoupper($val);
            }),

Answers

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin

    Yes - the validator will always run, as that is the only way to enforce a field being required. What you need to do is allow the field to be valid if it is not set:

    if ($val === null) {
      return true; // allow no data
    }
    ...
    

    Allan

  • VK2AZVK2AZ Posts: 4Questions: 1Answers: 0

    Thanks Allan,
    That works.
    Thank you so much.

    Hilary

This discussion has been closed.