PHP code example of xire28 / redbean-validation

1. Go to this page and download the library: Download xire28/redbean-validation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xire28 / redbean-validation example snippets



re 'rb.php';

R::setup( 'mysql:host=localhost;dbname=redbean', 'root', '' );

class Model_User extends RedBean_SimpleModel
{
  use RedbeanValidation\ModelValidation;

  static $validates_length_of = [
    ['first_name', 'within' => [5, 20]],
    ['last_name', 'within' => [5, 20]]
  ];

  static $validates_uniqueness_of = [
    [['first_name', 'last_name'], 'message' => 'name already in use']
  ];

  public function validate(){
    if ($this->first_name == $this->last_name)
    {
        $this->errors->add('first_name', "can't be the same as last name");
        $this->errors->add('last_name', "can't be the same as first name");
    }
  }
}

$user = R::dispense('user');
$user->first_name = 'John';
$user->last_name = 'John';
if($user->is_valid()) 
{
  R::store($user);
}
else
{
  var_dump($user->errors());
}