Is there any way that we can loop a function in a controller for multiple data validation? - laravel

I'm having a problem related to multiple data validation and want to know if there is any solution for loop condition of a function in a controller.If any please provide the solution.
I had tried using for loop but that dosent seem to work on Laravel Controllers

by default laravel supports multiple rules for validation, you have add | between the rules, like required|numeric|unique:posts. laravel will check the rules one after another.
Please add more details on your question and add your code.

Related

Laravel: form method != save method?

I am new to Laravel coming from CakePHP where the form and save method for a form is one and the same function name. I saw in many Laravel tutorials that the from method (that displays the form) is different than the method to save form (that actually saves data). Why using 2 different method names?
For example what's wrong with:
pub function xyz(Request $request)
{
if($results->isMethod('post')){
... then save and return redirect
}
... the code for showing the form in case there is no POST.
then having 2 routes one for GET and one for POST on the same url?
It is because people like to filter out things at route level not in controller, Also it helps developer to apply middleware grouping for each route separately. so that they can apply roles and permission etc. easily at route level.
It will looks horrible if mix all things in controller.
Think about middleware and groups in your code.
It is because you don't wanna mix a lot of logic in the same method . The case you have simple is the simple scenario . But there will be case where you wanna pass initial data in the create form . You have to write logic for that also in the same method and while you store the data you need to do the validation and calculate other business logic . If you combine all those things in one method it will mix all the things in one method and code difficult to read

Laravel Nova - How can i do custom logic on Create/Update

I would like to create custom Create/Update logic for one of my Nova Resource. I have been thinking to use Custom Tools to accomplish what i wanted, but i don't want to give up the excellent work in the Index page and rewrite it all myself.
My use case:
I want to have an input that can define how many record to be created recursively.
I want to have morphToMany selection input in the form.
If you want to use different components on the index vs form/detail page just use the hideFromIndex and onlyOnIndex functions.
Detailed reference to docs: https://nova.laravel.com/docs/1.0/resources/fields.html#showing-hiding-fields
If you want to build repetitive fields, you can use the package: https://packagist.org/packages/fourstacks/nova-repeatable-fields

Laravel 5 set route conditionally

Hi I want to get specific route only based on the condition. I currently tried Session but it doesn't work with routes. So anyone here might want to help if there's simpe way to this.
if(\Session::get('quiz_type') == 'quiz'){
Route::resource('quizzes.questions', 'QuestionsController');
}else{
Route::resource('surveys.questions', 'QuestionsController');
}
I want that certain route Quiz only if I passed and meet a certain condtion. Else I want to call on different route.
If I remember correctly Session start only after laravel has already parsed the routes file, this is why your code doesn't work as you expect.

Symfony2 validation filters

In my Symfony 2 application I need to filter input before passing it on to validation [1], however, I can't seem to find any system within Symfony to do this.
The type of filtering I looking for is e.g. to be able to filter a dash out of a specific field before validating it. E.g. users can enter 123-123 but the only accepted value is 123123. Just as I can set up validation rules with constraints, I'm looking for something similar for filters.
[1] http://symfony.com/doc/current/book/validation.html
Nifr's answer is good but is missing of an important alternative that, if I understand correctly your question, seems to fit perfectly your needs.
You can use a hook that is pretty much an event listener: if something happens or is going to happen, it intercepts the event and redirect it to your function.
In this case, you need a PRE_BIND hook (is deprecated since 2.3 version, now it's called PRE_SUBMIT)
Read this if you need help about
Either write your own Validation Assert to filter and then proxy the other validators for this purpose ...
... or one or multiple Regex Asserts.
... or use a DataTransformer to transform/filter the input.
With the DataTransformer involved you could aswell consider creating a new FieldType which renders two inputs with a seperator like the date form-field does. ( if not not used with widget => single_text )

CodeIgniter Validation: possible to validate GET query strings?

The form validation library seems to only work on POST. I need to use query strings and would like to use CI to validate the passed values. Is there a way to do this?
The current Codeigniter 3.0 development branch provides an option to insert your own variable instead of $_POST. So you could start using 3.0.
Alternatively, the only way in CI2.1 is to do $_POST=$_GET before you run the validation.
See this page for the CodeIgniter 3 solution:-
http://www.codeigniter.com/userguide3/libraries/form_validation.html#validating-an-array-other-than-post
For CodeIgniter 2 you can do $_POST = $_GET; before $this->form_validation->run() as mentioned above.
You could overwrite the Form_validation function run in a MY_Form_Validation and modify it.
Reference How do I validate a form field in Codeigniter when using Get parameters?
Before validation rules, set the validation data with the following code.
$this->form_validation->set_data($_GET);

Resources