Get checkbox value in errors

Hi,
i have this code from forum:

add_filter(
	'hivepress/v1/forms/listing_update/errors',
	function( $errors, $form ) {
		$listing = $form->get_model();

		if ( $listing && ! $listing->get_image__id() ) {
			$errors[] = 'Please upload at least one image.';
		}

		return $errors;
	},
	100,
	2
);

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form ) {
		$form['fields']['images']['statuses']['optional'] = null;

		return $form;
	},
	1000
);

i want to add a checkbox validation but the $primary_checked is always null regardless if I tick the check boxor not

add_filter(
    'hivepress/v1/forms/listing_update/errors',
    function( $errors, $form ) {
        $listing = $form->get_model();

        if ( $listing && ! $listing->get_image__id() ) {
            $errors[] = 'Please upload at least one image.';
        }
                
		
		$primary_checked = $listing->get_primarychecked();
		if (!$primary_checked) {
			$errors[] = 'You need to choose at least one number';			
		}
                
        return $errors;
    },
    1000,
    2
);

i can’t use required on the checkbox attribute because i have several checboxes and at least one must be checked so I need to have the validation in errors hook.
And i can’t use checkboxes (plural).

Thank you for waiting. Please try this PHP code snippet instead.

add_filter(
    'hivepress/v1/forms/listing_update/errors',
    function( $errors, $form ) {
        $listing = $form->get_model();

        if ( $listing && ! $listing->get_image__id() ) {
            $errors[] = 'Please upload at least one image.';
        }
		
		// Get form values.
		$values = $form->get_values();
		
		if (!hivepress()->helper->get_array_value($values, 'primarychecked')) {
			$errors[] = 'You need to choose at least one number';			
		}
                
        return $errors;
    },
    1000,
    2
);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.