This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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