public function testInvalidValidation() { $rules = array('test1' => array('rule1(:test2,3,:test3)' => function ($input, $test2, $value, $test3) { if ($input == "test data 1" && $value == 3 && $test2 == "test data 1" && $test3 == "test data 1") { return true; } return false; })); $naming = array('test2' => 'Test 2'); $validation = \SimpleValidator\Validator::validate($this->test_data, $rules, $naming); $this->assertFalse($validation->isSuccess()); $validation->customErrors(array('rule1' => "Foo :params(0) bar :params(1) baz :params(2)")); $errors = $validation->getErrors(); $this->assertEquals("Foo Test 2 bar 3 baz test3", $errors[0]); }
public function validate($data) { $obj = $this; $rules = array(':book_type' => array('required', 'integer', 'type_exists' => function ($input) use($obj) { return $obj->checkExist('type_id', ':type_id', 'types', $input); }), ':book_city' => array('required', 'integer', 'city_exists' => function ($input) use($obj) { return $obj->checkExist('city_id', ':city_id', 'cities', $input); }), ':book_size' => array('required', 'integer', 'size_exists' => function ($input) use($obj) { return $obj->checkExist('size_id', ':size_id', 'sizes', $input); }), ':book_address' => array('required'), ':book_coordinate_lat' => array('required')); $names = [':book_address' => 'Adresas']; $validate = \SimpleValidator\Validator::validate($data, $rules, $names); if ($validate->isSuccess() == true) { $response = ['success' => 'true']; return $response; } else { $validate->customErrors(['required' => '\':attribute\' Laukas yra privalomas', ':book_coordinate_lat.required' => 'Prašome patikslinti vietą', 'integer' => 'Reikšmė turi būti skaičius', 'type_exists' => 'Blogai pasirinktas tipas', 'city_exists' => 'Blogai pasirinktas miestas', 'size_exists' => 'Blogai pasirinktas dydis']); $response = ['success' => 'false', 'errors' => $validate->getErrors()]; return $response; } }
<?php /** * @author Gaaaab */ use App\Http\Model\Contact; use Tamtamchik\SimpleFlash\Flash; $app->get('/contact', function () use($app) { $app->render('site/contacts/contact.php'); })->name('contact.index'); $app->post('/contact', function () use($app) { // Mail $mail = new \Mailgun\Mailgun(MAILGUN_KEY); // Validation $rules = ['name' => ['required', 'alpha', 'min_length(3)', 'max_length(60)'], 'email' => ['email', 'required'], 'content' => ['required']]; $input = ['name' => $app->request->post('name'), 'email' => $app->request->post('email'), 'content' => $app->request->post('content')]; $validation_result = \SimpleValidator\Validator::validate($_POST, $rules); if ($validation_result->isSuccess()) { $contact = new Contact(); $contact->name = $app->request->post('name'); $contact->email = $app->request->post('email'); $contact->content = $app->request->post('content'); // Saving it to database? not necessary but WTH not =3 $contact->save(); $mail->sendMessage(MAILGUN_DOMAIN, array('from' => MAIL_FROM, 'to' => MAIL_TO, 'subject' => 'SHARE SOMETHING SUPPORT: CONTACT RESPONSE', 'text' => 'Contact request from: ' . $app->request->post('name') . '\\n' . 'Email: ' . $app->request->post('email') . '\\n' . 'Content: ' . ' ' . $app->request->post('content'))); $app->redirectTo('site.index'); } else { require_once __DIR__ . '/../errors.php'; } })->name('contact.post');