validate::unique

validate::unique

rrzavaletarrzavaleta Posts: 78Questions: 52Answers: 2
edited January 2015 in Editor

II can not validate that the field is required to use the Validate :: unique

        Field::inst( 'ING_ASEG_BANCO.CUENTA_BANCO')->validator( 'Validate::unique', array(
                                                    'required'=>true,
                                                    'message'=>'duplicado',)),

works but allows me to save empty fields .
the ING_ASEG_BANCO.CUENTA_BANCO field is named so because it is part of a table that does a join.

$data =Editor::inst( $db, 'ING_ASEG_BANCO' )
    ->pkey( 'ING_ASEG_BANCO.ID')
    ->fields(           
        Field::inst( 'ING_ASEG_BANCO.ID'),
        Field::inst( 'ING_ASEG_BANCO.ID_ASEGURADORA'),
        Field::inst( 'ING_ASEG_BANCO.ID_BANCO') ->validator( 'Validate::notEmpty')
                                                ->getFormatter( 'Format::CapitaliseFirstLetter' )
                                                ->setFormatter( 'Format::CapitaliseFirstLetter' ),
        Field::inst( 'ING_ASEG_BANCO.NUM_SUCURSAL')->validator( 'Validate::numeric',array('required'=>true) )                            
                                                   ->getFormatter( 'Format::CapitaliseFirstLetter' )
                                                   ->setFormatter( 'Format::CapitaliseFirstLetter' ),
        
        Field::inst( 'ING_ASEG_BANCO.NOM_SUCURSAL') ->validator( 'Validate::notEmpty')
                                                    ->getFormatter( 'Format::CapitaliseFirstLetter' )
                                                    ->setFormatter( 'Format::CapitaliseFirstLetter' ),
        Field::inst( 'ING_ASEG_BANCO.CUENTA_BANCO')->validator( 'Validate::unique', array(
                                                    'required'=>true,
                                                    'message'=>'duplicado',)),
                                                  // ->getFormatter( 'Format::CapitaliseFirstLetter' )
                                                   //->setFormatter( 'Format::CapitaliseFirstLetter' ),
        Field::inst( 'ING_ASEG_BANCO.CLABE')->validator( 'Validate::minMaxLen', array(
                                                        'required'=>true,
                                                        'min' => 18,
                                                        'max' => 18,
                                                        'message' => 'Cadena permitida de 18 caracteres',                                                       
                                                        ) ),
        Field::inst( 'ING_ASEG_BANCO.F_MODIFICACION')->set( false )
            ->getFormatter( 'Format::date_sql_to_format', 'D, jS F Y' ),    
        
        Field::inst( 'ING_BANCO.NOM_BANCO' )
          )
    ->leftJoin( 'ING_BANCO', 'ING_BANCO.ID_BANCO', '=', 'ING_ASEG_BANCO.ID_BANCO' )
    ->process( $_POST )
    ->data();
    
    if ( ! isset($_POST['action']) ) {
    // Get a list of sites for the `select` list
    $data['ING_BANCO'] = $db
        ->selectDistinct( 'ING_BANCO', 'ID_BANCO as value, NOM_BANCO as label' )
        ->fetchAll();
                            }


echo json_encode( $data );

I just need to validate that the field is required and not allow saving the nullo field

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Hi,

    Thanks for letting me know about this - there is a bug in the unqiue validation method that is causing this problem. In the Validate.php file if you locate the unique method (it should be right at the end of the file), immediately after the $cfg variable is defined, could you add the following please:

            $common = Validate::_common( $val, $cfg );
            if ( $common !== null ) {
                return $common;
            }
    

    That should address this problem.

    The fix will be in the next 1.4 beta release which is due out soon.

    Regards,
    Allan

  • rrzavaletarrzavaleta Posts: 78Questions: 52Answers: 2

    Hi allan .
    I worked your only solution that relocate . in the end I stayed follows

    // If doing an edit, then we need to also discount the current row,
        // since it is of course already validly unique
        if ( $host['action'] === 'edit' ) {
            $query->where( $editor->pkey(), $host['id'], '!=' ); 
        }
        //add
        $common = Validate::_common( $val, $cfg );
            if ( $common !== null ) {
            return $common;
                }
    
             //end add
        $res = $query->exec();
    
        return $res->count() === 0 ?
            true :
            $cfg['message'];
    

    thx

This discussion has been closed.