How to implement email validation on admin form in Magento2 - validation

I want to use email validation in admin form of my custom module. I've seen core module but couldn't get exact idea.

You can also validate the 'email' text field by adding a class ['validate-email'] in your form
$fieldset->addField(
'email',
'text',
[
'name' => 'email',
'label' => __('Email'),
'title' => __('Email'),
'required' => true,
'class' => 'validate-email'
]
);

Validation is Model's issue. Only model knows how your data should look like. You describe your data fields in model, so you should describe validation rules for this fields in the same place.
It seems to be obvious for me, but I'd gladly listen to opponents.
put database validation in the Model (assuming it's a db model) and http data validation in the controller. Xss filtering, for example, does not pertain to the Model. it pertains to the Controller in input and to the View in output

I found an answer by myself, I used PHP email validation in the save controller.

Related

Laravel Backpack

I need help. I am currently working with Laravel backpack and I have the following problem: I have a field called Media, that field has two behaviors, one to upload images and the other to write a URL, the problem is that it only lets me create the one time the field. Is there a way in laravel backpack to create 2 different fields with the same name?
I want to achieve something more or less like this:
CRUD::addField([ 'name' => 'url_media', 'label' => 'url_media', 'type' => 'upload',
]);
CRUD::addField([ 'name' => 'url_media', 'label' => 'url_media', 'type' => 'url', ]);
Because of the HTML spec, you cannot have two inputs with the same name in a form. I mean... you can. But only the last one will be sent in the request.
If you don't want to show both fields at the same time, you can use a conditional (if/else) to show either the upload field or the url field.
Otherwise, a different way would be to choose a fake name for the second field (say... url_media_link), then use a Laravel Mutator in your model, in this case setUrlMediaLinkAttribute() to do what you manipulate the model attributes as you want.

Laravel: Validate input only if available in DOM

I created a form with the following fields:
Name
Email
Country
City
Address
If the user selects a country that has states (ex. United States) then the form transforms to:
Name
Email
Country
State
City
Address
To validate this I created a separate form request like so:
public function rules()
{
return [
'name' => 'required|max:255',
'email' => 'required|email,
'country_id' => 'required|integer',
'state_id' => 'nullable|integer',
'city_id' => 'required|integer',
'address' => 'required',
];
}
The problem is that if I leave it like that, then if I don't select a state it will pass validation.
If i make it:
'state_id' => 'sometimes|nullable|integer',
Then again it passes validation.
If I make it:
'state_id' => 'required|nullable|integer',
It will not pass validation, but then again it will throw a validation error if there is no state field in the form.
I read a lot of articles about this but nothing seems to solve it for me.
PS1: I want to solve this in the form request, not in the controller. I assume that an
if($request->has('states')){...}
can help, but then again, i would like to keep everything tidy in the form request.
PS2: I am using VueJS and Axios to add/remove states from the form. The whole form is actually a Vue component.
Any clues?
Thank you in advance!
You can conditionally add rules via the sometimes method on Validator.
$v->sometimes('state_id', 'required|integer', function ($input) {
return in_array($input->countries, [1,2,3,4...]
});
You could use the required_with line of parameters, but because the validation is based on the value of the input instead of just the presence, the custom validation rule is probably your best bet.
Per https://laravel.com/docs/5.7/validation#conditionally-adding-rules

Laravel form request validation

Is there any way to get original data in request?
I mean, in my case I want to validate updating user's in the form. I want the email to be unique on emails in database except on the email that user wrote to the form.
Laravel already has a validation rule for this, you can add an id of the user to ignore to your unique validator.
'email' => [
'required',
Rule::unique('users')->ignore($currentUser->id),
]
I used something like this:
'email' => ['sometimes', 'email', 'unique:users,email,'.$user->getKey().','.$user->getKeyName().',deleted_at,NULL', 'string'],
Not sure if it is the best solution but works. I kind of do not like it because the $user is based on hidden input from the request.
You can do it liks this:
'email' => 'unique:users,email,'.$user->id
The user id will allow you to keep updating the record but maintain the unique constraint on email. You don't need a hidden input field for $user->id btw.

Validate against only a part of the defined constraints

I have this field:
$builder->add('offices', 'entity', array(
'class' => 'ControlPanelBundle:Offices',
'property' => 'name',
));
In my form I am only using one field from all possible fields in this Offices object. When I am submitting, it tries to validate all the fields. How can I temporary disable validation only for this time?
Validation Groups are designed specifically for that.

Accessing Different Controller Method from A View with Ajax->form()

In my conversations view, I'm trying to make it so I can add messages to a conversation.
Currently I have a Conversation hasMany Messages relationship.
Now when I try to invoke the following code:
<?=$ajax->form('message','post',array('update'=>'messages')); ?>
It produces a form with a form action to
action="facebook/conversations/messages/add"
So I get an error saying I don't have the a controller function labeled "messages" in my conversations controller.
I want the action to go to my messages controller instead.
I'm sure its some really silly code I have to implement, but I'd really appreciate your help.
You can pass a url explicitly when you create a form.
echo $ajax->form('message', 'post', array('url'=>$html->url(array('controller'=>'messages', 'action'=>'action_name'))));
From the CakePHP Book you can also use a slightly different variant of the Ajax form function and avoid using the Html Helper to build the url.
$this->Ajax->form( array(
'type' => 'post',
'options' => array(
'url' => array(
'controller' => 'messages',
'action' => 'action_name'
)
)
));

Resources