Set a custom error message on server side

Set a custom error message on server side

INTONEINTONE Posts: 153Questions: 58Answers: 6

I have looked around on the forum, but I have not found a current example. How can I set the error property from the server side and have it display on the client side?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,776Questions: 1Answers: 10,112 Site admin

    Is this using the pre-built Editor libraries? This is the PHP documentation for validators.

    You would do something like:

    Field::inst( 'first_name' )
        ->validator( 'Validate::notEmpty', array(
            "message"  => "A first name is required"
        ) )
    

    Allan

  • INTONEINTONE Posts: 153Questions: 58Answers: 6
    edited April 2015

    Thanks for your response, buit I should have been more specific. I wanted error message from the error property listed here:http://editor.datatables.net/manual/server#Server-to-client. for example in a create action :

          $data = Editor::inst( $db, 'site','site_id' ) ->fields(
           //add fields here
           ));
    
    
         if ( isset($_POST['action']) && ($_POST['action'] === "create")) {
                  //check if we have exceeded the total amount of locations we can add.
                   if($we_have_exceeded === true){
    
                        //output error message to the server "error" property
                        // this should be consumed by the "error" property not the 
                       //"fieldErrors" property.
    
                    }  else {
    
                           //if there is no error proceed to process as usual.
                            $out = $data->process( $_POST )->data();
    
                    }
            }
    
  • allanallan Posts: 61,776Questions: 1Answers: 10,112 Site admin
    Answer ✓

    I see - thanks for the clarification. There is currently no method built in to the Editor class to set that option as there is no form level validation, just field level. However, you can simply set it directly:

    $data = Editor::inst( ... )
      ->fields( ... )
      ->process( $_POST )
      ->data();
    
    $data['error'] = "Oh oh!";
    
    echo json_encode( $data );
    

    Obviously you'll need conditionals etc in then, but that is how it can be done.

    Allan

  • INTONEINTONE Posts: 153Questions: 58Answers: 6
    edited April 2015

    Thanks again. All is well.

This discussion has been closed.