public function validate($validator)
    {
        // Throw an error when there are no tickets selected
        if (empty($this->value)) {
            $validator->validationError($this->name, _t(
                'TicketsField.VALIDATION_EMPTY',
                'Select at least one ticket'
            ), 'validation');

            return false;
        }

        // Get the availability
        $available = $this->getForm()->event->getAvailability();
        // get the sum of selected tickets
        $ticketCount = array_sum(array_map(function ($item) {
            return $item['Amount'];
        }, $this->value));

        // If the sum of tickets is 0 trow the same error as empty
        if ($ticketCount === 0) {
            $validator->validationError($this->name, _t(
                'TicketsField.VALIDATION_EMPTY',
                'Select at least one ticket'
            ), 'validation');

            return false;
        }

        // Throw an error when there are more tickets selected than available
        if ($ticketCount > $available) {
            $validator->validationError($this->name, _t(
                'TicketsField.VALIDATION_TO_MUCH',
                'There are {ticketCount} tickets left',
                null,
                array(
                    'ticketCount' => $available
                )
            ), 'validation');

            return false;
        }

        return false;
    }
public function validate($validator)
    {
        // Throw an error when there are no tickets selected
        if (empty($this->value)) {
            $validator->validationError($this->name, _t(
                'TicketsField.VALIDATION_EMPTY',
                'Select at least one ticket'
            ), 'validation');

            return false;
        }

        // Get the availability
        $available = $this->getForm()->event->getAvailability();
        // get the sum of selected tickets
        $ticketCount = array_sum(array_map(function ($item) {
            return $item['Amount'];
        }, $this->value));

        // If the sum of tickets is 0 trow the same error as empty
        if ($ticketCount === 0) {
            $validator->validationError($this->name, _t(
                'TicketsField.VALIDATION_EMPTY',
                'Select at least one ticket'
            ), 'validation');

            return false;
        }

        // Throw an error when there are more tickets selected than available
        if ($ticketCount > $available) {
            $validator->validationError($this->name, _t(
                'TicketsField.VALIDATION_TO_MUCH',
                'There are {ticketCount} tickets left',
                null,
                array(
                    'ticketCount' => $available
                )
            ), 'validation');

            return false;
        }

        return false;
    }
public function validate ( $ validator ) { // Throw an error when there are no tickets selected if ( empty ( $ this -> value ) ) { $ validator -> validationError ( $ this -> name , _t ( 'TicketsField.VALIDATION_EMPTY' , 'Select at least one ticket' ) , 'validation' ) ; return false ; } // Get the availability $ available = $ this -> getForm ( ) -> event -> getAvailability ( ) ; // get the sum of selected tickets $ ticketCount = array_sum ( array_map ( function ( $ item ) { return $ item [ 'Amount' ] ; } , $ this -> value ) ) ; // If the sum of tickets is 0 trow the same error as empty if ( $ ticketCount === 0 ) { $ validator -> validationError ( $ this -> name , _t ( 'TicketsField.VALIDATION_EMPTY' , 'Select at least one ticket' ) , 'validation' ) ; return false ; } // Throw an error when there are more tickets selected than available if ( $ ticketCount > $ available ) { $ validator -> validationError ( $ this -> name , _t ( 'TicketsField.VALIDATION_TO_MUCH' , 'There are {ticketCount} tickets left' , null , array ( 'ticketCount' => $ available ) ) , 'validation' ) ; return false ; } return false ; }
Make sure a ticket is selected and that the selected amount is available

@param Validator $validator

@return bool
Make sure a ticket is selected and that the selected amount is available