<commit_before><?php
require 'Form.php';

// Defining the form using a class because it's actually cool to name forms.
// But we could have also pass the fields to the constructor.

class RegistrationForm extends Form {
    public function init() {
        $this->fields = array(
            'email' =>
                function($value) {
                    Form::check(!empty($value), 'Email is required');
                    Form::check(filter_var($value, FILTER_VALIDATE_EMAIL), 'Invalid email');
                },
            'password' =>
                function($value) {
                    Form::check(strlen($value) >= 6, 'Password must be at least 6 characters');
                },
            'password_confirmation' =>
                // php < 5.4 does not support $this in closure
                array($this, 'validatePasswordConfirmation'),
            'picture' =>
                function($value) {
                    Form::check($value['error'] != UPLOAD_ERR_NO_FILE, 'Picture is required');
                    Form::check($value['size'] < 307200, 'Please upload a picture less than 300k');
                    Form::check(in_array($value['type'], array('image/gif', 'image/jpeg', 'image/png')),
                        'Uploaded picture must be a gif, jpeg or png');
                }
        );
    }

    public function validatePasswordConfirmation($value) {
        Form::check($this->password == $value, 'Password confirmation must match');
    }
}

// Do this in your controller or something

$form = new RegistrationForm($_POST + $_FILES);
?>


<?php if ($form->errors): ?>
    <ul class="errors">
        <?php foreach ($form->errors as $key => $message): ?>
            <li><?= htmlentities($message) ?></li>
        <?php endforeach ?>
    </ul>
<?php elseif (!empty($form->values)): ?>
    <div>
        Email: <?= $form->email ?><br>
        Password: <?= $form->password ?><br><commit_msg>Update sample.php for PHP 5.4<commit_after><?php
require 'Form.php';

assert(version_compare(PHP_VERSION, '5.4', '>='));

// Defining the form using a class because it's actually cool to name forms.
// But we could have also pass the fields to the constructor.

class RegistrationForm extends Form {
    public function init() {
        $this->fields = array(
            'email' =>
                function($value) {
                    Form::check(!empty($value), 'Email is required');
                    Form::check(filter_var($value, FILTER_VALIDATE_EMAIL), 'Invalid email');
                },
            'password' =>
                function($value) {
                    Form::check(strlen($value) >= 6, 'Password must be at least 6 characters');
                },
            'password_confirmation' =>
                function($value) {
                    Form::check($this->password == $value, 'Password confirmation must match');
                },
            'picture' =>
                function($value) {
                    Form::check($value['error'] != UPLOAD_ERR_NO_FILE, 'Picture is required');
                    Form::check($value['size'] < 307200, 'Please upload a picture less than 300k');
                    Form::check(in_array($value['type'], array('image/gif', 'image/jpeg', 'image/png')),
                        'Uploaded picture must be a gif, jpeg or png');
                }
        );
    }

}

// Do this in your controller or something

$form = new RegistrationForm($_POST + $_FILES);
?>


<?php if ($form->errors): ?>
    <ul class="errors">
        <?php foreach ($form->errors as $key => $message): ?>
            <li><?= htmlentities($message) ?></li>
        <?php endforeach ?>
    </ul>
<?php elseif (!empty($form->values)): ?>
    <div>
        Email: <?= $form->email ?><br>
        Password: <?= $form->password ?><br>
{'fraction_non_alphanumeric': 0.1524486571879937, 'fraction_numerical': 0.00737230121116377, 'mean_word_length': 2.031923383878691, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6661489', 'n_tokens_mistral': 1055, 'n_tokens_neox': 994, 'n_words': 281}