I am wondering how to pass to inputs old value if user fails the laravel validation. I've tried to use:
Example: {{ Request::old('mail') }}
And: {{ old('mail') }}
I think that if user fails laravel validation input values are deleted.
Anyone has some idea to solve the problem?
With laravel 5.2 If you’re redirecting back to the form, a really useful method is withInput():
return redirect()->back()->withInput();
This method has no parameters and what it does is saves the old form values into Session. Then in the form you can use function old($key) to retrieve those values for every field – that would be a separate topic about the Forms.
You need to do something like this, if invalid
return Redirect::to('your_ulr')->withInput(Request::all());
Related
I have two modal fields on a page with forms and need to determine which of the model fields is has an error.
So my question is:
How can I pass multiple MessageBag's to $errors to be able check errors using $errors->errorType->get('message')?
I found a solution, it is called "Named Error Bags":
First need to manually create validator: https://laravel.com/docs/5.3/validation#manually-creating-validators
and then create named bag in response;
I've a standard form (without object) and I need to check if field1 < field2. If not I would like to display the error in the form like I do when I'm using form validation with object.
I red http://symfony.com/doc/current/book/validation.html#validating-values-and-arrays but it's for assert constraints. Mine is specific.
I also red this http://symfony.com/doc/current/book/forms.html#adding-validation. But once again, it use constraint validation.
Maybe I need to use this : http://symfony.com/doc/current/cookbook/validation/custom_constraint.html.
Thanks for your advices
After some research, here is my solution.
In my controller, If my condition is true, than I'm using the code to set an error and return the form. That's way, I see my error for the specific field.
use Symfony\Component\Form\FormError;
...
$form = $this->createForm(new formType());
$form->get('my-field')->addError(new FormError($this->get('translator')->trans('error.message.greather.than')));
return $this->render('MyBundle:Default:search.html.twig', array('form' => $form->createView()));
I'm not sure that it's the best way but it works for me.
I have a helper method that returns a captcha image url & stores a session of the key:
function captcha(){
$builder = new CaptchaBuilder;
$builder->build();
Session::put('phrase', $builder->getPhrase());
return $builder->inline();
}
The user then writes the captcha and submits the form and my controller grabs all and validates it:
'captcha' => 'required|same:'.Session::get('phrase')
The problem is no-matter what it always says they phrase & the textbox submission are not the same...
I can give more information if needed, also if this is not the best way to do it please give me suggestions, I am just learning Laravel4.
Edit for some output info:
If I return the values from the controller:
return "Session:".Session::get('phrase')." - Input:".$input['captcha'];
It returns: Session:5zij5 - Input:5zij5
According to Laravel's docs, same refers to another input.
I'd recommend using a custom validation rule to compare the session and the input.
I have a big model with around 13 attributs.
I want to update a few value of them using a form.
But actually using $form->isValid() empty my model and set only value which are feeded in my form or have an inputFilter setted.
Are they a way to avoid it ?
Checkout Validation Groups
http://framework.zend.com/manual/2.0/en/modules/zend.form.quick-start.html#validation-groups
You can partially validate your model, and only get back the values in the Validation Group.
I think you have set filter for all the fields in form ,
check what you are getting with this .
$unfiltered = $form->getUnfilteredValues();
print_r($unfiltered);
There is a sample in the bottom of the official documentation http://kohanaframework.org/3.2/guide/kohana/security/validation
But obviously it wont work at the request as long as $post['username'] in View is used but the $post array is empty on first request.
So how do you restore the values in this case? Any general solution?
PS: yes, I do understand I could do isset($post['username']) ? $post['username'] : ''; but it is just annoying
I use the model to display the data in the form. That way the initial form value is the initial value in the model.
I then update the model data with POST data in the controller, if there are validation errors, the model data will contain the POST data. This means I don't have to put any conditional logic in the view, and I just do: Form::input('name', $model->name)
Here's a more detailed explanation of this approach: Kohana ORM and Validation, having problems
I use Arr::get function:
echo Form::input('name', Arr::get($post, 'name'))
I was just looking at the old documentation on Building and Validating a Form.
You can see from the sample code that first you need to initialize an array with the form field names as the key and set the value to an empty string. And if there's an error, fill in the values of each element. In the views, you can simply call Form::input() normally without any if statement or some sort.
I guess Kohana has already been built this way from the start. And it doesn't seem to change. You'll probably just need to do the same thing.