Skip to content

Instantly share code, notes, and snippets.

@tomothumb
Last active January 17, 2016 16:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tomothumb/46e33908adee4134bac5 to your computer and use it in GitHub Desktop.
<?php
namespace App\Form;
use Aura\Input\Form;
class Contact extends Form
{
public function init()
{
$this->setField('first_name', 'text')
->setAttribs(array(
'id' => 'first_name',
'size' => 20,
'maxlength' => 20,
));
$this->setField('message', 'text')
->setAttribs(array(
'id' => 'message',
'size' => 200,
'maxlength' => 200,
));
$filter = $this->getFilter();
$filter->addSoftRule('first_name', $filter::IS, 'string');
$filter->addSoftRule('first_name', $filter::IS, 'strlenBetween', 6, 12);
}
}
$di->set('MYAPP/contactform',$di->lazyNew('App\Form\Contact'));
....
$dispatcher->setObject('send', function () use ($di) {
$request = $di->get('aura/web-kernel:request');
$input = $request->post->get();
$response = $di->get('aura/web-kernel:response');
$response->content->setType('application/json');
$response_content = array();
$form = $di->get('MYAPP/contactform');
$form->fill($input);
$success = $form->filter();
if ($success) {
$response_content["message"] = "User input is valid.";
} else {
foreach ($form->getMessages() as $name => $messages) {
foreach ($messages as $message) {
$response_content["message"][] = "Input '{$name}': {$message}";
}
}
}
$response_content["data"] = $input;
$response->content->set($response_content);
$json = json_encode($response->content->get());
$response->content->set($json);
});
$dispatcher->setObject('send', function () use ($di) {
$request = $di->get('aura/web-kernel:request');
$input = $request->post->get();
$response = $di->get('aura/web-kernel:response');
$response->content->setType('application/json');
$response_content = array();
$filter_factory = new FilterFactory();
$filter = $filter_factory->newInstance();
$filter->addSoftRule('first_name', $filter::IS, 'string');
$filter->addSoftRule('first_name', $filter::IS, 'strlenBetween', 6, 12);
$success = $filter->values($input);
if ( $success) {
$response_content["message"] = "User input is valid.";
}else{
$response_content["message"][] = "User input is not valid.";
$messages = $filter->getMessages();
$response_content["message"] = $messages;
}
$response_content["data"] = $input;
$response->content->set($response_content);
$json = json_encode($response->content->get());
$response->content->set($json);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment