Laravel Form Request Validation Error Issue - forms

I'm using the Form Request Validation (Laravel 5) to validate my input.
It validates my form, but when it doesn't get through the validation Laravel should return automatically an in-built error.
But it stays on the same page (without refreshing) and without showing an error, neither putting the input into the database
//Controller
public function store(StoreProjectPostRequest $request)
{
$input = $request->all();
Project::create($input);
return Redirect::route('projects.index')->with('message', 'Project created');
}
//Request
public function rules()
{
return [
'name' => 'required|max:15|alpha_num',
'slug' => 'alpha_dash|required|max:10',
];
}

Related

Silverstripe 4 Form on custom PageController submit

I have a Form in my custom PageController.
The Form renders properly in template Register.ss.
In my setup the submit - function on PageController doesn't work.
I have tried it without setting the submit-route in routes.yml
resulting in a 404.
I have tried to set the submit-route in routes.yml
and in $url_handlers which results in an error:
Uncaught ArgumentCountError: Too few arguments to function
TherapyRegisterController::submit(), 1 passed in
/var/home/xxxxx/vendor/silverstripe/framework/src/Control/RequestHandler.php
on line 323 and exactly 2 expected
How to get the submit - function to work?
//routes:
SilverStripe\Control\Director:
rules:
therapy//anmeldung/$ID: TherapyRegisterController
# therapy//submit: TherapyRegisterController
TherapyRegisterController:
class TherapyRegisterController extends PageController{
private static $allowed_actions = ['registerForm', 'submit'];
private static $url_handlers = [
'anmeldung/$ID' => 'index',
//'anmeldung/submit' => 'submit',
];
public function registerForm($id)
{
$fields = new FieldList(
TextField::create('Name', 'Name')
);
$actions = new FieldList( [
$cancelButton = FormAction::create('cancel')->setTitle('ABBRECHEN')->setAttribute('formaction', 'therapy/cancel'), // 'cancel'
$sendButton = FormAction::create('submit')->setTitle('ANMELDEN')->setAttribute('formaction', 'therapy/submit') // 'submit'
]);
$validator = new RequiredFields('');
$form = new Form($this, 'registerForm', $fields, $actions, $validator);
$form->setTemplate('Register');
return $form;
}
public function submit($data, Form $form)
{
Debug::show($data);
}
public function index(HTTPRequest $request)
{
$arrayData = array (
'ID' => $request->param('ID')
);
return $this->customise($arrayData)->renderWith(array('Anmeldung', 'Page'));
}
Register.ss :
$registerForm($ID)
The ArgumentCountError does say that the submit method was only getting 1 argument but that submit method is expecting 2 as seen in your code. I'm not sure what exact version of SilverStripe you have but I can see this on line 323 of that RequestHandler.php:
$actionRes = $this->$action($request);
That first argument is going to be a SilverStripe\Control\HTTPRequest. The form submission should be a POST. You can get that array $data using the example below:
public function submit($request)
{
$data = $request->postVars();
}
It seems that you can't get that Form $form from the arguments here.

Validation error messages as JSON in Laravel 5.3 REST

My app is creating a new entry via a POST request in an api end point.
Now, if any validation is failed then instead of returning an error json, laravel 5.3 is redirecting the request to home page.
Here is my controller:
public function create( Request $request )
{
$organization = new Organization;
// Validate user input
$this->validate($request, [
'organizationName' => 'required',
'organizationType' => 'required',
'companyStreet' => 'required'
]);
// Add data
$organization->organizationName = $request->input('organizationName');
$organization->organizationType = $request->input('organizationType');
$organization->companyStreet = $request->input('companyStreet');
$organization->save();
return response()->json($organization);
}
If there is no issue with validation then the entity will be successfully added in the database, but if there is issue with validating the request then instead of sending all the error messages as a json response it redirects back to the home page.
How i can set the validate return type to json, so with every request if the validation failed then laravel will send all the error messages as json by default.
You can do your validation as:
$validator = \Validator::make($request->all(), [
'organizationName' => 'required',
'organizationType' => 'required',
'companyStreet' => 'required'
]);
if ($validator->fails()) {
return response()->json($validator->errors(), 422)
}
The validation used in the question looks as per the recommendation by laravel. The reason of redirection is that it throws an exception which you can easily catch using the code below. So it's better to use the recommended way of code instead of re-writing framework's code again :)
public function create( Request $request )
{
$organization = new Organization;
// Validate user input
try {
$this->validate($request, [
'organizationName' => 'required',
'organizationType' => 'required',
'companyStreet' => 'required'
]);
} catch (ValidationException $e) {
return response()->json($e->validator->errors(), 422);
}
// Add data
$organization->organizationName = $request->input('organizationName');
$organization->organizationType = $request->input('organizationType');
$organization->companyStreet = $request->input('companyStreet');
$organization->save();
return response()->json($organization, 201);
}

field array type in entity for form choice type field symfony

I would like to create a UserForm for create user in my system backend.
I use a entity with a 'role' field as type array
I want use a select choice field type Form with that entity field.
I use a transformer class system for convert data between Entity and form.
but I turn around in my head and nothing run correctly.
When I use options 'multiple' of choice type, my field display correctly but I don't want to display and select multiple value for this field.
I have Notice: Undefined offset: 0 error
or
I have ContextErrorException: Notice: Array to string conversion
Here few essential code :
UserForm class
$builder->add($builder->create('roles', 'choice', array(
'label' => 'I am:',
'mapped' => true,
'expanded' => false,
'multiple' => false,
'choices' => array(
'ROLE_NORMAL' => 'Standard',
'ROLE_VIP' => 'VIP',
)
))->addModelTransformer($transformer));
transformer Class
class StringToArrayTransformer implements DataTransformerInterface
{
public function transform($array)
{
return $array[0];
}
public function reverseTransform($string)
{
return array($string);
}
}
controller method
$user = new User(); //init entity
$form = $this->createForm(new UserForm(), $user);
$form->handleRequest($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($form);
$em->flush();
return $this->redirect($this->generateUrl('task_success'));
}
entity part
/**
* #ORM\Column(name="roles", type="array")
*/
protected $roles;
public function getRoles()
{
return $this->roles;
}
public function setRoles(array $roles)
{
$this->roles = $roles;
return $this;
}
My field roles entity must be a array for run correctly the security component Symfony
can you help me to understand why this field form refuse to display ?
I already readed others questions in same issue but there is anything that I don't understand because nothing help me to resolve my problem.
If you can help me with MY particular context...
Thank for support
because security symfony component integration
If you only need the "getRoles" method because of the interface you are implementing, it is simpler (and cleaner) to do the following:
Change the entities field again to role with type string
Rename your getter and setter to getRole() and setRole()
and add a getRoles method like this:
public function getRoles()
{
return array($this->role);
}
In your form type, change the field name to "role" and 'multiple' => false
Remove your model transformer
This should be the solution ;)

How can I replace this _forward() with something that can exit?

I use the following code over and over in my zend framework application. It is used in action() to check if an article exists. If not, the user shall see an error message:
$article = ArticleQuery::create()->findOneByUrl($this->_getParam('url', ''));
if (!$article) {
$this->getResponse()
->setRawHeader('HTTP/1.1 404 Not Found');
return $this->_forward('error', null, null, array(
'message' => 'Article not found',
));
}
I was wondering how to factor this out into an own method to reduce the code load in all actions.
I came to something like this:
protected function myAction() {
$article = $this->getArticleIfExists($this->_getParam('url', ''));
if ($article == null) {
return;
}
}
protected function getArticleIfExists($url) {
$article = ArticleQuery::create()->findOneByUrl($this->_getParam('url', ''));
if ($article) {
return $article;
} else {
$this->getResponse()
->setRawHeader('HTTP/1.1 404 Not Found');
$this->_forward('error', null, null, array(
'message' => 'Article not found',
));
return nulL;
}
}
I still would like to get rid of the if case in myAction(), but _forward() does not allow to exit the execution (of course, because it still needs to execute the other actions).
Another possibility (I have implemented in some other controllers) is this:
protected function myAction() {
$article = ArticleQuery::create()->findOneByUrl($this->_getParam('url', ''));
if (!$article) {
return $this->notFound('Article does not exist');
}
}
protected function notFound($message) {
$this->getResponse()
->setRawHeader('HTTP/1.1 404 Not Found');
return $this->_forward('error', null, null, array(
'message' => $message,
));
}
Again we have this if check in the action. It’s already better than before, but can we make it even better?
How can I circumvent this? Is there a possibility to do it without losing the current URL? With a Redirector I can of course exit, but then I would lose the current URL (/controller/myaction/url/hello -> /error/error/message/Article%20not%20found)
A possible approach would be to throw an Exception. Because of the Zend_Controller_Plugin_ErrorHandler this will automatically redirect you to the ErrorController without any further code being executed.
If you don't want to get to the ErrorController but only to the current controller's error actions, you can simply modify the plugin in the controller's init method:
public function init()
{
$plugin = Zend_Controller_Front::getInstance()->getPlugin('Zend_Controller_Plugin_ErrorHandler');
$plugin->setErrorHandlerModule('default')
->setErrorHandlerController('MyController')
->setErrorHandlerAction('error');
}
But of course you can also write your own ErrorHandler plugin for a more fine grained error handling. This is described in the Zend Framework Manual on Plugins
For something as simple as just displaying a "* does not exist" against a user request I prefer to leave the user in the application and just hit them with a flashmessenger notice and leave them on the page to make another request (if appropriate):
public function indexAction() {
//get form and pass to view
$form = new Form();
$this->view->form = $form;
try {
//get form values from request object
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
$data = $form->getValues();
// Do some stuff
}
}
} catch (Zend_Exception $e) {
$this->_helper->flashMessenger->addMessage($e->getMessage());//add message to flashmessenger
$this->_redirect($this->getRequest()->getRequestUri());//perform redirect to originating page so the messenger will flash
}
}
This is simple and works well when the possibility for incorrect user input exists.

How does one create a validator that depends on more than one value for Zend_Form?

I've got a form that has a single text field (for company):
class Cas_Form_Company extends Zend_Form
{
public function init()
{
$this->addElement('hidden', 'id');
$this->addElement('text', 'name', array('label' => 'Name'));
$this->addElement('submit', 'submit', array('label' => 'Create'));
$name = $this->getElement('name');
$name->addValidator('stringLength', false, array(2,45));
$name->addValidator(new Cas_Model_Validate_CompanyUnique());
$this->setMethod('post');
$this->setAction(Zend_Controller_Front::getInstance()->getBaseUrl() . '/Company/Submit');
}
public function SetFromExistingCompany(Cas_Model_Company $company)
{
$this->getElement('id')->setValue($company->GetId());
$this->getElement('name')->setValue($company->GetName());
$this->getElement('submit')->setLabel('Edit');
$this->addElement('submit', 'delete', array('label' => 'Delete', 'value' => 'delete'));
}
public function Commit()
{
if (!$this->valid())
{
throw new Exception('Company form is not valid.');
}
$data = $this->getValues();
if (empty($data['id']))
{
Cas_Model_Gateway_Company::FindOrCreateByName($data['name']);
}
else
{
$company = Cas_Model_Gateway_Company::FindById((int)$data['id']);
$company->SetName($data['name']);
Cas_Model_Gateway_Company::Commit($company);
}
}
}
I've also created a little validator which enforces that I want companies to have unique names:
class Cas_Model_Validate_CompanyUnique extends Zend_Validate_Abstract
{
protected $_messageTemplates = array(
'exists' => '\'%value%\' is already a company.'
);
/**
* #param string $value
* #return bool
*/
public function isValid($value)
{
$this->_setValue($value);
$company = Cas_Model_Gateway_Company::FindByName($value);
if ($company)
{
$this->_error('exists');
return false;
}
return true;
}
}
Now, this works just fine for creating new companies. The problem comes in when I want to allow editing of companies. This is because for an edit operation, while the company name needs to be unique, a form containing the name already pertaining to the given ID isn't an edit at all (and therefore is valid). That is, the form is valid if either the name doesn't already exist in the database, or the name given matches the name already assigned to that ID.
However, writing this as a validator seems to be problematic, because the validator only gets the value it's working on -- not the ID in question.
How does one write a validator for this sort of thing?
You can use the poorly documented second $context argument to isValid().
See http://framework.zend.com/manual/en/zend.form.elements.html#zend.form.elements.validators and scroll down to the note "Validation Context"
I think this link may help you.
Zend Form Edit and Zend_Validate_Db_NoRecordExists
You have to user Db no record exist but for edit you can specify the exclude attribute in validation.