Set at least one field as required

Set at least one field as required

TocafundoTocafundo Posts: 3Questions: 1Answers: 0

I have two fields and I want that at least one of then be required. Just one can be empty, not both. Can I do that?

Answers

  • rf1234rf1234 Posts: 2,983Questions: 87Answers: 421

    Yes, you can. If you want to know how please let us know what you are using and share your code. Many thanks.

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Assuming you are using the PHP libraries for Editor, a global validator can be used for what you are looking for. The NodeJS and .NET libraries have similar options.

    Allan

  • TocafundoTocafundo Posts: 3Questions: 1Answers: 0

    Yes, I am using PHP. This is my code and the two fields I want to do.

    editor::inst( $db, 'cliente', 'id_cliente' ) //id_cliente
        ->fields(
                
            Field::inst( 'cliente.cpf' )
                ->validator( 'Validate::numeric', array("message" => "Permitido informar somente números." ))
                ->validator( 'Validate::maxLen', array(
                                            'min' => 11,
                                            'max' => 11,
                                            'message' => 'Permitido informar apenas números com 11 caracteres.'
                    ))      
                ->validator( 'Validate::unique', array("message" => "CPF anteriormente cadastrado." )),
            
            Field::inst( 'cliente.nome_mae' )
                ->validator( 'Validate::notEmpty', array("message" => "Campo de preenchimento obrigatório." ))
                ->validator( 'Validate::maxLen', array(
                                            'max' => 255,
                                            'message' => 'Permitido informar no máximo 255 caracteres.'
                    ))
            )       
    
  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Allan recommended using a global validator.
    https://editor.datatables.net/manual/php/validation#Global-validators

    You would provide it with a function to determine whether both fields are empty.

  • rf1234rf1234 Posts: 2,983Questions: 87Answers: 421

    Here is another example of a global validator. You can do all sorts of things with it ...

    ->validator( function ( $editor, $editorAction, $data ) {
        if ( $editorAction === Editor::ACTION_CREATE || $editorAction === Editor::ACTION_EDIT ) {
            if ( $editorAction === Editor::ACTION_CREATE ) {
                $action = 'create';
            } else {
                $action = 'edit';
            } //no return statement if validation passes
            foreach ( $data['data'] as $pkey => $values ) {
                return validateCashFlowDates($values, $action);
            }
        }
    } )
    
    function validateCashFlowDates(&$values, &$action) {         
        if ( $values["cashflow"]["payment_date"]  <= '' &&
             $values["cashflow"]["interest_date"] <= ''    ) {
            return 'Campo de preenchimento obrigatório. ';
        } 
        //global validators do not return true ... for whatever reason
        return null;    
    }
    

    Saudações ao Brasil

  • TocafundoTocafundo Posts: 3Questions: 1Answers: 0

    Thank you all from Brazil! I will get it.

This discussion has been closed.