Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A simple form validation example? #135

Open
trymeouteh opened this issue Feb 11, 2024 · 0 comments
Open

A simple form validation example? #135

trymeouteh opened this issue Feb 11, 2024 · 0 comments

Comments

@trymeouteh
Copy link

trymeouteh commented Feb 11, 2024

I was unable to find a simple web form validation example using Opis JSON Schema. The examples from Opis JSON Schema show the input data to be always in a JSON format.

I wrote this code below which is a simple form, however when I submit the form I get an error, The data (string) must match the type: integer

When I comment out the $_POST input and uncomment the manual input, it does work by I get no errors, but I am unable to use the form to submit the data.

How would one grab form data and process it for Opis JSON Schema is a simple clean way?

Any help will be most appreciated!

PHP code

<form method="post">
	<input name="name" placeholder="Name" value="<?=isset($_POST['name']) ? $_POST['name'] : '' ?>"/>
    <br />
	<input name="age" placeholder="Age" value="<?=isset($_POST['age']) ? $_POST['age'] : '' ?>"/>
	<br />
	<input type="submit" name="mySubmitButton" />
</form>



<?php

//Include the Composer autoloader
require 'vendor/autoload.php';





use Opis\JsonSchema\{
    Validator,
    ValidationResult,
    Errors\ErrorFormatter,
};



//Create a new validator
$myJSONSchemaValidation = new Validator();



//Register our schema
$myJSONSchemaValidation->resolver()->registerFile('http://subdomain.mydomain.com/myschema.json', 'mySchema.json');



if(isset($_POST['mySubmitButton'])) {
    // Manual input
    // $myInput = (object)[
    //     'name' => 'John Doe',
    //     'age' => 18
    // ];

    // $_POST input
    $myInput = (object)[
        'name' => htmlspecialchars($_POST['name']),
        'age' => htmlspecialchars($_POST['age'])
    ];




    //Validate the input
    $result = $myJSONSchemaValidation->validate($myInput, 'http://subdomain.mydomain.com/myschema.json');

    if ($result->isValid()) {
        echo "Valid";
    }

    else {
        print_r((new ErrorFormatter())->format($result->error()));
    }
}

JSON code

{
    "type": "object",
    "properties": {
        "name": {
            "type": "string",
            "minLength": 5,
            "maxLength": 64,
            "pattern": "^[a-zA-Z0-9\\-]+(\\s[a-zA-Z0-9\\-]+)*$"
        },
        "age": {
            "type": "integer",
            "minimum": 18,
            "maximum": 150
        }
    },
    "required": [
        "name",
        "age"
    ],
    "additionalProperties": false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant