PHP code example of eghojansu / bumbon-validation

1. Go to this page and download the library: Download eghojansu/bumbon-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/ */

    

eghojansu / bumbon-validation example snippets




$validation = Validation::create(
    [
        'foo' => NotBlank(),
        'baz' => [
            new NotBlank(),
            new Length([
                'min' => 5,
            ])
        ],
    ],
    [
        'foo' => 'bar',
        'baz' => 'qux'
    ]
);
$violation = $validation->validate();

// or
// $validation->validate(['foo' => 'bar','baz' => 'qux']);

if ($violation->hasViolation()) {
    echo 'Invalid data';
} else {
    echo 'Data valid';
}

// or
if ($violation->noViolation()) {
    echo 'Data valid';
} else {
    echo 'Invalid data';
}

// get violation message
var_dump($violation->all());

// [
//   'foo' => ['Nilai ini tidak boleh kosong']
// ]




// default parent option
$option = [
    // message
    'message' => 'Custom message',
    // group validate
    'groups' => ['Default'],
    // trim value
    'trim' => true,
    // callback for normalize data (before validate)
    'normalizer' => null,
];

  
  $option = [
    'min' => 5,
    'max' => 5,
  ];
  

  
  $option = [
    // callback (return bool)
    'callback' => null,
  ];
  

  
  $option = [
    // array of valid choices
    'choices' => [],
  ];
  

  
  $option = [
    // equal to this value
    'value' => null,
  ];
  

  
  $option = [
    // greater than or equal to this value
    'value' => null,
  ];
  

  
  $option = [
    // greater than to this value
    'value' => null,
  ];
  

  
  $option = [
    // identical to this value
    'value' => null,
  ];
  

  
  $option = [
    // pdo connection
    'pdo' => null,
    // table to lookup
    'table' => null,
    // field to find
    'field' => 'ID',
  ];
  

  
  $option = [
    'min' => 5,
    'max' => 5,
  ];
  

  
  $option = [
    // lest than or equal to this value
    'value' => null,
  ];
  

  
  $option = [
    // less than to this value
    'value' => null,
  ];
  

  
  $option = [
    // not equal to this value
    'value' => null,
  ];
  

  
  $option = [
    // not identical to this value
    'value' => null,
  ];
  

  
  $option = [
    // pdo connection
    'pdo' => null,
    // table to lookup
    'table' => null,
    // field to find
    'field' => 'ID',
    // primary key, can be array
    'id' => 'ID',
    // current primary key value, can be array
    'current_id' => null,
  ];
  

  
  $option = [
    // match to this pattern
    'pattern' => null,
  ];
  



namespace Bumbon\Validation\Constraint;

interface ConstraintInterface
{
    /**
     * Get violation message
     * @return array|string
     */
    public function getMessages();

    /**
     * Get groups
     * @return array
     */
    public function getGroups();

    /**
     * Set value to check
     * @param mixed $value
     * @return  $this
     */
    public function setValue($value);

    /**
     * Get value
     * @return  mixed
     */
    public function getValue();

    /**
     * Validate value
     * @return $this
     */
    public function validate();

    /**
     * Get constraint validity
     * @return boolean
     */
    public function isValid();
}




use Bumbon\Validation\Constraint;

class CustomConstraint extends AbstractContraint
{
    public function validate()
    {
        $this->valid = $this->value == 'true';

        return $this;
    }
}