Flexbase
Search…
INSERT : Plugin Validation
BasicCRUD Insert Query with Bridge
Now let us look at another type of validation. That is the plug-in validation.
The plug-in validation happens in the ProcessServices. You see a ProcessCustomerServices, it creates a sequence with the data packet and the data packet contains the data that was passed as parameter from the controller. In the controller, we are passing the parameter that contains the ContextInfo and the InputAPIModel. Now the first validation that we will look at is the validation which are more complex in scenarios. We can have more database interactions or we have something we want to validate something like the aadhar number. So this is one validation that is not started with number, if you see it cause a packets method do not start with number.
1
public partial class AddCustomerDataPacket : FlexiFlowDataPacketBridge
2
{
3
#region "Validations"
4
//Validations goes here
5
/// <summary>
6
///
7
/// </summary>
8
public async Task DoesNotStartWithNumber()
9
{
10
11
//If any validation fails, uncomment and use the below line of code to add error to the packet
12
13
if (Regex.IsMatch(this.InputParams.Model.Name, @"^\d+"))
14
this.AddError("Name", quot;Name {this.InputParams.Model.Name} cannot start with numbers");
15
16
17
await Task.CompletedTask; //If you have any await in the validation, remove this line
18
}
19
20
#endregion
21
22
}
Copied!
Next validation, I am checking if the name that was provided by the model is unique, if any duplicate name is found. We are adding the message in the data packets error.
1
public partial class AddCustomerDataPacket : FlexiFlowDataPacketBridge
2
{
3
#region "Validations"
4
//Validations goes here
5
/// <summary>
6
///
7
/// </summary>
8
public async Task IsUnique()
9
{
10
11
//If any validation fails, uncomment and use the below line of code to add error to the packet
12
//this.AddError("key", "ErrorMessage");
13
14
_connectionProvider.ConfigureDbConnectionString(this.InputParams.HostContextInfo);
15
_repo.InitializeConnection(_connectionProvider);
16
17
var custId = _repo.FindAll<Customer>().Where(c => c.Name == this.InputParams.Model.Name).Select(s => s.Id).FirstOrDefault();
18
if (custId != null)
19
this.AddError("Name", quot;Duplicate {this.InputParams.Model.Name} Customer found");
20
21
await Task.CompletedTask; //If you have any await in the validation, remove this line
22
}
23
24
#endregion
25
26
}
Copied!
And refer to the repo instance in the constructor of your data packet as shown below:
1
public partial class AddCustomerDataPacket : FlexiFlowDataPacketBridge
2
{
3
4
readonly ILogger<AddCustomerDataPacket> _logger;
5
IFlexRepositoryBridge _repo;
6
IWriteDbConnectionProviderBridge _connectionProvider;
7
8
/// <summary>
9
///
10
/// </summary>
11
/// <param name="logger"></param>
12
public AddCustomerDataPacket(ILogger<AddCustomerDataPacket> logger, IFlexRepositoryBridge repo, IWriteDbConnectionProviderBridge connectionProvider)
13
{
14
_logger = logger;
15
_repo = repo;
16
_connectionProvider = connectionProvider;
17
}
18
19
#region "Properties
20
//Models and other properties goes here
21
22
public AddCustomerParams InputParams { get; set; }
23
24
#endregion
25
}
Copied!
Last modified 1yr ago
Copy link