How can I set 2 validations for one field?

How can I set 2 validations for one field?

bendsitobendsito Posts: 6Questions: 1Answers: 0

I want to validate that the input is numeric AND not empty (the user is required to enter a number in this particular field).

I have tried following codes, but they all bring up an Ajax error:

Field::inst( 'number' )
->validator( 'Validate::numeric' ),
->validator( 'Validate::required' ),
(...)

Field::inst( 'number' )
->validator( 'Validate::numeric', array('required'=>true) ),
(...)

Could anybody please give me some clue on how this can be done?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,734Questions: 1Answers: 10,110 Site admin
    Answer ✓

    Currently only a single validator can be used for a field at a time, although Editor 1.4 will change that...

    As such, the second option you specify is the way to do this at the moment. You note that it gives an error - what is the Ajax response? Likely there is an error message in it.

    Allan

  • bendsitobendsito Posts: 6Questions: 1Answers: 0

    Ok, I see...

    The error I get is this; http://datatables.net/manual/tech-notes/7

    I had to remove one of the two parameters.

    Thanks for the info :-)

  • bendsitobendsito Posts: 6Questions: 1Answers: 0
    edited August 2014

    I just tried the second option again, and curiously I don't get that error anymore :o

    But it only validates the "required" condition, it doesn't complain when the input is non-numeric (despite of the parameter 'Validate::numeric'). Maybe I am doing something wrong...

  • allanallan Posts: 61,734Questions: 1Answers: 10,110 Site admin

    I've just tried this in my examples:

            Field::inst( 'salary' )->validator( 'Validate::numeric', array('required'=>true) ),
    

    And it appears to correctly validate as a number and is a required field.

    Something is going wrong somewhere, but I think I'd need to be able to see your PHP and Javascript to understand what exactly. Can you link me to the page and also show the PHP?

    Allan

  • bendsitobendsito Posts: 6Questions: 1Answers: 0
    edited August 2014

    The page is http://bendsito.net/2black/index.html

    And the PHP is the following:

    <?php
    
    // DataTables PHP library
    include( "DataTables.php" );
    
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Join,
        DataTables\Editor\Validate;
    
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'socios', 'id' )
        ->fields(
            Field::inst( 'n_socio' )
                ->validator( 'Validate::required', array('required'=>true) ),
            Field::inst( 'nombre' )
                ->validator( 'Validate::required' ),
            Field::inst( 'apellidos' )
                ->validator( 'Validate::required' ),
            Field::inst( 'NIF' )
                ->validator( 'Validate::required' ),
            Field::inst( 'fecha_nacimiento' )
                ->validator( 'Validate::required', 'Validate::dateFormat', array(
                    "format"  => Format::DATE_ISO_8601,
                    "message" => "Introduce una fecha en el formato aaaa-mm-dd"
                ) )
                ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
                ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 ),
            Field::inst( 'direccion' ),
            Field::inst( 'municipio' ),
            Field::inst( 'email' ),
            Field::inst( 'telefono' ),
            Field::inst( 'fecha_inscripcion' )
                ->validator( 'Validate::required', 'Validate::dateFormat', array(
                    "format"  => Format::DATE_ISO_8601,
                    "message" => "Introduce una fecha en el formato aaaa-mm-dd"
                ) )
                ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
                ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 )
        )
        ->process( $_POST )
        ->json();
    

    The field 'n_socio' is the one I'm having trouble with this double validation.

  • allanallan Posts: 61,734Questions: 1Answers: 10,110 Site admin
    Answer ✓

    The validation applied to that field at the moment is:

    ->validator( 'Validate::required', array('required'=>true) ),

    so it is required, required :-)

    Do you not want:

    ->validator( 'Validate::numeric', array('required'=>true) ),
    

    ?

    Allan

  • bendsitobendsito Posts: 6Questions: 1Answers: 0

    After so many changes and testing, I'm not sure why I did that :D

    Your code is exactly what I want, and it works now :-) Thanks a lot!

  • allanallan Posts: 61,734Questions: 1Answers: 10,110 Site admin

    Good to hear that works!

    Allan

  • AlexandreDelachapelleAlexandreDelachapelle Posts: 9Questions: 3Answers: 0

    Thanks Allan, you help me too !

This discussion has been closed.