I am newbie with Laravel and picked up a system to give maintenance.
I have this rule:
protected $rules = array(
'per_vlr_principal_rte' => 'required_if:per_fase_processual,2',
'per_vlr_juros_rte' => 'required_if:per_fase_processual,2',
'per_vlr_principal_rdo' => 'required_if:per_fase_processual,1,2',
'per_vlr_juros_rdo' => 'required_if:per_fase_processual,1,2',
'per_vlr_principal_perito' => 'required_if:per_fase_processual,2',
'per_vlr_juros_perito' => 'required_if:per_fase_processual,2',
'per_vlr_homologado' => 'required_if:per_fase_processual,2',
'per_qte_rte' => 'required',
'per_competencias' => 'required',
'per_qte_laudo' => 'required',
'per_dt_calculo' => 'required'
);
I'd like to know if the code below is correct:
'per_vlr_principal_rdo' => 'required_if:per_fase_processual,1,2',
'per_vlr_juros_rdo' => 'required_if:per_fase_processual,1,2',
The validation occurs only when the value is 1.
How to fix it?
Related
I'm trying to validate some input array fields in Laravel. My application reports back the field is required, even though it has been filled out.
The validation code I have is:
$this->validate($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'email' => 'unique:contacts,email,' . $request->id,
'address' => 'required',
'address.0.address_line_1' => 'required',
]);
The full posted array is:
Array
(
[id] => 1
[_token] => xxx
[first_name] => Joe
[last_name] => Bloggs
[contact_name] => Joe Bloggs
[telephone] => 077
[email] => joe#test.com
[address] => Array
(
[0] => Array
(
['address_line_1'] => sss
['address_line_2'] =>
['city'] =>
['county'] =>
['postcode'] =>
['property_type'] => site
)
)
)
My input fields are constructed like so:
address[0]['address_line_1']
I'm getting the validation message error:
The address.0.address line 1 field is required.
Anyone know what's wrong here?
That's because you don't have any address_line_1 or address_line_2 in your array validation but you have posted those two elements.
You only have address.0.address_line_1 and there's no address.0.address_line_1 inside your posted data.
The elements of your array validation should be the same with what you posted.
In your case, you can remove address.0.address_line_1 and add 2 more elements of your array validation.
'address_line_1' => 'required',
'address_line_2' => 'required',
The updated answer :
'address' => 'required|array',
'address.*' => 'required|array',
'address.*.address_line_1' => 'required|string',
Just replace your validator with this
$this->validate($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'email' => 'unique:contacts,email,' . $request->id,
'address' => 'required',
'address.*' => 'required',
'address.*.address_line_1' => 'required',
]);
I managed to figure out what was causing the issue...it was actually to do with the form input field names.
I removed the single quotes from the input names and it worked!
I changed:
<input type="text" class="form-control" name="address[0]['address_line_1']">
To, this:
<input type="text" class="form-control" name="address[0][address_line_1]">
And it worked! Hope it helps someone else.
in my app the user can update the info of stripe connected account, however I ONLY want to actullay update the value of the fields that appear in the request payload, I could do this with a simple if check but the way I update the stripe array method makes this issue more complicated .
Is there any syntax sugar or trick to make this easier.
How my update method looks;
public function editConnectedAccount(Request $request)
{
$account = Account::retrieve($request->connectedAccountId);
Account::update(
$request->connectedAccountId,
[
'type' => 'custom',
'country' => 'ES',
'email' => $request->userEmail,
'business_type' => 'individual',
'tos_acceptance' => [ 'date' => Carbon::now()->timestamp, 'ip' => '83.46.154.71' ],
'individual' =>
[
'dob' => [ 'day' => $request->userDOBday, 'month' => $request->userDOBmonth, 'year' => $request->userDOByear ],
'first_name' => $request->userName,
'email' => $request->userEmail,
'phone' => $request->userPhone,
'last_name' => $request->userSurname,
//'ssn_last_4' => 7871,
'address' => [ 'city' => $request->userBusinessCity, 'line1' => $request->userBusinessAddress, 'postal_code' => $request->userBusinessZipCode, 'state' => $request->userBusinessCity ]
],
'business_profile' =>
[
'mcc' => 5812, //got it
'description' => '',
//'url' => 'https://www.youtube.com/?hl=es&gl=ES', //got it
],
'capabilities' => [
'card_payments' => ['requested' => true],
'transfers' => ['requested' => true],
],
]
);
return response()->json([
'account' => $account,
], 200);
Consider using a Form Request where you preform validation. This will neaten up your controller for a start and also make validation (never trust user input!) reusable.
Assuming validation is successful, calling $request->validated() from inside your controller method will return only the fields present and validated. You can then use either fill($request->validated()) or update($request->validated()).
im using laravel 7 and im using php faker ..
if i want to seed customer i did this code ..
$factory->define(App\Models\Customer::class, function (Faker $faker) {
return [
'number' => $faker->numberBetween(1,1000000),
'name' => $faker->name,
'foreign_name' => $faker->name,
'financial_account_type_id' => 2,
'address' => $faker->address,
'tax_id' => $faker->numberBetween(1,100000),
'phone' => $faker->e164PhoneNumber,
'mobile' => $faker->e164PhoneNumber,
'email' => $faker->email,
'sales_payment_term_id' => $faker->numberBetween(1,7),
'purchase_payment_term_id' => $faker->numberBetween(1,7),
'note' => $faker->sentence,
];
});
now in my app.php i did this code also ..
'faker_locale' => 'ar_SA',
i want the 'foreign_name' => $faker->name, to be in english language just this line ..
is that possible to change only one line locale ..
thanks ..
I have updated symfony 2.8 -> 3.3
Now Top page looks working.
However when I load the page where uses form builder I burped into this error.
Type error: Argument 3 passed to
Sonata\CoreBundle\Form\Extension\DependencyInjectionExtension::__construct()
must be of the type array, object given, called in
/Users/whitebear/CodingWorks/httproot/myapp/var/cache/dev/appDevDebugProjectContainer.php
on line 1325
I think this is related to service container though, I can’t figure out where should I fix.
What I am doing is like this below
$em = $this->getDoctrine()->getManager();
$reqDoc = new reqDoc();
$form = $this->createFormBuilder($reqDoc)
error messages is below
DependencyInjectionExtension->__construct(object(appDevDebugProjectContainer), array('Symfony\Component\Form\Extension\Core\Type\FormType'
=>object(RewindableGenerator), 'Symfony\Component\Form\Extension\Core\Type\RepeatedType'
=> object(RewindableGenerator), 'Symfony\Component\Form\Extension\Core\Type\SubmitType'
=> object(RewindableGenerator), 'Symfony\Component\Form\Extension\Core\Type\ChoiceType'
=> object(RewindableGenerator)), object(RewindableGenerator), null,array('sonata_type_admin'
=> 'Sonata\AdminBundle\Form\Type\AdminType', 'sonata_type_model' => 'Sonata\AdminBundle\Form\Type\ModelType', 'sonata_type_model_list' =>
'Sonata\AdminBundle\Form\Type\ModelTypeList',
'sonata_type_model_reference' =>
'Sonata\AdminBundle\Form\Type\ModelReferenceType',
'sonata_type_model_hidden' =>
'Sonata\AdminBundle\Form\Type\ModelHiddenType',
'sonata_type_model_autocomplete' =>
'Sonata\AdminBundle\Form\Type\ModelAutocompleteType',
'sonata_type_native_collection' =>
'Sonata\AdminBundle\Form\Type\CollectionType',
'sonata_type_choice_field_mask' =>
'Sonata\AdminBundle\Form\Type\ChoiceFieldMaskType',
'sonata_type_filter_number' =>
'Sonata\AdminBundle\Form\Type\Filter\NumberType',
'sonata_type_filter_choice' =>
'Sonata\AdminBundle\Form\Type\Filter\ChoiceType',
'sonata_type_filter_default' =>
'Sonata\AdminBundle\Form\Type\Filter\DefaultType',
'sonata_type_filter_date' =>
'Sonata\AdminBundle\Form\Type\Filter\DateType',
'sonata_type_filter_date_range' =>
'Sonata\AdminBundle\Form\Type\Filter\DateRangeType',
'sonata_type_filter_datetime' =>
'Sonata\AdminBundle\Form\Type\Filter\DateTimeType',
'sonata_type_filter_datetime_range' =>
'Sonata\AdminBundle\Form\Type\Filter\DateTimeRangeType', 'tab' =>
'Mopa\Bundle\BootstrapBundle\Form\Type\TabType',
'sonata_block_service_choice' =>
'Sonata\BlockBundle\Form\Type\ServiceListType',
'sonata_type_container_template_choice' =>
'Sonata\BlockBundle\Form\Type\ContainerTemplateType', 'form' =>
'Symfony\Component\Form\Extension\Core\Type\FormType',
'birthday' =>
'Symfony\Component\Form\Extension\Core\Type\BirthdayType',
'checkbox' =>
'Symfony\Component\Form\Extension\Core\Type\CheckboxType',
'choice' =>
'Symfony\Component\Form\Extension\Core\Type\ChoiceType',
'collection' =>
'Symfony\Component\Form\Extension\Core\Type\CollectionType',
'country' =>
'Symfony\Component\Form\Extension\Core\Type\CountryType', 'date'
=> 'Symfony\Component\Form\Extension\Core\Type\DateType', 'datetime' =>
'Symfony\Component\Form\Extension\Core\Type\DateTimeType',
'email' =>
'Symfony\Component\Form\Extension\Core\Type\EmailType', 'file'
=> 'Symfony\Component\Form\Extension\Core\Type\FileType', 'hidden' =>
'Symfony\Component\Form\Extension\Core\Type\HiddenType',
'integer' =>
'Symfony\Component\Form\Extension\Core\Type\IntegerType',
'language' =>
'Symfony\Component\Form\Extension\Core\Type\LanguageType',
'locale' =>
'Symfony\Component\Form\Extension\Core\Type\LocaleType', 'money'
=> 'Symfony\Component\Form\Extension\Core\Type\MoneyType', 'number' =>
'Symfony\Component\Form\Extension\Core\Type\NumberType',
'password' =>
'Symfony\Component\Form\Extension\Core\Type\PasswordType',
'percent' =>
'Symfony\Component\Form\Extension\Core\Type\PercentType',
'radio' =>
'Symfony\Component\Form\Extension\Core\Type\RadioType',
'repeated' =>
'Symfony\Component\Form\Extension\Core\Type\RepeatedType',
'search' =>
'Symfony\Component\Form\Extension\Core\Type\SearchType',
'textarea' =>
'Symfony\Component\Form\Extension\Core\Type\TextareaType',
'text' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'time' => 'Symfony\Component\Form\Extension\Core\Type\TimeType',
'timezone' =>
'Symfony\Component\Form\Extension\Core\Type\TimezoneType', 'url'
=> 'Symfony\Component\Form\Extension\Core\Type\UrlType', 'button' =>
'Symfony\Component\Form\Extension\Core\Type\ButtonType',
'submit' =>
'Symfony\Component\Form\Extension\Core\Type\SubmitType', 'reset'
=> 'Symfony\Component\Form\Extension\Core\Type\ResetType', 'currency' =>
'Symfony\Component\Form\Extension\Core\Type\CurrencyType',
'entity' => 'Symfony\Bridge\Doctrine\Form\Type\EntityType',
'sonata_type_immutable_array' =>
'Sonata\CoreBundle\Form\Type\ImmutableArrayType',
'sonata_type_boolean' =>
'Sonata\CoreBundle\Form\Type\BooleanType',
'sonata_type_collection' =>
'Sonata\CoreBundle\Form\Type\CollectionType',
'sonata_type_translatable_choice' =>
'Sonata\CoreBundle\Form\Type\TranslatableChoiceType',
'sonata_type_date_range' =>
'Sonata\CoreBundle\Form\Type\DateRangeType',
'sonata_type_datetime_range' =>
'Sonata\CoreBundle\Form\Type\DateTimeRangeType',
'sonata_type_date_picker' =>
'Sonata\CoreBundle\Form\Type\DatePickerType',
'sonata_type_datetime_picker' =>
'Sonata\CoreBundle\Form\Type\DateTimePickerType',
'sonata_type_date_range_picker' =>
'Sonata\CoreBundle\Form\Type\DateRangePickerType',
'sonata_type_datetime_range_picker' =>
'Sonata\CoreBundle\Form\Type\DateTimeRangePickerType',
'sonata_type_equal' => 'Sonata\CoreBundle\Form\Type\EqualType',
'sonata_type_color_selector' =>
'Sonata\CoreBundle\Form\Type\ColorSelectorType'), array('form'
=>array('sonata.admin.form.extension.field', 'mopa_bootstrap.form.type_extension.help',
'mopa_bootstrap.form.type_extension.legend',
'mopa_bootstrap.form.type_extension.error',
'mopa_bootstrap.form.type_extension.widget',
'mopa_bootstrap.form.type_extension.horizontal',
'mopa_bootstrap.form.type_extension.widget_collection',
'mopa_bootstrap.form.type_extension.tabbed',
'form.type_extension.form.http_foundation',
'form.type_extension.form.validator', 'form.type_extension.csrf',
'form.type_extension.form.data_collector'), 'choice'
=>array('sonata.admin.form.extension.choice'), 'button' => array('mopa_bootstrap.form.type_extension.button'), 'date' =>array('mopa_bootstrap.form.type_extension.date'), 'repeated' => array('form.type_extension.repeated.validator'), 'submit' =>array('form.type_extension.submit.validator')), array())
in var/cache/dev/appDevDebugProjectContainer.php (line 1325)
Here my answer mentioned in the comments:
Probably you have to upgrade your SonataCoreBundle dependency version as well. Maybe this one is not compatible with Symfony 3.3.
I am validating an array in Laravel. I get "0.id has already been taken." in default error message. So I added a 2nd parameter in my validator: 'unique' =>':attribute has already been taken. Please fix your data in spreadsheet.' and shows "0.id has already been taken. Please fix your data in spreadsheet.". I added the 3rd parameter which is the custom attribute. ['*.id' =>'Student ID']. But I want to have a message like this: ID has already been taken. Please fix your data in spreadsheet in line 1.
Here's my full validation code:
$validate = $request - > validate([
'*.id' => 'required|unique:students|numeric',
'*.rfid_number' => 'required|unique:students|numeric',
'*.first_name' => 'required|alpha|max:100',
'*.middle_name' => 'alpha|max:100|nullable',
'*.last_name' => 'required|string|max:100',
'*.name_extension' => 'alpha|max:10|nullable',
'*.email' => 'required|email|unique:students',
'*.photo' => 'string|nullable',
'*.house_number' => 'required|integer',
'*.barangay' => 'required|alpha|max:100',
'*.city' => 'required|alpha|max:100',
'*.province' => 'required|string|max:100',
'*.zip_code' => 'required|integer',
'*.birth_date' => 'required|date|max:100',
'*.birth_place' => 'string|max:200',
'*.gender' => 'required|alpha',
'*.religion' => 'alpha|max:100|nullable',
'*.landline_number' => 'numeric|max:20|nullable',
'*.mobile_number' => 'required',
'*.father_name' => 'string|max:200|required',
'*.father_occupation' => 'string|max:200|nullable',
'*.mother_name' => 'string|max:200|required',
'*.mother_occupation' => 'string|max:200|nullable',
'*.guardian_name' => 'string|max:200|required',
'*.guardian_occupation' => 'string|max:200|nullable',
'*.guardian_address' => 'string|max:200|nullable',
'*.year' => 'integer|max:10|required',
'*.section' => 'alpha|max:200|required'
], [
'unique' => ':attribute has already been taken. Please fix your data in spreadsheet.'
], [ //attributes
'*.id' => 'Student ID'
]);
Something like this would do the trick:
$validate = $request->validate([
//
], [
//
],
collect($request->all())->keys()->flatMap(function ($index) {
return ["$index.id" => 'ID'];
})->toArray());
Iterate over all the indexes so you end up with something like:
[
'0.id' => 'ID',
'1.id' => 'ID',
'2.id' => 'ID',
'3.id' => 'ID',
'4.id' => 'ID',
'5.id' => 'ID',
'6.id' => 'ID',
'7.id' => 'ID',
]
As your final array to the validator